Mike Spadaru / physcom
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rpc.cpp Source File

rpc.cpp

00001 #include "rpc.h"
00002 
00003 using namespace std;
00004 
00005 namespace physcom {
00006 
00007 RPC::RPC(const char *name) {
00008     if (name != NULL) {
00009         _name = new char[strlen(name) + 1];
00010         strcpy(_name, name);
00011     } else {
00012         _name = new char[12];
00013         sprintf(_name, "obj%p", this);
00014     }
00015     // put this object at head of the list
00016     _next = _head;
00017     _head = this;
00018 }
00019 
00020 RPC::~RPC() {
00021     // remove this object from the list
00022     if (_head == this) { // first in the list, so just drop me
00023         _head = _next;
00024     } else {            // find the object before me, then drop me
00025         RPC* p = _head;
00026         while (p->_next != this) {
00027             p = p->_next;
00028         }
00029         p->_next = _next;
00030     }
00031 }
00032 
00033 RPC *RPC::lookup(const char *name) {
00034     size_t len = strlen(name);
00035     for (RPC *p = _head; p != NULL; p = p->_next) {
00036         /* Check that p->_name matches name and is the correct length */
00037         if (strncmp(p->_name, name, len) == 0 && (strlen(p->_name) == len)) {
00038             return p;
00039         }
00040     }
00041     return NULL;
00042 }
00043 
00044 RPC *RPC::_head = NULL;
00045 
00046 bool RPC::call(char *input, char *output) {
00047     // A request has the syntax: "/Object/Method arg1 arg2 ... "
00048     const char* ARGUMENT_DELIMITER = " ";
00049     const char* REQUEST_DELIMITER = "/";
00050     
00051     if (input == NULL) {
00052         sprintf(output, "ERROR: Invalid Request");
00053         return false;
00054     }
00055     char *request = strtok (input, ARGUMENT_DELIMITER);
00056     char *arguments = strtok(NULL, "");
00057     char *object = strtok (request, REQUEST_DELIMITER);
00058     char *method = strtok (NULL, "");
00059          
00060     if (object == NULL) {
00061         sprintf(output, "ERROR: Invalid Oject");
00062         return false;
00063     }
00064 
00065     if (method == NULL) {
00066         sprintf(output, "ERROR: Invalid Method");
00067         return false;
00068     }
00069     
00070     /* First try matching an instance */
00071     RPC *p = lookup(object);
00072     
00073     if (p != NULL) {
00074         if (strcmp(method, "run") != 0) {
00075             sprintf(output, "ERROR: Could not find method %s. Try executing method 'run'", 
00076                 method);
00077             return false;    
00078         }
00079     } else {
00080         sprintf(output, "ERROR: Could not find object %s.", object);
00081         return false;    
00082     }  
00083 
00084     p->run(arguments, output);
00085     return true;
00086 }
00087 
00088 
00089 void RPC::run(char *input, char *output) {
00090         sprintf(output, "ERROR: Unimplemented");
00091 }
00092 
00093 } // namespace physcom