各ピンへのread/writeを提供するサーバサンプル

Dependencies:   NySNICInterface mbed-rtos mbed

Fork of RESTServerSample2 by KDDI Fx0 hackathon

Committer:
komoritan
Date:
Thu Mar 12 12:40:48 2015 +0000
Revision:
1:e5d3bd4af9da
Parent:
0:998e2e00df0c
Bug fix - handle_request

Who changed what in which revision?

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