Logo Search packages:      
Sourcecode: pwlib version File versions  Download package

void XMLRPCApp::Main (  ) [virtual]

User override function for the main execution routine of the thread. A descendent class must provide the code that will be executed in the thread within this function.

Note that the correct way for a thread to terminate is to return from this function.

Implements PThread.

Definition at line 102 of file main.cxx.

References PString::Empty(), PProcess::GetArguments(), PArgList::GetCount(), PArgList::GetOptionCount(), PArgList::GetOptionString(), PArgList::GetParameters(), PArgList::HasOption(), PTrace::Initialise(), PString::IsEmpty(), and PArgList::Parse().

{
  PINDEX i;
  PArgList & args = GetArguments();

  args.Parse("a-array."
             "d-debug."
             "f-float."
             "i-integer."
             "o-output:"
             "s-struct."
             "t-trace."
             "-echo-struct."
             );

  PTrace::Initialise(args.GetOptionCount('t'),
                     args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL);

  if (args.GetCount() < 2) {
    PError << "usage: xmlrpc url method [parms...]" << endl;
    return;
  }

  PString url    = args[0];
  PString method = args[1];

  PXMLRPC rpc(url);

  PXMLRPCBlock request(method);
  PXMLRPCBlock response;

  if (args.HasOption("echo-struct")) {
    TestStruct ts;
    ts.a_date -= PTimeInterval(0, 0, 0, 0, 5);

    ts.a_binary.SetSize(10);
    for (i = 0; i < 10; i++)
      ts.a_binary[i] = (BYTE)(i+1);

    ts.a_string_array.SetSize(3);
    ts.a_string_array[0] = "first";
    ts.a_string_array[1] = "second";
    ts.a_string_array[2] = "third";

    ts.an_integer_array.SetSize(7);
    for (i = 0; i < ts.an_integer_array.GetSize(); i++)
      ts.an_integer_array[i] = i+1;

    ts.a_float_array.SetSize(5);
    for (i = 0; i < ts.a_float_array.GetSize(); i++)
      ts.a_float_array[i] = (float)(1.0/(i+2));

    ts.nested_struct.another_string = "Another string!";
    ts.nested_struct.another_integer = 345;

    ts.array_struct.SetSize(2);
    ts.array_struct.SetAt(0, new NestedStruct);
    ts.array_struct[0].another_string = "Structure one";
    ts.array_struct[0].another_integer = 11111;
    ts.array_struct.SetAt(1, new NestedStruct);
    ts.array_struct[1].another_string = "Structure two";
    ts.array_struct[1].another_integer = 22222;
    request.AddParam(ts);
  }
  else {
    if (args.HasOption('a')) {
      if (args.HasOption('s')) {
        PArray<PStringToString> array;
        PStringToString dict;
        PString key;
        for (i = 2; i < args.GetCount(); i++) {
          if (args[i] == ",") {
            array.SetAt(array.GetSize(), new PStringToString(dict));
            dict = PStringToString();
            key = PString::Empty();
          }
          else if (key.IsEmpty())
            key = args[i];
          else {
            dict.SetAt(key, args[i]);
            key = PString::Empty();
          }
        }

        if (!dict.IsEmpty())
          array.SetAt(array.GetSize(), new PStringToString(dict));

        request.AddArray(array);
      }
      else if (args.HasOption('i'))
        request.AddArray(args.GetParameters(2), "int");
      else if (args.HasOption('f'))
        request.AddArray(args.GetParameters(2), "double");
      else
        request.AddArray(args.GetParameters(2));
    }
    else if (args.HasOption('s')) {
      PStringToString dict;
      for (i = 2; (i+1) < args.GetCount(); i += 2) {
        PString key   = args[i];
        PString value = args[i+1];
        dict.SetAt(key, value);
      }

      request.AddStruct(dict);
    }
    else {
      for (i = 2; i < args.GetCount(); i++) {
        if (args.HasOption('i'))
          request.AddParam(args[i].AsInteger());
        else if (args.HasOption('f'))
          request.AddParam(args[i].AsReal());
        else
          request.AddParam(args[i]);
      }
    }
  }

  if (args.HasOption('d'))
    cout << "Request = " << request << endl;

  if (!rpc.MakeRequest(request, response)) {
    PError << "Error in request (" 
           << rpc.GetFaultCode() 
           << ") : "
           << rpc.GetFaultText()
           << endl;
    return;
  }

  // scan through the response and print it out
  cout << "Response" << endl;
  for (i = 0; i < response.GetParamCount(); i++) {
    cout << "  " << i << ": ";
    PString type;
    PString val;
    if (response.GetParam(i, type, val)) {
      cout << type << " = ";
      if (type == "struct") {
        PStringToString dict;
        response.GetParam(i, dict);
        cout << '\n' << dict;
      }
      else if (type == "array") {
        PStringArray array;
        response.GetParam(i, array);
        cout << '\n' << setfill('\n') << array << setfill(' ');
      }
      else
        cout << val;
    }
    else
      cout << "error: " << response.GetFaultText();
    cout << endl;
  }

  if (args.HasOption("echo-struct")) {
    TestStruct ts;
    ts.a_date = PTime(0);
    if (response.GetParam(0, ts))
      cout << "Parsed response:\n" << ts;
    else
      cout << "Failed to parse resonse: " << response.GetFaultText();
    cout << endl;
  }
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index