Пример #1
0
Requester::waitForResponse(Protocol *protocol, bool wbxml_mode)
{
  ULXR_TRACE(ULXR_PCHAR("waitForResponse(Protocol, wbxml)"));
  char buffer[ULXR_RECV_BUFFER_SIZE];
  char *buff_ptr;

  std::auto_ptr<XmlParserBase> parser;
  MethodResponseParserBase *rpb = 0;
  if (wbxml_mode)
  {
    ULXR_TRACE(ULXR_PCHAR("waitForResponse in WBXML"));
    MethodResponseParserWb *rp = new MethodResponseParserWb();
    rpb = rp;
#ifdef _MSC_VER
  std::auto_ptr<XmlParserBase> temp(rp);
  parser = temp;
#else
    parser.reset(rp);
#endif
  }
  else
  {
    ULXR_TRACE(ULXR_PCHAR("waitForResponse in XML"));
    MethodResponseParser *rp = new MethodResponseParser();
    rpb = rp;
#ifdef _MSC_VER
    std::auto_ptr<XmlParserBase> temp(rp);
    parser = temp;
#else
    parser.reset(rp);
#endif
  }

  bool done = false;
  long readed;
  while (!done && protocol->hasBytesToRead()
               && ((readed = protocol->readRaw(buffer, sizeof(buffer))) > 0) )
  {
    buff_ptr = buffer;
    while (readed > 0)
    {
      Protocol::State state = protocol->connectionMachine(buff_ptr, readed);
      if (state == Protocol::ConnError)
      {
        done = true;
        throw ConnectionException(TransportError, ulxr_i18n(ULXR_PCHAR("network problem occured")), 400);
      }

      else if (state == Protocol::ConnSwitchToBody)
      {
#ifdef ULXR_SHOW_READ
        Cpp8BitString super_data (buff_ptr, readed);
        while ((readed = protocol->readRaw(buffer, sizeof(buffer))) > 0)
          super_data.append(buffer, readed);
        ULXR_DOUT_READ(ULXR_PCHAR("superdata 3 start:\n"));

        if (wbxml_mode)
        {
           ULXR_DOUT_READ(binaryDebugOutput(super_data));
        }
        else
        {
          ULXR_DOUT_READ(ULXR_GET_STRING(super_data));
        }
        ULXR_DOUT_READ(ULXR_PCHAR("superdata 3 end:\n") );
#endif
        if (!protocol->hasBytesToRead())
        {
          throw ConnectionException(NotConformingError,
                                    ulxr_i18n(ULXR_PCHAR("Content-Length of message not available")), 411);
        }

        CppString s;
        if (!protocol->responseStatus(s))
          throw ConnectionException(TransportError, s, 500);

      }

      else if (state == Protocol::ConnBody)
      {
        ULXR_DOUT_XML(ULXR_GET_STRING(std::string(buff_ptr, readed)));
        if (!parser->parse(buff_ptr, readed, false))
        {
          throw XmlException(parser->mapToFaultCode(parser->getErrorCode()),
                             ulxr_i18n(ULXR_PCHAR("Problem while parsing xml response")),
                             parser->getCurrentLineNumber(),
                             ULXR_GET_STRING(parser->getErrorString(parser->getErrorCode())));
        }
        readed = 0;
      }
    }

    if (!protocol->hasBytesToRead())
//        || parser->isComplete())
      done = true;
  }

  if (protocol->isOpen() && !protocol->isPersistent() )
    protocol->close();

  return rpb->getMethodResponse();
}
Пример #2
0
MethodCall Dispatcher::waitForCall(int _timeout)
{
  ULXR_TRACE("waitForCall");
  if (!protocol->isOpen())
  {
    if (!protocol->accept(_timeout))
      return MethodCall();  // // @todo throw exception?
  }
  else
    protocol->resetConnection();

  char buffer[ULXR_RECV_BUFFER_SIZE];
  char *buff_ptr;


  std::auto_ptr<XmlParserBase> parser;
  MethodCallParserBase *cpb = 0;
  ULXR_TRACE("waitForCall in XML");
  MethodCallParser *cp = new MethodCallParser();
  cpb = cp;
  parser.reset(cp);

  bool done = false;
  long myRead;
  while (!done && ((myRead = protocol->readRaw(buffer, sizeof(buffer))) > 0) )
  {
    buff_ptr = buffer;
    while (myRead > 0)
    {
      Protocol::State state = protocol->connectionMachine(buff_ptr, myRead);
      if (state == Protocol::ConnError)
        throw ConnectionException(TransportError, "network problem occured", 500);

      else if (state == Protocol::ConnSwitchToBody)
      {
        if (!protocol->hasBytesToRead())
        {
#ifdef ULXR_SHOW_READ
          std::string super_data(buff_ptr, myRead);
          while ((myRead = protocol->readRaw(buffer, sizeof(buffer))) > 0)
            super_data.append(buffer, myRead);
          ULXR_DOUT_READ("superdata 1 start:\n"
                         << super_data
                         << "superdata 1 end:\n");
#endif
          throw ConnectionException(NotConformingError,  "Content-Length of message not available", 411);
        }
      }

      else if (state == Protocol::ConnBody)
      {
        ULXR_DOUT_XML(std::string(buff_ptr, myRead));
        if (!parser->parse(buff_ptr, myRead, done))
        {
          ULXR_DOUT("errline: " << parser->getCurrentLineNumber());
          ULXR_DWRITE(buff_ptr, myRead);
          ULXR_DOUT("") ;

          throw XmlException(parser->mapToFaultCode(parser->getErrorCode()),
                             "Problem while parsing xml request",
                             parser->getCurrentLineNumber(),
                             parser->getErrorString(parser->getErrorCode()));
        }
        myRead = 0;
      }
    }

    if (!protocol->hasBytesToRead())
//        || parser->isComplete())
      done = true;
  }

  ULXR_TRACE("waitForCall got " << cpb->getMethodCall().getXml());
  return cpb->getMethodCall();
}