XmlRpcInterface Class Reference

#include <XmlRpcInterface.h>

Inheritance diagram for XmlRpcInterface:

BaseApp XmlRpc::XmlRpcServer BaseRpc XmlRpc::XmlRpcSource RpcListener

List of all members.


Detailed Description

Applicaton that communicates with a realword application via a socket.

Definition at line 42 of file XmlRpcInterface.h.


Public Member Functions

 XmlRpcInterface ()
 ~XmlRpcInterface ()
virtual void initializeApp (int stage)
 initializes derived class-attributes
virtual void handleMessage (cMessage *msg)
 The "main loop".
void handleRpcTimeout (BaseCallMessage *msg, const TransportAddress &dest, cPolymorphic *context, int rpcId, const OverlayKey &)
 This method is called if an RPC timeout has been reached.
void localLookup (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void lookup (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void p2pnsRegister (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void p2pnsResolve (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void put (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void get (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void dumpDht (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
void joinOverlay (XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)

Protected Types

enum  ServerConnectionState { READ_HEADER, READ_REQUEST, EXECUTE_REQUEST, WRITE_RESPONSE }
 Possible IO states for the connection. More...

Protected Member Functions

bool readHeader (char *buf, uint32_t length)
 Reads the http header.
bool readRequest (char *buf, uint32_t length)
 Reads the request (based on the content-length header value).
bool writeResponse ()
 Executes the request and writes the resulting response.
bool isPrivileged ()
 Check if the connected application is allowed to call privileged methods.
void handleRealworldPacket (char *buf, uint32_t len)
void handleCommonAPIPacket (cMessage *msg)
void handleRpcResponse (BaseResponseMessage *msg, cPolymorphic *context, int rpcId, simtime_t rtt)
 This method is called if an RPC response has been received.
void resetConnectionState ()
 Reset the internal connection state.
void closeConnection ()
void sendInternalRpcWithTimeout (CompType destComp, BaseCallMessage *call)

Protected Attributes

unsigned int mtu
cMessage * packetNotification
RealtimeScheduler::PacketBuffer packetBuffer
RealtimeScheduler * scheduler
std::map< int,
XmlRpcConnectionState > 
state
SOCKET curAppFd
bool limitAccess
XmlRpc::XmlRpcServerMethod * _localLookup
XmlRpc::XmlRpcServerMethod * _lookup
XmlRpc::XmlRpcServerMethod * _register
XmlRpc::XmlRpcServerMethod * _resolve
XmlRpc::XmlRpcServerMethod * _put
XmlRpc::XmlRpcServerMethod * _get
XmlRpc::XmlRpcServerMethod * _dumpDht
XmlRpc::XmlRpcServerMethod * _joinOverlay

Classes

struct  XmlRpcConnectionState

Member Enumeration Documentation

Possible IO states for the connection.

Enumerator:
READ_HEADER 
READ_REQUEST 
EXECUTE_REQUEST 
WRITE_RESPONSE 

Definition at line 61 of file XmlRpcInterface.h.


Constructor & Destructor Documentation

XmlRpcInterface::XmlRpcInterface (  ) 

Definition at line 451 of file XmlRpcInterface.cc.

00452 {
00453     _localLookup = NULL;
00454     _lookup = NULL;
00455     _register = NULL;
00456     _resolve = NULL;
00457     _put = NULL;
00458     _get = NULL;
00459     _dumpDht = NULL;
00460     _joinOverlay = NULL;
00461 
00462     packetNotification = NULL;
00463 }

XmlRpcInterface::~XmlRpcInterface (  ) 

Definition at line 465 of file XmlRpcInterface.cc.

00466 {
00467     delete _localLookup;
00468     delete _lookup;
00469     delete _register;
00470     delete _resolve;
00471     delete _put;
00472     delete _get;
00473     delete _dumpDht;
00474     delete _joinOverlay;
00475 
00476     cancelAndDelete(packetNotification);
00477 }


Member Function Documentation

bool XmlRpcInterface::readHeader ( char *  buf,
uint32_t  length 
) [protected]

Reads the http header.

Definition at line 806 of file XmlRpcInterface.cc.

Referenced by handleRealworldPacket().

00807 {
00808     // Read available data
00809     bool eof = false;
00810 
00811     state[curAppFd]._header.append(std::string(buf, length));
00812 
00813     if (length <= 0) {
00814         // Its only an error if we already have read some data
00815         if (state[curAppFd]._header.length() > 0)
00816             XmlRpcUtil::error("XmlRpcServerConnection::readHeader: error "
00817                 "while reading header.");
00818         return false;
00819     }
00820 
00821     XmlRpcUtil::log(4, "XmlRpcServerConnection::readHeader: read %d bytes.",
00822                     state[curAppFd]._header.length());
00823     char *hp = (char*)state[curAppFd]._header.c_str(); // Start of header
00824     char *ep = hp + state[curAppFd]._header.length(); // End of string
00825     char *bp = 0; // Start of body
00826     char *lp = 0; // Start of content-length value
00827     char *kp = 0; // Start of connection value
00828 
00829     for (char *cp = hp; (bp == 0) && (cp < ep); ++cp) {
00830         if ((ep - cp > 16) && (strncasecmp(cp, "Content-length: ", 16) == 0))
00831             lp = cp + 16;
00832         else if ((ep - cp > 12) && (strncasecmp(cp, "Connection: ", 12) == 0))
00833             kp = cp + 12;
00834         else if ((ep - cp > 4) && (strncmp(cp, "\r\n\r\n", 4) == 0))
00835             bp = cp + 4;
00836         else if ((ep - cp > 2) && (strncmp(cp, "\n\n", 2) == 0))
00837             bp = cp + 2;
00838     }
00839 
00840     // If we haven't gotten the entire header yet, return (keep reading)
00841     if (bp == 0) {
00842         // EOF in the middle of a request is an error, otherwise its ok
00843         if (eof) {
00844             XmlRpcUtil::log(4, "XmlRpcServerConnection::readHeader: EOF");
00845             if (state[curAppFd]._header.length() > 0)
00846                 XmlRpcUtil::error("XmlRpcServerConnection::readHeader: EOF while reading header");
00847             return false; // Either way we close the connection
00848         }
00849 
00850         return true; // Keep reading
00851     }
00852 
00853     // Decode content length
00854     if (lp == 0) {
00855         XmlRpcUtil::error("XmlRpcServerConnection::readHeader: No Content-length specified");
00856         return false; // We could try to figure it out by parsing as we read, but for now...
00857     }
00858 
00859     state[curAppFd]._contentLength = atoi(lp);
00860     if (state[curAppFd]._contentLength <= 0) {
00861         XmlRpcUtil::error(
00862                           "XmlRpcServerConnection::readHeader: Invalid Content-length specified (%d).",
00863                           state[curAppFd]._contentLength);
00864         return false;
00865     }
00866 
00867     XmlRpcUtil::log(
00868                      3,
00869                     "XmlRpcServerConnection::readHeader: specified content length is %d.",
00870                     state[curAppFd]._contentLength);
00871 
00872     // Otherwise copy non-header data to request buffer and set state to read request.
00873     state[curAppFd]._request = bp;
00874 
00875     // Parse out any interesting bits from the header (HTTP version, connection)
00876     state[curAppFd]._keepAlive = true;
00877     if (state[curAppFd]._header.find("HTTP/1.0") != std::string::npos) {
00878         if (kp == 0 || strncasecmp(kp, "keep-alive", 10) != 0)
00879             state[curAppFd]._keepAlive = false; // Default for HTTP 1.0 is to close the connection
00880     } else {
00881         if (kp != 0 && strncasecmp(kp, "close", 5) == 0)
00882             state[curAppFd]._keepAlive = false;
00883     }
00884     XmlRpcUtil::log(3, "KeepAlive: %d", state[curAppFd]._keepAlive);
00885 
00886     state[curAppFd]._header = "";
00887     state[curAppFd]._connectionState = READ_REQUEST;
00888     return true; // Continue monitoring this source
00889 }

bool XmlRpcInterface::readRequest ( char *  buf,
uint32_t  length 
) [protected]

Reads the request (based on the content-length header value).

Definition at line 891 of file XmlRpcInterface.cc.

Referenced by handleRealworldPacket().

00892 {
00893     // If we dont have the entire request yet, read available data
00894     if (int(state[curAppFd]._request.length()) < state[curAppFd]._contentLength) {
00895         bool eof = false;
00896 
00897         state[curAppFd]._request.append(std::string(buf, length));
00898 
00899         if (length <= 0) {
00900             XmlRpcUtil::error("XmlRpcServerConnection::readRequest: read error.");
00901             return false;
00902         }
00903 
00904         // If we haven't gotten the entire request yet, return (keep reading)
00905         if (int(state[curAppFd]._request.length()) < state[curAppFd]._contentLength) {
00906             if (eof) {
00907                 XmlRpcUtil::error("XmlRpcServerConnection::readRequest: EOF while reading request");
00908                 return false; // Either way we close the connection
00909             }
00910             return true;
00911         }
00912     }
00913 
00914     // Otherwise, parse and dispatch the request
00915     XmlRpcUtil::log(3, "XmlRpcServerConnection::readRequest read %d bytes.",
00916                     state[curAppFd]._request.length());
00917     //XmlRpcUtil::log(5, "XmlRpcServerConnection::readRequest:\n%s\n", state[curAppFd]._request.c_str());
00918 
00919     state[curAppFd]._connectionState = WRITE_RESPONSE;
00920 
00921     return true; // Continue monitoring this source
00922 }

bool XmlRpcInterface::writeResponse (  )  [protected]

Executes the request and writes the resulting response.

Definition at line 924 of file XmlRpcInterface.cc.

Referenced by handleRealworldPacket(), handleRpcResponse(), and handleRpcTimeout().

00925 {
00926     if (state[curAppFd]._response.length() == 0) {
00927         state[curAppFd]._response = executeRequest(state[curAppFd]._request);
00928         state[curAppFd]._bytesWritten = 0;
00929 
00930         if (state[curAppFd]._connectionState == EXECUTE_REQUEST)
00931             return true;
00932 
00933         if (state[curAppFd]._response.length() == 0) {
00934             XmlRpcUtil::error("XmlRpcServerConnection::writeResponse: empty response.");
00935             return false;
00936         }
00937     }
00938 
00939     // Try to write the response
00940     int curBytesWritten = scheduler->sendBytes(state[curAppFd]._response.c_str(),
00941                                                state[curAppFd]._response.length(),
00942                                                0, 0, true, curAppFd);
00943 
00944     if (curBytesWritten <= 0) {
00945         XmlRpcUtil::error("XmlRpcServerConnection::writeResponse: write error.");
00946         return false;
00947     } else {
00948         state[curAppFd]._bytesWritten += curBytesWritten;
00949     }
00950 
00951     XmlRpcUtil::log(3,
00952                 "XmlRpcServerConnection::writeResponse: wrote %d of %d bytes.",
00953                 state[curAppFd]._bytesWritten, state[curAppFd]._response.length());
00954 
00955     // Prepare to read the next request
00956     if (state[curAppFd]._bytesWritten == int(state[curAppFd]._response.length())) {
00957         state[curAppFd]._header = "";
00958         state[curAppFd]._request = "";
00959         state[curAppFd]._response = "";
00960         state[curAppFd]._connectionState = READ_HEADER;
00961     }
00962 
00963     return state[curAppFd]._keepAlive; // Continue monitoring this source if true
00964 }

bool XmlRpcInterface::isPrivileged (  )  [protected]

Check if the connected application is allowed to call privileged methods.

Currently this is true for all applications connecting to localhost. If **.limitAccess = false there are no access restrictions.

Returns:
true, if the application is allowed to call privileged methods

Definition at line 412 of file XmlRpcInterface.cc.

Referenced by dumpDht(), joinOverlay(), p2pnsRegister(), and put().

00413 {
00414     if (limitAccess) {
00415         return state[curAppFd].localhost;
00416     } else {
00417         return true;
00418     }
00419 }

void XmlRpcInterface::handleRealworldPacket ( char *  buf,
uint32_t  len 
) [protected]

Definition at line 615 of file XmlRpcInterface.cc.

Referenced by handleMessage().

00616 {
00617     if (state[curAppFd]._connectionState == READ_HEADER) {
00618         if (!readHeader(buf, length)) {
00619             // discard data, if the header is invalid
00620             state[curAppFd]._header = "";
00621             state[curAppFd]._request = "";
00622             state[curAppFd]._response = "";
00623             state[curAppFd]._connectionState = READ_HEADER;
00624             return;
00625         }
00626     }
00627 
00628     if (state[curAppFd]._connectionState == READ_REQUEST)
00629         if (!readRequest(buf, length))
00630             return;
00631 
00632     if (state[curAppFd]._connectionState == WRITE_RESPONSE)
00633         if (!writeResponse() ) {
00634             closeConnection();
00635             return;
00636         }
00637 
00638     return;
00639 }

void XmlRpcInterface::handleCommonAPIPacket ( cMessage *  msg  )  [protected]

Definition at line 801 of file XmlRpcInterface.cc.

Referenced by handleMessage().

00802 {
00803     error("DHTXMLRealworldApp::handleCommonAPIPacket(): Unknown Packet!");
00804 }

void XmlRpcInterface::handleRpcResponse ( BaseResponseMessage *  msg,
cPolymorphic *  context,
int  rpcId,
simtime_t  rtt 
) [protected, virtual]

This method is called if an RPC response has been received.

Parameters:
msg The response message.
context Pointer to an optional state object. The object has to be handled/deleted by the handleRpcResponse() code
rpcId The RPC id.
rtt The Round-Trip-Time of this RPC

Reimplemented from RpcListener.

Definition at line 641 of file XmlRpcInterface.cc.

00645 {
00646     curAppFd = rpcId;
00647 
00648     if (state.count(curAppFd) == 0) {
00649         return;
00650     }
00651 
00652     RPC_SWITCH_START(msg)
00653     RPC_ON_RESPONSE(Lookup) {
00654         if (state[curAppFd]._connectionState != EXECUTE_REQUEST) break;
00655 
00656         XmlRpcValue resultValue;
00657         resultValue.setSize(_LookupResponse->getSiblingsArraySize());
00658 
00659         if (_LookupResponse->getIsValid() == true) {
00660             for (uint32_t i=0; i < _LookupResponse->getSiblingsArraySize();
00661                     i++) {
00662                 resultValue[i].setSize(3);
00663                 resultValue[i][0] =
00664                 _LookupResponse->getSiblings(i).ip.str();
00665                 resultValue[i][1] =
00666                 _LookupResponse->getSiblings(i).port;
00667                 resultValue[i][2] =
00668                 _LookupResponse->getSiblings(i).key.toString(16);
00669             }
00670             state[curAppFd]._response = generateResponse(resultValue.toXml());
00671         } else {
00672             std::cout << "XmlRpcInterface(): lookup() failed!" << endl;
00673             state[curAppFd]._response = generateFaultResponse("lookup() failed", 22);
00674         }
00675 
00676         state[curAppFd]._connectionState = WRITE_RESPONSE;
00677         if (!writeResponse()) {
00678             closeConnection();
00679         }
00680         break;
00681     }
00682     RPC_ON_RESPONSE(P2pnsRegister) {
00683         if (state[curAppFd]._connectionState != EXECUTE_REQUEST)
00684             break;
00685 
00686         XmlRpcValue resultValue;
00687 
00688         if (_P2pnsRegisterResponse->getIsSuccess() == true) {
00689             resultValue = 0;
00690             state[curAppFd]._response = generateResponse(resultValue.toXml());
00691         } else {
00692             std::cout << "XmlRpcInterface(): register() failed!" << endl;
00693             state[curAppFd]._response = generateFaultResponse("register() failed", 22);
00694         }
00695 
00696         state[curAppFd]._connectionState = WRITE_RESPONSE;
00697         if (!writeResponse() ) {
00698             closeConnection();
00699         }
00700         break;
00701     }
00702     RPC_ON_RESPONSE(P2pnsResolve) {
00703         if (state[curAppFd]._connectionState != EXECUTE_REQUEST)
00704             break;
00705 
00706         XmlRpcValue resultValue;
00707         resultValue.setSize(1);
00708 
00709         if (_P2pnsResolveResponse->getIsSuccess() == true) {
00710             resultValue[0] = XmlRpcValue(
00711                                          &(*(_P2pnsResolveResponse->getAddress().begin())),
00712                                          _P2pnsResolveResponse->getAddress().size());
00713             state[curAppFd]._response = generateResponse(resultValue.toXml());
00714         } else {
00715             std::cout << "XmlRpcInterface(): resolve() failed!" << endl;
00716             state[curAppFd]._response = generateFaultResponse("resolve() failed: Name not found", 9);
00717         }
00718 
00719         state[curAppFd]._connectionState = WRITE_RESPONSE;
00720         if (!writeResponse() ) {
00721             closeConnection();
00722         }
00723         break;
00724     }
00725     RPC_ON_RESPONSE(DHTputCAPI) {
00726         if (state[curAppFd]._connectionState != EXECUTE_REQUEST)
00727             break;
00728 
00729         XmlRpcValue resultValue;
00730 
00731         if (_DHTputCAPIResponse->getIsSuccess() == true) {
00732             resultValue = 0;
00733             state[curAppFd]._response = generateResponse(resultValue.toXml());
00734         } else {
00735             std::cout << "XmlRpcInterface(): put() failed!" << endl;
00736             state[curAppFd]._response = generateFaultResponse("put() failed", 22);
00737         }
00738 
00739         state[curAppFd]._connectionState = WRITE_RESPONSE;
00740         if (!writeResponse() ) {
00741             closeConnection();
00742         }
00743         break;
00744     }
00745     RPC_ON_RESPONSE(DHTgetCAPI) {
00746         if (state[curAppFd]._connectionState != EXECUTE_REQUEST)
00747             break;
00748 
00749         XmlRpcValue resultValue;
00750         resultValue.setSize(2);
00751         resultValue[0].setSize(1);
00752 
00753         if (_DHTgetCAPIResponse->getIsSuccess() == true) {
00754             resultValue[0][0] = XmlRpcValue(
00755                       &(*(_DHTgetCAPIResponse->getValue().begin())),
00756                       _DHTgetCAPIResponse->getValue().size());
00757             resultValue[1] = std::string();
00758             state[curAppFd]._response = generateResponse(resultValue.toXml());
00759         } else {
00760             std::cout << "XmlRpcInterface(): get() failed!" << endl;
00761             state[curAppFd]._response = generateFaultResponse("get() failed", 22);
00762         }
00763 
00764         state[curAppFd]._connectionState = WRITE_RESPONSE;
00765         if (!writeResponse() ) {
00766             closeConnection();
00767         }
00768         break;
00769     }
00770     RPC_ON_RESPONSE(DHTdump) {
00771         if (state[curAppFd]._connectionState != EXECUTE_REQUEST)
00772             break;
00773 
00774         XmlRpcValue resultValue;
00775         resultValue.setSize(_DHTdumpResponse->getRecordArraySize());
00776 
00777         for (uint32_t i=0; i < _DHTdumpResponse->getRecordArraySize();
00778              i++) {
00779             resultValue[i].setSize(3);
00780             resultValue[i][0] =
00781                 _DHTdumpResponse->getRecord(i).key.toString(16);
00782             resultValue[i][1] = XmlRpcValue(
00783                  &(*(_DHTdumpResponse->getRecord(i).value.begin())),
00784                  _DHTdumpResponse->getRecord(i).value.size());
00785             resultValue[i][2] =
00786                 _DHTdumpResponse->getRecord(i).ttl;
00787         }
00788 
00789         state[curAppFd]._response = generateResponse(resultValue.toXml());
00790 
00791         state[curAppFd]._connectionState = WRITE_RESPONSE;
00792          if (!writeResponse()) {
00793              closeConnection();
00794          }
00795          break;
00796 
00797     }
00798     RPC_SWITCH_END( )
00799 }

void XmlRpcInterface::resetConnectionState (  )  [protected]

Reset the internal connection state.

This is called after a RPC has finished and the socket was closed.

Definition at line 479 of file XmlRpcInterface.cc.

Referenced by closeConnection(), and handleMessage().

00480 {
00481     if (state.count(curAppFd) && state[curAppFd].pendingRpc) {
00482         cancelRpcMessage(state[curAppFd].pendingRpc);
00483     }
00484 
00485     state[curAppFd].appFd = INVALID_SOCKET;
00486     state[curAppFd].localhost = false;
00487     state[curAppFd]._header = "";
00488     state[curAppFd]._request = "";
00489     state[curAppFd]._response = "";
00490     state[curAppFd]._connectionState = READ_HEADER;
00491     state[curAppFd]._keepAlive = true;
00492     state[curAppFd].pendingRpc = 0;
00493 }

void XmlRpcInterface::closeConnection (  )  [protected]

Definition at line 495 of file XmlRpcInterface.cc.

Referenced by handleRealworldPacket(), handleRpcResponse(), and handleRpcTimeout().

00496 {
00497     scheduler->closeAppSocket(curAppFd);
00498     resetConnectionState();
00499 }

void XmlRpcInterface::sendInternalRpcWithTimeout ( CompType  destComp,
BaseCallMessage *  call 
) [protected]

Definition at line 501 of file XmlRpcInterface.cc.

Referenced by dumpDht(), get(), lookup(), p2pnsRegister(), p2pnsResolve(), and put().

00503 {
00504     state[curAppFd].pendingRpc = sendInternalRpcCall(destComp, call, NULL,
00505                                                      XMLRPC_TIMEOUT, 0,
00506                                                      curAppFd);
00507 }

void XmlRpcInterface::initializeApp ( int  stage  )  [virtual]

initializes derived class-attributes

Parameters:
stage the init stage

Reimplemented from BaseApp.

Definition at line 421 of file XmlRpcInterface.cc.

00422 {
00423     // all initialization is done in the first stage
00424     if (stage != MIN_STAGE_APP)
00425         return;
00426 
00427     packetNotification = new cMessage("packetNotification");
00428     mtu = par("mtu");
00429     limitAccess = par("limitAccess");
00430 
00431     scheduler = check_and_cast<RealtimeScheduler *>(simulation.getScheduler());
00432     scheduler->setInterfaceModule(this, packetNotification, &packetBuffer, mtu,
00433                                    true);
00434 
00435     XmlRpc::setVerbosity(1);
00436 
00437     _localLookup = new LocalLookup(this);
00438     _lookup = new Lookup(this);
00439     _register = new P2pnsRegister(this);
00440     _resolve = new P2pnsResolve(this);
00441     _put = new Put(this);
00442     _get = new Get(this);
00443     _dumpDht = new DumpDht(this);
00444     _joinOverlay = new JoinOverlay(this);
00445 
00446     enableIntrospection(true);
00447 
00448     curAppFd = INVALID_SOCKET;
00449 }

void XmlRpcInterface::handleMessage ( cMessage *  msg  )  [virtual]

The "main loop".

Every message that is received or send is handled by this method

Reimplemented from BaseApp.

Definition at line 509 of file XmlRpcInterface.cc.

00510 {
00511     // Packet from the application...
00512     if (msg==packetNotification) {
00513         EV << "[XmlRpcInterface::handleMessage() @ " << overlay->getThisNode().ip
00514         << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00515         << "    Message from application. Queue length = " << packetBuffer.size()
00516         << endl;
00517         while (packetBuffer.size() > 0) {
00518             // get packet from buffer and parse it
00519             RealtimeScheduler::PacketBufferEntry packet =
00520                     *(packetBuffer.begin());
00521             packetBuffer.pop_front();
00522             curAppFd = packet.fd;
00523 
00524             switch (packet.func) {
00525 
00526             case RealtimeScheduler::PacketBufferEntry::PACKET_DATA:
00527 
00528                 if (state.count(curAppFd) == 0) {
00529                     throw cRuntimeError("XmlRpcInterface::handleMessage(): "
00530                                             "Received packet "
00531                                             "from unknown socket!");
00532                 }
00533 
00534                 handleRealworldPacket(packet.data, packet.length);
00535                 break;
00536 
00537             case RealtimeScheduler::PacketBufferEntry::PACKET_FD_NEW:
00538 
00539                 if (state.count(curAppFd)) {
00540                     throw cRuntimeError("XmlRpcInterface::handleMessage(): "
00541                                             "Connection state table corrupt!");
00542                 }
00543 
00544                 resetConnectionState();
00545 
00546                 if (packet.addr != NULL) {
00547                     if (((sockaddr_in*)packet.addr)->sin_addr.s_addr
00548                             == inet_addr("127.0.0.1")) {
00549                         state[curAppFd].localhost = true;
00550                     }
00551                     delete packet.addr;
00552                     packet.addr = NULL;
00553                 }
00554 
00555                 break;
00556 
00557             case RealtimeScheduler::PacketBufferEntry::PACKET_FD_CLOSE:
00558                 if (state.count(curAppFd) == 0) {
00559                     throw cRuntimeError("XmlRpcInterface::handleMessage(): "
00560                                             "Trying to close unknown "
00561                                             "connection!");
00562                 }
00563 
00564                 resetConnectionState();
00565                 state.erase(curAppFd);
00566             }
00567 
00568             if (packet.data) {
00569                 delete[] packet.data;
00570             }
00571         }
00572     } else if (msg->isSelfMessage()) {
00573         // process rpc self-messages
00574         BaseRpcMessage* rpcMessage = dynamic_cast<BaseRpcMessage*>(msg);
00575         if (rpcMessage!=NULL) {
00576             internalHandleRpcMessage(rpcMessage);
00577             return;
00578         }
00579 
00580         delete msg;
00581     } else {
00582         // RPCs
00583         BaseRpcMessage* rpcMessage = dynamic_cast<BaseRpcMessage*>(msg);
00584         if (rpcMessage!=NULL) {
00585             internalHandleRpcMessage(rpcMessage);
00586             return;
00587         }
00588         // common API
00589         CommonAPIMessage* commonAPIMsg = dynamic_cast<CommonAPIMessage*>(msg);
00590         if (commonAPIMsg != NULL)
00591             handleCommonAPIPacket(commonAPIMsg);
00592 
00593         delete msg;
00594     }
00595 }

void XmlRpcInterface::handleRpcTimeout ( BaseCallMessage *  msg,
const TransportAddress &  dest,
cPolymorphic *  context,
int  rpcId,
const OverlayKey &  destKey 
) [virtual]

This method is called if an RPC timeout has been reached.

Parameters:
msg The original RPC message.
dest The destination node
context Pointer to an optional state object. The object has to be handled/deleted by the handleRpcResponse() code
rpcId The RPC id.
destKey the destination OverlayKey

Reimplemented from RpcListener.

Definition at line 597 of file XmlRpcInterface.cc.

00601 {
00602     curAppFd = rpcId;
00603 
00604     if (state.count(curAppFd) == 0)
00605         return;
00606 
00607     std::cout << "XmlRpcInterface(): XML-RPC failed!" << endl;
00608     state[curAppFd]._response = generateFaultResponse("XML-RPC timeout", 22);
00609     state[curAppFd]._connectionState = WRITE_RESPONSE;
00610     if (!writeResponse() ) {
00611         closeConnection();
00612     }
00613 }

void XmlRpcInterface::localLookup ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 244 of file XmlRpcInterface.cc.

00245 {
00246     if ((params.size() != 3)
00247             || (params[0].getType() != XmlRpcValue::TypeBase64)
00248             || (params[1].getType() != XmlRpcValue::TypeInt)
00249             || (params[2].getType() != XmlRpcValue::TypeBoolean))
00250         throw XmlRpcException("local_lookup(base64 key, int num, "
00251                 "boolean safe): Invalid argument type");
00252 
00253     BinaryValue keyString = (const XmlRpcValue::BinaryData&)params[0];
00254 
00255     NodeVector* nextHops = NULL;
00256 
00257     if (keyString.size() > 0) {
00258         nextHops = overlay->local_lookup(OverlayKey::sha1(keyString),
00259                                                      params[1], params[2]);
00260     } else {
00261         nextHops = overlay->local_lookup(overlay->getThisNode().key,
00262                                                      params[1], params[2]);
00263     }
00264 
00265     for (uint32_t i=0; i < nextHops->size(); i++) {
00266         result[i][0] = (*nextHops)[i].ip.str();
00267         result[i][1] = (*nextHops)[i].port;
00268         result[i][2] = (*nextHops)[i].key.toString(16);
00269     }
00270 
00271     delete nextHops;
00272 }

void XmlRpcInterface::lookup ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 274 of file XmlRpcInterface.cc.

00275 {
00276     if (((params.size() != 2)
00277             || (params[0].getType() != XmlRpcValue::TypeBase64)
00278             || (params[1].getType() != XmlRpcValue::TypeInt))
00279         && ((params.size() != 3)
00280             || (params[0].getType() != XmlRpcValue::TypeBase64)
00281             || (params[1].getType() != XmlRpcValue::TypeInt)
00282             || (params[2].getType() != XmlRpcValue::TypeInt)))
00283         throw XmlRpcException("lookup(base64 key, int numSiblings(, "
00284                               "int RoutingType)): Invalid argument type");
00285 
00286     if ((int)params[1] > overlay->getMaxNumSiblings())
00287         throw XmlRpcException("lookup(base64 key, int numSiblings(, "
00288                               "int RoutingType)): numSibling to big");
00289 
00290     if (params.size() == 3) {
00291         if (((int)params[2] != DEFAULT_ROUTING) &&
00292             ((int)params[2] != ITERATIVE_ROUTING) &&
00293             ((int)params[2] != EXHAUSTIVE_ITERATIVE_ROUTING) &&
00294             ((int)params[2] != SEMI_RECURSIVE_ROUTING) &&
00295             ((int)params[2] != FULL_RECURSIVE_ROUTING) &&
00296             ((int)params[2] != RECURSIVE_SOURCE_ROUTING)) {
00297 
00298             throw XmlRpcException("lookup(base64 key, int numSiblings(, "
00299                                   "int RoutingType)): invalid routingType");
00300         }
00301     }
00302 
00303     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00304 
00305     LookupCall* lookupCall = new LookupCall();
00306 
00307     BinaryValue keyString = (const XmlRpcValue::BinaryData&)params[0];
00308     lookupCall->setKey(OverlayKey::sha1(keyString));
00309     lookupCall->setNumSiblings(params[1]);
00310 
00311     if (params.size() == 3) {
00312         lookupCall->setRoutingType(params[2]);
00313     }
00314 
00315     sendInternalRpcWithTimeout(OVERLAY_COMP, lookupCall);
00316 }

void XmlRpcInterface::p2pnsRegister ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 197 of file XmlRpcInterface.cc.

00198 {
00199     if ((params.size() != 3) ||
00200             (params[0].getType() != XmlRpcValue::TypeBase64) ||
00201             (params[1].getType() != XmlRpcValue::TypeBase64) ||
00202             (params[2].getType() != XmlRpcValue::TypeInt))
00203         throw XmlRpcException("register(base64 name, base64 address, int ttl): "
00204                               "Invalid argument type");
00205 
00206     if (overlay->getCompModule(TIER2_COMP) == NULL)
00207         throw XmlRpcException("register(base64 name, base64 address, int ttl): "
00208                               "No P2PNS service");
00209 
00210     if (!isPrivileged()) {
00211         throw XmlRpcException("register(base64 name, base64 address, "
00212                               "int ttl): Not allowed");
00213     }
00214 
00215     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00216 
00217     P2pnsRegisterCall* registerCall = new P2pnsRegisterCall();
00218 
00219     registerCall->setP2pName(((const XmlRpcValue::BinaryData&)params[0]));
00220     registerCall->setAddress(((const XmlRpcValue::BinaryData&)params[1]));
00221     registerCall->setTtl(params[2]);
00222 
00223     sendInternalRpcWithTimeout(TIER2_COMP, registerCall);
00224 }

void XmlRpcInterface::p2pnsResolve ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 226 of file XmlRpcInterface.cc.

00227 {
00228     if ((params.size() != 1) ||
00229             (params[0].getType() != XmlRpcValue::TypeBase64))
00230         throw XmlRpcException("resolve(base64 name): Invalid argument type");
00231 
00232     if (overlay->getCompModule(TIER2_COMP) == NULL)
00233         throw XmlRpcException("resolve(base64 name): No P2PNS service");
00234 
00235     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00236 
00237     P2pnsResolveCall* resolveCall = new P2pnsResolveCall();
00238 
00239     resolveCall->setP2pName(((const XmlRpcValue::BinaryData&)params[0]));
00240 
00241     sendInternalRpcWithTimeout(TIER2_COMP, resolveCall);
00242 }

void XmlRpcInterface::put ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 335 of file XmlRpcInterface.cc.

00336 {
00337     if ((params.size() != 4)
00338             || (params[0].getType() != XmlRpcValue::TypeBase64)
00339             || (params[1].getType() != XmlRpcValue::TypeBase64)
00340             || (params[2].getType() != XmlRpcValue::TypeInt)
00341             || (params[3].getType() != XmlRpcValue::TypeString))
00342         throw XmlRpcException("put(base64 key, base64 value, int ttl "
00343                 ", string application): Invalid argument type");
00344 
00345     if (!isPrivileged()) {
00346         throw XmlRpcException("put(base64 key, base64 value, int ttl "
00347                 ", string application): Not allowed");
00348     }
00349 
00350     if (overlay->getCompModule(TIER1_COMP) == NULL)
00351         throw XmlRpcException("put(base64 key, base64 value, int ttl "
00352                 ", string application): No DHT service");
00353 
00354     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00355 
00356     DHTputCAPICall* dhtPutMsg = new DHTputCAPICall();
00357 
00358     BinaryValue keyString = (const XmlRpcValue::BinaryData&)params[0];
00359 
00360     dhtPutMsg->setKey(OverlayKey::sha1(keyString));
00361     dhtPutMsg->setValue(((const XmlRpcValue::BinaryData&)params[1]));
00362     dhtPutMsg->setTtl(params[2]);
00363     dhtPutMsg->setIsModifiable(true);
00364 
00365     sendInternalRpcWithTimeout(TIER1_COMP, dhtPutMsg);
00366 }

void XmlRpcInterface::get ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 368 of file XmlRpcInterface.cc.

00369 {
00370     if ((params.size() != 4)
00371             || (params[0].getType() != XmlRpcValue::TypeBase64)
00372             || (params[1].getType() != XmlRpcValue::TypeInt)
00373             || (params[2].getType() != XmlRpcValue::TypeBase64)
00374             || (params[3].getType() != XmlRpcValue::TypeString))
00375         throw XmlRpcException("get(base64 key, int num, base64 placemark "
00376                 ", string application): Invalid argument type");
00377 
00378     if (overlay->getCompModule(TIER1_COMP) == NULL)
00379         throw XmlRpcException("get(base64 key, int num, base64 placemark "
00380                 ", string application): No DHT service");
00381 
00382     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00383 
00384     DHTgetCAPICall* dhtGetMsg = new DHTgetCAPICall();
00385 
00386     BinaryValue keyString = (const XmlRpcValue::BinaryData&)params[0];
00387     dhtGetMsg->setKey(OverlayKey::sha1(keyString));
00388 
00389     sendInternalRpcWithTimeout(TIER1_COMP, dhtGetMsg);
00390 }

void XmlRpcInterface::dumpDht ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 392 of file XmlRpcInterface.cc.

00393 {
00394     if (params.size() != 1) {
00395         throw XmlRpcException("dump_dht(int dummy): Invalid argument type");
00396     }
00397 
00398     if (!isPrivileged()) {
00399          throw XmlRpcException("dump_dht(int dummy): Not allowed");
00400      }
00401 
00402     if (overlay->getCompModule(TIER1_COMP) == NULL)
00403         throw XmlRpcException("dump_dht(): No DHT service");
00404 
00405     state[curAppFd]._connectionState = EXECUTE_REQUEST;
00406 
00407     DHTdumpCall* call = new DHTdumpCall();
00408 
00409     sendInternalRpcWithTimeout(TIER1_COMP, call);
00410 }

void XmlRpcInterface::joinOverlay ( XmlRpc::XmlRpcValue &  params,
XmlRpc::XmlRpcValue &  result 
)

Definition at line 318 of file XmlRpcInterface.cc.

00319 {
00320     if ((params.size() != 1)
00321             || (params[0].getType() != XmlRpcValue::TypeBase64))
00322         throw XmlRpcException("join(base64 nodeID): Invalid argument type");
00323 
00324     if (!isPrivileged()) {
00325         throw XmlRpcException("join(base64 nodeID): Not allowed");
00326     }
00327 
00328     BinaryValue nodeID = (const XmlRpcValue::BinaryData&)params[0];
00329 
00330     overlay->join(OverlayKey::sha1(nodeID));
00331 
00332     result[0] = 0;
00333 }


Member Data Documentation

unsigned int XmlRpcInterface::mtu [protected]

Definition at line 45 of file XmlRpcInterface.h.

Referenced by initializeApp().

cMessage* XmlRpcInterface::packetNotification [protected]

Definition at line 47 of file XmlRpcInterface.h.

Referenced by handleMessage(), initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 48 of file XmlRpcInterface.h.

Referenced by handleMessage(), and initializeApp().

Definition at line 49 of file XmlRpcInterface.h.

Referenced by closeConnection(), initializeApp(), and writeResponse().

std::map<int, XmlRpcConnectionState> XmlRpcInterface::state [protected]

SOCKET XmlRpcInterface::curAppFd [protected]

bool XmlRpcInterface::limitAccess [protected]

Definition at line 98 of file XmlRpcInterface.h.

Referenced by initializeApp(), and isPrivileged().

Definition at line 100 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 101 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 102 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 103 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 104 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 105 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 106 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().

Definition at line 107 of file XmlRpcInterface.h.

Referenced by initializeApp(), XmlRpcInterface(), and ~XmlRpcInterface().


The documentation for this class was generated from the following files:

Generated on Wed Mar 25 16:32:54 2009 for OverSim by  doxygen 1.5.5