student test for professor

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Fork of HTTP-Server by Francois Berder

Committer:
feb11
Date:
Thu Jul 18 09:39:35 2013 +0000
Revision:
9:a9bf63017854
Parent:
6:d03e189ebbfe
Child:
10:8b4c3d605bf0
Improved error message

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "Formatter.h"
feb11 0:9e4bcb10b3e3 2 #include "mbed.h"
feb11 0:9e4bcb10b3e3 3 #include "RPCObjectManager.h"
feb11 0:9e4bcb10b3e3 4 #include "EthernetInterface.h"
feb11 0:9e4bcb10b3e3 5
feb11 0:9e4bcb10b3e3 6 const char *SIMPLE_HTML_CODE = "\
feb11 0:9e4bcb10b3e3 7 <!DOCTYPE html>\
feb11 0:9e4bcb10b3e3 8 <html>\
feb11 4:624527ebc0fa 9 <head>\
feb11 4:624527ebc0fa 10 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 11 <title>TCP Server</title>\
feb11 4:624527ebc0fa 12 </head>\
feb11 4:624527ebc0fa 13 <body>";
feb11 0:9e4bcb10b3e3 14
feb11 0:9e4bcb10b3e3 15
feb11 0:9e4bcb10b3e3 16 const char* INTERACTIVE_HTML_CODE_1 = "\
feb11 4:624527ebc0fa 17 <!DOCTYPE html> \
feb11 0:9e4bcb10b3e3 18 <html>\
feb11 4:624527ebc0fa 19 <head>\
feb11 4:624527ebc0fa 20 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
feb11 4:624527ebc0fa 21 <title>TCP Server</title>\
feb11 4:624527ebc0fa 22 <script type=\"text/javascript\">\
feb11 4:624527ebc0fa 23 var ip = \"%s\";\
feb11 4:624527ebc0fa 24 function submitCreateForm()\
feb11 4:624527ebc0fa 25 {\
feb11 4:624527ebc0fa 26 var list = document.getElementById(\"type\");\
feb11 4:624527ebc0fa 27 var type = list.options[list.selectedIndex].value;\
feb11 4:624527ebc0fa 28 var name = document.getElementById(\"name\").value;\
feb11 4:624527ebc0fa 29 var arg = document.getElementById(\"arg\").value;\
feb11 4:624527ebc0fa 30 var url;\
feb11 6:d03e189ebbfe 31 if(arg === \"\") url = \"http://\" + ip + type + \"new?name=\" + name;\
feb11 6:d03e189ebbfe 32 else url = \"http://\" + ip + type + \"new?arg=\" + arg + \"&name=\" + name;\
feb11 4:624527ebc0fa 33 location.href= url;\
feb11 4:624527ebc0fa 34 }\
feb11 4:624527ebc0fa 35 function submitCallFuncForm()\
feb11 4:624527ebc0fa 36 {\
feb11 4:624527ebc0fa 37 var command = document.getElementById(\"command\").value;\
feb11 4:624527ebc0fa 38 var tmp = command.split(\' \');\
feb11 4:624527ebc0fa 39 var url = tmp[0];\
feb11 4:624527ebc0fa 40 if(tmp.length > 1)\
feb11 4:624527ebc0fa 41 url += \"?\";\
feb11 4:624527ebc0fa 42 for(var i = 1; i < tmp.length; ++i)\
feb11 4:624527ebc0fa 43 {\
feb11 4:624527ebc0fa 44 url += \"arg\" + i + \"=\" + tmp[i];\
feb11 4:624527ebc0fa 45 if(i+1 < tmp.length)\
feb11 4:624527ebc0fa 46 url += \"&\";\
feb11 4:624527ebc0fa 47 }\
feb11 4:624527ebc0fa 48 location.href = url;\
feb11 4:624527ebc0fa 49 }\
feb11 4:624527ebc0fa 50 </script>\
feb11 4:624527ebc0fa 51 </head> \
feb11 0:9e4bcb10b3e3 52 <body>";
feb11 0:9e4bcb10b3e3 53
feb11 0:9e4bcb10b3e3 54 const char* INTERACTIVE_HTML_CODE_2 = "<h3>Create Object :</h3>\
feb11 9:a9bf63017854 55 <form>\
feb11 4:624527ebc0fa 56 Type: <select id=\"type\">\
feb11 4:624527ebc0fa 57 <option value=\"/DigitalOut/\">DigitalOut</option>\
feb11 4:624527ebc0fa 58 <option value=\"/DigitalIn/\">DigitalIn</option>\
feb11 4:624527ebc0fa 59 <option value=\"/DigitalInOut/\">DigitalInOut</option>\
feb11 4:624527ebc0fa 60 <option value=\"/PwmOut/\">PwmOut</option>\
feb11 4:624527ebc0fa 61 <option value=\"/Timer/\">Timer</option>\
feb11 4:624527ebc0fa 62 </select><br>\
feb11 4:624527ebc0fa 63 name: <input type=\"text\" id=\"name\"><br>\
feb11 4:624527ebc0fa 64 arg(optional): <input type=\"text\" id=\"arg\">\
feb11 4:624527ebc0fa 65 <p><input type=\"button\" value=\"Create\" onclick=\"javascript:submitCreateForm();\"></p>\
feb11 4:624527ebc0fa 66 </form> \
feb11 4:624527ebc0fa 67 \
feb11 4:624527ebc0fa 68 <h3>Call a function :</h3>\
feb11 4:624527ebc0fa 69 <p>Enter an RPC command.</p>\
feb11 9:a9bf63017854 70 <form>\
feb11 4:624527ebc0fa 71 Command: <input type= \"text\" id=\"command\"><br>\
feb11 9:a9bf63017854 72 <p><input type=\"button\" value=\"Send\" onclick=\"javascript:submitCallFuncForm();\"></p><br>\
feb11 4:624527ebc0fa 73 </form>\
feb11 4:624527ebc0fa 74 </body> \
feb11 0:9e4bcb10b3e3 75 </html>";
feb11 0:9e4bcb10b3e3 76
feb11 0:9e4bcb10b3e3 77 static char chunk[1024];
feb11 0:9e4bcb10b3e3 78
feb11 0:9e4bcb10b3e3 79 Formatter::Formatter(int nb):
feb11 0:9e4bcb10b3e3 80 currentChunk(0),
feb11 0:9e4bcb10b3e3 81 nbChunk(nb)
feb11 0:9e4bcb10b3e3 82 {
feb11 0:9e4bcb10b3e3 83 }
feb11 0:9e4bcb10b3e3 84
feb11 0:9e4bcb10b3e3 85 char* Formatter::get_page(char *reply)
feb11 0:9e4bcb10b3e3 86 {
feb11 0:9e4bcb10b3e3 87 chunk[0] = '\0';
feb11 0:9e4bcb10b3e3 88
feb11 0:9e4bcb10b3e3 89 if(currentChunk < nbChunk)
feb11 0:9e4bcb10b3e3 90 {
feb11 0:9e4bcb10b3e3 91 get_chunk(currentChunk, reply);
feb11 0:9e4bcb10b3e3 92 currentChunk++;
feb11 0:9e4bcb10b3e3 93 }
feb11 0:9e4bcb10b3e3 94 else
feb11 0:9e4bcb10b3e3 95 currentChunk = 0;
feb11 0:9e4bcb10b3e3 96
feb11 0:9e4bcb10b3e3 97 return chunk;
feb11 0:9e4bcb10b3e3 98 }
feb11 0:9e4bcb10b3e3 99
feb11 0:9e4bcb10b3e3 100 void Formatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 101 {
feb11 0:9e4bcb10b3e3 102 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 103 }
feb11 0:9e4bcb10b3e3 104
feb11 0:9e4bcb10b3e3 105 SimpleHTMLFormatter::SimpleHTMLFormatter():
feb11 0:9e4bcb10b3e3 106 Formatter()
feb11 0:9e4bcb10b3e3 107 {
feb11 0:9e4bcb10b3e3 108 }
feb11 0:9e4bcb10b3e3 109
feb11 0:9e4bcb10b3e3 110 void SimpleHTMLFormatter::get_chunk(const int c, char* reply)
feb11 0:9e4bcb10b3e3 111 {
feb11 0:9e4bcb10b3e3 112 strcat(chunk, SIMPLE_HTML_CODE);
feb11 0:9e4bcb10b3e3 113
feb11 0:9e4bcb10b3e3 114 if(reply != NULL && strlen(reply) != 0)
feb11 0:9e4bcb10b3e3 115 {
feb11 0:9e4bcb10b3e3 116 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 117 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 118 }
feb11 0:9e4bcb10b3e3 119
feb11 0:9e4bcb10b3e3 120 if(!RPCObjectManager::instance().is_empty())
feb11 0:9e4bcb10b3e3 121 {
feb11 0:9e4bcb10b3e3 122 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 123 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
feb11 0:9e4bcb10b3e3 124 itor != RPCObjectManager::instance().end();
feb11 0:9e4bcb10b3e3 125 ++itor)
feb11 0:9e4bcb10b3e3 126 {
feb11 0:9e4bcb10b3e3 127 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 128 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 129 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 130 }
feb11 0:9e4bcb10b3e3 131 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 132 }
feb11 0:9e4bcb10b3e3 133
feb11 0:9e4bcb10b3e3 134 strcat(chunk, "</body></html>");
feb11 0:9e4bcb10b3e3 135 }
feb11 0:9e4bcb10b3e3 136
feb11 0:9e4bcb10b3e3 137 InteractiveHTMLFormatter::InteractiveHTMLFormatter():
feb11 0:9e4bcb10b3e3 138 Formatter(3)
feb11 0:9e4bcb10b3e3 139 {
feb11 0:9e4bcb10b3e3 140 }
feb11 0:9e4bcb10b3e3 141
feb11 0:9e4bcb10b3e3 142 void InteractiveHTMLFormatter::get_chunk(const int c, char *reply)
feb11 0:9e4bcb10b3e3 143 {
feb11 0:9e4bcb10b3e3 144 if(c == 0)
feb11 0:9e4bcb10b3e3 145 sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress());
feb11 4:624527ebc0fa 146
feb11 0:9e4bcb10b3e3 147 else if(c == 1)
feb11 0:9e4bcb10b3e3 148 {
feb11 0:9e4bcb10b3e3 149 if(reply != NULL && strlen(reply) != 0)
feb11 0:9e4bcb10b3e3 150 {
feb11 0:9e4bcb10b3e3 151 strcat(chunk, "RPC reply : ");
feb11 0:9e4bcb10b3e3 152 strcat(chunk, reply);
feb11 0:9e4bcb10b3e3 153 }
feb11 0:9e4bcb10b3e3 154 if(!RPCObjectManager::instance().is_empty())
feb11 0:9e4bcb10b3e3 155 {
feb11 0:9e4bcb10b3e3 156 strcat(chunk, "<p>Objects created :</p>");
feb11 0:9e4bcb10b3e3 157
feb11 0:9e4bcb10b3e3 158 strcat(chunk, "<ul>");
feb11 0:9e4bcb10b3e3 159 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
feb11 0:9e4bcb10b3e3 160 itor != RPCObjectManager::instance().end();
feb11 0:9e4bcb10b3e3 161 ++itor)
feb11 0:9e4bcb10b3e3 162 {
feb11 0:9e4bcb10b3e3 163 strcat(chunk, "<li>");
feb11 0:9e4bcb10b3e3 164 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 165 strcat(chunk, " (<a href=\"http://");
feb11 0:9e4bcb10b3e3 166 strcat(chunk, EthernetInterface::getIPAddress());
feb11 0:9e4bcb10b3e3 167 strcat(chunk, "/");
feb11 0:9e4bcb10b3e3 168 strcat(chunk, *itor);
feb11 0:9e4bcb10b3e3 169 strcat(chunk, "/delete\">delete</a>)");
feb11 0:9e4bcb10b3e3 170 strcat(chunk, "</li>");
feb11 0:9e4bcb10b3e3 171 }
feb11 0:9e4bcb10b3e3 172 strcat(chunk, "</ul>");
feb11 0:9e4bcb10b3e3 173 }
feb11 0:9e4bcb10b3e3 174 strcat(chunk, " ");
feb11 0:9e4bcb10b3e3 175 }
feb11 0:9e4bcb10b3e3 176 else if(c == 2)
feb11 0:9e4bcb10b3e3 177 strcat(chunk, INTERACTIVE_HTML_CODE_2);
feb11 0:9e4bcb10b3e3 178 }
feb11 0:9e4bcb10b3e3 179
feb11 0:9e4bcb10b3e3 180