Webserver for peach-board

Committer:
thudt90
Date:
Wed Mar 04 02:16:31 2015 +0000
Revision:
0:6ec14f880a00
add webserver

Who changed what in which revision?

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