student test for professor
Dependencies: EthernetInterface mbed-rpc mbed-rtos mbed
Fork of HTTP-Server by
RequestHandler.cpp@11:8d63f2e3d152, 2014-10-14 (annotated)
- Committer:
- pricegc2
- Date:
- Tue Oct 14 19:48:41 2014 +0000
- Revision:
- 11:8d63f2e3d152
- Parent:
- 7:838d7ea07e18
rev 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 0:9e4bcb10b3e3 | 1 | #include "RequestHandler.h" |
feb11 | 0:9e4bcb10b3e3 | 2 | #include "mbed_rpc.h" |
feb11 | 0:9e4bcb10b3e3 | 3 | #include "RPCObjectManager.h" |
feb11 | 0:9e4bcb10b3e3 | 4 | #include "RPCCommand.h" |
feb11 | 0:9e4bcb10b3e3 | 5 | |
feb11 | 7:838d7ea07e18 | 6 | const char* INVALID_CMD = "Invalid RPC command"; |
feb11 | 7:838d7ea07e18 | 7 | const char* DELETE_ERROR = "You must send a DELETE request to remove an object "; |
feb11 | 7:838d7ea07e18 | 8 | const char* CREATE_ERROR = "You must send a PUT request to create an object"; |
feb11 | 7:838d7ea07e18 | 9 | const char* FUNC_CALL_ERROR = "You must send a GET request to call a function"; |
feb11 | 7:838d7ea07e18 | 10 | |
feb11 | 0:9e4bcb10b3e3 | 11 | void GetRequestHandler::handle(const RPCCommand& cmd, char *reply) |
feb11 | 0:9e4bcb10b3e3 | 12 | { |
feb11 | 0:9e4bcb10b3e3 | 13 | switch(cmd.get_type()) |
feb11 | 0:9e4bcb10b3e3 | 14 | { |
feb11 | 0:9e4bcb10b3e3 | 15 | case DELETE: |
feb11 | 7:838d7ea07e18 | 16 | printf("Error: %s\n", DELETE_ERROR); |
feb11 | 7:838d7ea07e18 | 17 | strcat(reply, DELETE_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 18 | break; |
feb11 | 0:9e4bcb10b3e3 | 19 | case FUNCTION_CALL: |
feb11 | 0:9e4bcb10b3e3 | 20 | RPC::call(cmd.get_cmd(), reply); |
feb11 | 0:9e4bcb10b3e3 | 21 | break; |
feb11 | 0:9e4bcb10b3e3 | 22 | case CREATE: |
feb11 | 7:838d7ea07e18 | 23 | printf("Error: %s\n", CREATE_ERROR); |
feb11 | 7:838d7ea07e18 | 24 | strcat(reply, CREATE_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 25 | break; |
feb11 | 0:9e4bcb10b3e3 | 26 | default: |
feb11 | 7:838d7ea07e18 | 27 | printf("Error: %s\n", INVALID_CMD); |
feb11 | 7:838d7ea07e18 | 28 | strcat(reply, INVALID_CMD); |
feb11 | 0:9e4bcb10b3e3 | 29 | break; |
feb11 | 0:9e4bcb10b3e3 | 30 | } |
feb11 | 0:9e4bcb10b3e3 | 31 | } |
feb11 | 0:9e4bcb10b3e3 | 32 | |
feb11 | 0:9e4bcb10b3e3 | 33 | void PutRequestHandler::handle(const RPCCommand& cmd, char *reply) |
feb11 | 0:9e4bcb10b3e3 | 34 | { |
feb11 | 0:9e4bcb10b3e3 | 35 | switch(cmd.get_type()) |
feb11 | 0:9e4bcb10b3e3 | 36 | { |
feb11 | 0:9e4bcb10b3e3 | 37 | case DELETE: |
feb11 | 7:838d7ea07e18 | 38 | printf("Error: %s\n", DELETE_ERROR); |
feb11 | 7:838d7ea07e18 | 39 | strcat(reply, DELETE_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 40 | break; |
feb11 | 0:9e4bcb10b3e3 | 41 | case FUNCTION_CALL: |
feb11 | 7:838d7ea07e18 | 42 | printf("Error: %s\n", FUNC_CALL_ERROR); |
feb11 | 7:838d7ea07e18 | 43 | strcat(reply, FUNC_CALL_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 44 | break; |
feb11 | 0:9e4bcb10b3e3 | 45 | case CREATE: |
feb11 | 0:9e4bcb10b3e3 | 46 | RPC::call(cmd.get_cmd(), reply); |
feb11 | 0:9e4bcb10b3e3 | 47 | if(strlen(reply) > 0) |
feb11 | 0:9e4bcb10b3e3 | 48 | { |
feb11 | 0:9e4bcb10b3e3 | 49 | RPCObjectManager::instance().store_object(reply); |
feb11 | 0:9e4bcb10b3e3 | 50 | strcat(reply, " has been created"); |
feb11 | 0:9e4bcb10b3e3 | 51 | } |
feb11 | 0:9e4bcb10b3e3 | 52 | else |
feb11 | 7:838d7ea07e18 | 53 | { |
feb11 | 7:838d7ea07e18 | 54 | printf("Error while creating object\n"); |
feb11 | 0:9e4bcb10b3e3 | 55 | strcat(reply, "Error while creating object."); |
feb11 | 7:838d7ea07e18 | 56 | } |
feb11 | 0:9e4bcb10b3e3 | 57 | break; |
feb11 | 0:9e4bcb10b3e3 | 58 | default: |
feb11 | 7:838d7ea07e18 | 59 | printf("Error: %s\n", INVALID_CMD); |
feb11 | 7:838d7ea07e18 | 60 | strcat(reply, INVALID_CMD); |
feb11 | 0:9e4bcb10b3e3 | 61 | break; |
feb11 | 0:9e4bcb10b3e3 | 62 | } |
feb11 | 0:9e4bcb10b3e3 | 63 | } |
feb11 | 0:9e4bcb10b3e3 | 64 | |
feb11 | 0:9e4bcb10b3e3 | 65 | void DeleteRequestHandler::handle(const RPCCommand& cmd, char *reply) |
feb11 | 0:9e4bcb10b3e3 | 66 | { |
feb11 | 0:9e4bcb10b3e3 | 67 | switch(cmd.get_type()) |
feb11 | 0:9e4bcb10b3e3 | 68 | { |
feb11 | 0:9e4bcb10b3e3 | 69 | case CREATE: |
feb11 | 7:838d7ea07e18 | 70 | printf("Error: %s\n", CREATE_ERROR); |
feb11 | 7:838d7ea07e18 | 71 | strcat(reply, CREATE_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 72 | break; |
feb11 | 0:9e4bcb10b3e3 | 73 | case FUNCTION_CALL: |
feb11 | 7:838d7ea07e18 | 74 | printf("Error: %s\n", FUNC_CALL_ERROR); |
feb11 | 7:838d7ea07e18 | 75 | strcat(reply, FUNC_CALL_ERROR); |
feb11 | 0:9e4bcb10b3e3 | 76 | break; |
feb11 | 0:9e4bcb10b3e3 | 77 | case DELETE: |
feb11 | 0:9e4bcb10b3e3 | 78 | RPC::call(cmd.get_cmd(), reply); |
feb11 | 0:9e4bcb10b3e3 | 79 | RPCObjectManager::instance().remove_object(cmd.get_obj_name()); |
feb11 | 0:9e4bcb10b3e3 | 80 | strcat(reply, "Deleted object "); |
feb11 | 0:9e4bcb10b3e3 | 81 | strcat(reply, cmd.get_obj_name()); |
feb11 | 0:9e4bcb10b3e3 | 82 | break; |
feb11 | 0:9e4bcb10b3e3 | 83 | default: |
feb11 | 7:838d7ea07e18 | 84 | printf("Error: %s\n", INVALID_CMD); |
feb11 | 7:838d7ea07e18 | 85 | strcat(reply, INVALID_CMD); |
feb11 | 0:9e4bcb10b3e3 | 86 | break; |
feb11 | 0:9e4bcb10b3e3 | 87 | } |
feb11 | 0:9e4bcb10b3e3 | 88 | } |
feb11 | 0:9e4bcb10b3e3 | 89 | |
feb11 | 0:9e4bcb10b3e3 | 90 | void ComplexRequestHandler::handle(const RPCCommand& cmd, char *reply) |
feb11 | 0:9e4bcb10b3e3 | 91 | { |
feb11 | 0:9e4bcb10b3e3 | 92 | switch(cmd.get_type()) |
feb11 | 0:9e4bcb10b3e3 | 93 | { |
feb11 | 0:9e4bcb10b3e3 | 94 | case CREATE : |
feb11 | 0:9e4bcb10b3e3 | 95 | putHandler.handle(cmd, reply); |
feb11 | 0:9e4bcb10b3e3 | 96 | break; |
feb11 | 0:9e4bcb10b3e3 | 97 | case DELETE : |
feb11 | 0:9e4bcb10b3e3 | 98 | deleteHandler.handle(cmd, reply); |
feb11 | 0:9e4bcb10b3e3 | 99 | break; |
feb11 | 0:9e4bcb10b3e3 | 100 | case FUNCTION_CALL : |
feb11 | 0:9e4bcb10b3e3 | 101 | getHandler.handle(cmd, reply); |
feb11 | 0:9e4bcb10b3e3 | 102 | break; |
feb11 | 0:9e4bcb10b3e3 | 103 | default : |
feb11 | 7:838d7ea07e18 | 104 | printf("Error: %s\n", INVALID_CMD); |
feb11 | 7:838d7ea07e18 | 105 | strcat(reply, INVALID_CMD); |
feb11 | 0:9e4bcb10b3e3 | 106 | break; |
feb11 | 0:9e4bcb10b3e3 | 107 | } |
feb11 | 0:9e4bcb10b3e3 | 108 | } |
feb11 | 0:9e4bcb10b3e3 | 109 |