00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <platdep/sockets.h>
00025 #if not defined _WIN32
00026 #include <arpa/inet.h>
00027 #endif
00028
00029 #include <NodeVector.h>
00030 #include "XmlRpcInterface.h"
00031
00032 using namespace XmlRpc;
00033
00034 Define_Module(XmlRpcInterface);
00035
00036
00037 class P2pnsRegister : public XmlRpcServerMethod
00038 {
00039 public:
00040 P2pnsRegister(XmlRpcServer* s) :
00041 XmlRpcServerMethod("register", s)
00042 {
00043 }
00044
00045 void execute(XmlRpcValue& params, XmlRpcValue& result)
00046 {
00047 (dynamic_cast<XmlRpcInterface*>(_server))->p2pnsRegister(params, result);
00048 }
00049
00050 std::string help()
00051 {
00052 return std::string("Register a name with P2PNS");
00053 }
00054 };
00055
00056
00057 class P2pnsResolve : public XmlRpcServerMethod
00058 {
00059 public:
00060 P2pnsResolve(XmlRpcServer* s) :
00061 XmlRpcServerMethod("resolve", s)
00062 {
00063 }
00064
00065 void execute(XmlRpcValue& params, XmlRpcValue& result)
00066 {
00067 (dynamic_cast<XmlRpcInterface*>(_server))->p2pnsResolve(params, result);
00068 }
00069
00070 std::string help()
00071 {
00072 return std::string("Resolve a name with P2PNS");
00073 }
00074 };
00075
00076
00077 class LocalLookup : public XmlRpcServerMethod
00078 {
00079 public:
00080 LocalLookup(XmlRpcServer* s) :
00081 XmlRpcServerMethod("local_lookup", s)
00082 {
00083 }
00084
00085 void execute(XmlRpcValue& params, XmlRpcValue& result)
00086 {
00087 (dynamic_cast<XmlRpcInterface*>(_server))->localLookup(params, result);
00088 }
00089
00090 std::string help()
00091 {
00092 return std::string("Lookup key in local "
00093 "routing table");
00094 }
00095 };
00096
00097
00098 class Lookup : public XmlRpcServerMethod
00099 {
00100 public:
00101 Lookup(XmlRpcServer* s) :
00102 XmlRpcServerMethod("lookup", s)
00103 {
00104 }
00105
00106 void execute(XmlRpcValue& params, XmlRpcValue& result)
00107 {
00108 (dynamic_cast<XmlRpcInterface*>(_server))->lookup(params, result);
00109 }
00110
00111 std::string help()
00112 {
00113 return std::string("Lookup key with KBR layer");
00114 }
00115 };
00116
00117
00118 class Put : public XmlRpcServerMethod
00119 {
00120 public:
00121 Put(XmlRpcServer* s) :
00122 XmlRpcServerMethod("put", s)
00123 {
00124 }
00125
00126 void execute(XmlRpcValue& params, XmlRpcValue& result)
00127 {
00128 (dynamic_cast<XmlRpcInterface*>(_server))->put(params, result);
00129 }
00130
00131 std::string help()
00132 {
00133 return std::string("Store a value in the DHT");
00134 }
00135 };
00136
00137
00138 class Get : public XmlRpcServerMethod
00139 {
00140 public:
00141 Get(XmlRpcServer* s) :
00142 XmlRpcServerMethod("get", s)
00143 {
00144 }
00145
00146 void execute(XmlRpcValue& params, XmlRpcValue& result)
00147 {
00148 (dynamic_cast<XmlRpcInterface*>(_server))->get(params, result);
00149 }
00150
00151 std::string help()
00152 {
00153 return std::string("Get a value from the DHT");
00154 }
00155 };
00156
00157
00158 class DumpDht : public XmlRpcServerMethod
00159 {
00160 public:
00161 DumpDht(XmlRpcServer* s) :
00162 XmlRpcServerMethod("dump_dht", s)
00163 {
00164 }
00165
00166 void execute(XmlRpcValue& params, XmlRpcValue& result)
00167 {
00168 (dynamic_cast<XmlRpcInterface*>(_server))->dumpDht(params, result);
00169 }
00170
00171 std::string help()
00172 {
00173 return std::string("Dump all local records from the DHT");
00174 }
00175 };
00176
00177
00178 class JoinOverlay : public XmlRpcServerMethod
00179 {
00180 public:
00181 JoinOverlay(XmlRpcServer* s) :
00182 XmlRpcServerMethod("join", s)
00183 {
00184 }
00185
00186 void execute(XmlRpcValue& params, XmlRpcValue& result)
00187 {
00188 (dynamic_cast<XmlRpcInterface*>(_server))->joinOverlay(params, result);
00189 }
00190
00191 std::string help()
00192 {
00193 return std::string("Join the overlay with a specific nodeID");
00194 }
00195 };
00196
00197 void XmlRpcInterface::p2pnsRegister(XmlRpcValue& params, XmlRpcValue& result)
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 }
00225
00226 void XmlRpcInterface::p2pnsResolve(XmlRpcValue& params, XmlRpcValue& result)
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 }
00243
00244 void XmlRpcInterface::localLookup(XmlRpcValue& params, XmlRpcValue& result)
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 }
00273
00274 void XmlRpcInterface::lookup(XmlRpcValue& params, XmlRpcValue& result)
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 }
00317
00318 void XmlRpcInterface::joinOverlay(XmlRpcValue& params, XmlRpcValue& result)
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 }
00334
00335 void XmlRpcInterface::put(XmlRpcValue& params, XmlRpcValue& result)
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 }
00367
00368 void XmlRpcInterface::get(XmlRpcValue& params, XmlRpcValue& result)
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 }
00391
00392 void XmlRpcInterface::dumpDht(XmlRpcValue& params, XmlRpcValue& result)
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 }
00411
00412 bool XmlRpcInterface::isPrivileged()
00413 {
00414 if (limitAccess) {
00415 return state[curAppFd].localhost;
00416 } else {
00417 return true;
00418 }
00419 }
00420
00421 void XmlRpcInterface::initializeApp(int stage)
00422 {
00423
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 }
00450
00451 XmlRpcInterface::XmlRpcInterface()
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 }
00464
00465 XmlRpcInterface::~XmlRpcInterface()
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 }
00478
00479 void XmlRpcInterface::resetConnectionState()
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 }
00494
00495 void XmlRpcInterface::closeConnection()
00496 {
00497 scheduler->closeAppSocket(curAppFd);
00498 resetConnectionState();
00499 }
00500
00501 void XmlRpcInterface::sendInternalRpcWithTimeout(CompType destComp,
00502 BaseCallMessage *call)
00503 {
00504 state[curAppFd].pendingRpc = sendInternalRpcCall(destComp, call, NULL,
00505 XMLRPC_TIMEOUT, 0,
00506 curAppFd);
00507 }
00508
00509 void XmlRpcInterface::handleMessage(cMessage *msg)
00510 {
00511
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
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
00574 BaseRpcMessage* rpcMessage = dynamic_cast<BaseRpcMessage*>(msg);
00575 if (rpcMessage!=NULL) {
00576 internalHandleRpcMessage(rpcMessage);
00577 return;
00578 }
00579
00580 delete msg;
00581 } else {
00582
00583 BaseRpcMessage* rpcMessage = dynamic_cast<BaseRpcMessage*>(msg);
00584 if (rpcMessage!=NULL) {
00585 internalHandleRpcMessage(rpcMessage);
00586 return;
00587 }
00588
00589 CommonAPIMessage* commonAPIMsg = dynamic_cast<CommonAPIMessage*>(msg);
00590 if (commonAPIMsg != NULL)
00591 handleCommonAPIPacket(commonAPIMsg);
00592
00593 delete msg;
00594 }
00595 }
00596
00597 void XmlRpcInterface::handleRpcTimeout(BaseCallMessage* msg,
00598 const TransportAddress& dest,
00599 cPolymorphic* context, int rpcId,
00600 const OverlayKey&)
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 }
00614
00615 void XmlRpcInterface::handleRealworldPacket(char *buf, uint32_t length)
00616 {
00617 if (state[curAppFd]._connectionState == READ_HEADER) {
00618 if (!readHeader(buf, length)) {
00619
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 }
00640
00641 void XmlRpcInterface::handleRpcResponse(BaseResponseMessage* msg,
00642 cPolymorphic* context,
00643 int rpcId,
00644 simtime_t rtt)
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 }
00800
00801 void XmlRpcInterface::handleCommonAPIPacket(cMessage *msg)
00802 {
00803 error("DHTXMLRealworldApp::handleCommonAPIPacket(): Unknown Packet!");
00804 }
00805
00806 bool XmlRpcInterface::readHeader(char* buf, uint32_t length)
00807 {
00808
00809 bool eof = false;
00810
00811 state[curAppFd]._header.append(std::string(buf, length));
00812
00813 if (length <= 0) {
00814
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();
00824 char *ep = hp + state[curAppFd]._header.length();
00825 char *bp = 0;
00826 char *lp = 0;
00827 char *kp = 0;
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
00841 if (bp == 0) {
00842
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;
00848 }
00849
00850 return true;
00851 }
00852
00853
00854 if (lp == 0) {
00855 XmlRpcUtil::error("XmlRpcServerConnection::readHeader: No Content-length specified");
00856 return false;
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
00873 state[curAppFd]._request = bp;
00874
00875
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;
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;
00889 }
00890
00891 bool XmlRpcInterface::readRequest(char *buf, uint32_t length)
00892 {
00893
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
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;
00909 }
00910 return true;
00911 }
00912 }
00913
00914
00915 XmlRpcUtil::log(3, "XmlRpcServerConnection::readRequest read %d bytes.",
00916 state[curAppFd]._request.length());
00917
00918
00919 state[curAppFd]._connectionState = WRITE_RESPONSE;
00920
00921 return true;
00922 }
00923
00924 bool XmlRpcInterface::writeResponse()
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
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
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;
00964 }