MurataTypeYD_RPC_Sample fixed version for 050314

Dependencies:   PowerControl SNICInterface_mod2 mbed-rtos mbed

Fork of HTTPClient_WiFi_HelloWorld by KDDI Fx0 hackathon

Committer:
komoritan
Date:
Thu Mar 12 12:27:31 2015 +0000
Revision:
6:6c49fdc29825
Fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 6:6c49fdc29825 1 #include "mbed.h"
komoritan 6:6c49fdc29825 2 #include "RequestHandler.h"
komoritan 6:6c49fdc29825 3 #include "RPCObject.h"
komoritan 6:6c49fdc29825 4 #include "HTTPServer.h"
komoritan 6:6c49fdc29825 5
komoritan 6:6c49fdc29825 6
komoritan 6:6c49fdc29825 7 int GetRequestHandler::handle(RPCObject& cmd, char* reply)
komoritan 6:6c49fdc29825 8 {
komoritan 6:6c49fdc29825 9 int value;
komoritan 6:6c49fdc29825 10 std::map<PinName, RPCClass*>::iterator itor;
komoritan 6:6c49fdc29825 11
komoritan 6:6c49fdc29825 12 printf("handling GET request.\r\n");
komoritan 6:6c49fdc29825 13 itor = cmd.pinObjects.find(cmd.get_pin_name());
komoritan 6:6c49fdc29825 14 if(itor == cmd.pinObjects.end()){
komoritan 6:6c49fdc29825 15 printf("The pin is not created.\r\n");
komoritan 6:6c49fdc29825 16 return HTTP_404_NOTFOUND;
komoritan 6:6c49fdc29825 17 }
komoritan 6:6c49fdc29825 18 value = itor->second->read();
komoritan 6:6c49fdc29825 19
komoritan 6:6c49fdc29825 20 reply[0] = '0' + value;
komoritan 6:6c49fdc29825 21 reply[1] = '\0';
komoritan 6:6c49fdc29825 22
komoritan 6:6c49fdc29825 23 return HTTP_200_OK;
komoritan 6:6c49fdc29825 24 }
komoritan 6:6c49fdc29825 25
komoritan 6:6c49fdc29825 26
komoritan 6:6c49fdc29825 27 int PostRequestHandler::handle(RPCObject& cmd, char* reply)
komoritan 6:6c49fdc29825 28 {
komoritan 6:6c49fdc29825 29 int value = cmd.get_value();
komoritan 6:6c49fdc29825 30 std::map<PinName, RPCClass*>::iterator itor;
komoritan 6:6c49fdc29825 31
komoritan 6:6c49fdc29825 32 printf("handling POST request.\r\n");
komoritan 6:6c49fdc29825 33 switch(value){
komoritan 6:6c49fdc29825 34 case 0:
komoritan 6:6c49fdc29825 35 case 1:
komoritan 6:6c49fdc29825 36 //update
komoritan 6:6c49fdc29825 37 printf("now updating the object to %d.\r\n", value);
komoritan 6:6c49fdc29825 38 itor = cmd.pinObjects.find(cmd.get_pin_name());
komoritan 6:6c49fdc29825 39 if(itor == cmd.pinObjects.end()){
komoritan 6:6c49fdc29825 40 printf("The pin is not created.\r\n");
komoritan 6:6c49fdc29825 41 return HTTP_404_NOTFOUND;
komoritan 6:6c49fdc29825 42 }
komoritan 6:6c49fdc29825 43 itor->second->write(value);
komoritan 6:6c49fdc29825 44 break;
komoritan 6:6c49fdc29825 45 case -1:
komoritan 6:6c49fdc29825 46 //create
komoritan 6:6c49fdc29825 47 printf("now createing the object.\r\n");
komoritan 6:6c49fdc29825 48 if(!cmd.create_pin_object(reply)){
komoritan 6:6c49fdc29825 49 return -1;
komoritan 6:6c49fdc29825 50 }
komoritan 6:6c49fdc29825 51 break;
komoritan 6:6c49fdc29825 52 case -2:
komoritan 6:6c49fdc29825 53 // delete
komoritan 6:6c49fdc29825 54 itor = cmd.pinObjects.find(cmd.get_pin_name());
komoritan 6:6c49fdc29825 55 if(itor == cmd.pinObjects.end()){
komoritan 6:6c49fdc29825 56 printf("The pin is not created.\r\n");
komoritan 6:6c49fdc29825 57 return HTTP_404_NOTFOUND;
komoritan 6:6c49fdc29825 58 }
komoritan 6:6c49fdc29825 59 delete itor->second;
komoritan 6:6c49fdc29825 60 cmd.pinObjects.erase(cmd.pinObjects.find(cmd.get_pin_name()));
komoritan 6:6c49fdc29825 61 break;
komoritan 6:6c49fdc29825 62 default:
komoritan 6:6c49fdc29825 63 return -1;
komoritan 6:6c49fdc29825 64 }
komoritan 6:6c49fdc29825 65
komoritan 6:6c49fdc29825 66 return HTTP_200_OK;
komoritan 6:6c49fdc29825 67 }
komoritan 6:6c49fdc29825 68
komoritan 6:6c49fdc29825 69
komoritan 6:6c49fdc29825 70 int DeleteRequestHandler::handle(RPCObject& cmd, char* reply)
komoritan 6:6c49fdc29825 71 {
komoritan 6:6c49fdc29825 72 std::map<PinName, RPCClass*>::iterator itor;
komoritan 6:6c49fdc29825 73
komoritan 6:6c49fdc29825 74 printf("handling DELETE request.\r\n");
komoritan 6:6c49fdc29825 75 itor = cmd.pinObjects.find(cmd.get_pin_name());
komoritan 6:6c49fdc29825 76 if(itor == cmd.pinObjects.end()){
komoritan 6:6c49fdc29825 77 printf("The pin is not created.\r\n");
komoritan 6:6c49fdc29825 78 return HTTP_404_NOTFOUND;
komoritan 6:6c49fdc29825 79 }
komoritan 6:6c49fdc29825 80 delete itor->second;
komoritan 6:6c49fdc29825 81 cmd.pinObjects.erase(cmd.pinObjects.find(cmd.get_pin_name()));
komoritan 6:6c49fdc29825 82
komoritan 6:6c49fdc29825 83 return HTTP_200_OK;
komoritan 6:6c49fdc29825 84 }
komoritan 6:6c49fdc29825 85
komoritan 6:6c49fdc29825 86