Server that executes RPC commands through HTTP.

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Import programHTTP-Server

Server that executes RPC commands through HTTP.

Overview

This program is a small HTTP Server that can execute RPC commands sent through HTTP and sends back a reply. This reply can be either a line of text or an HTML page.

HTTP Request

The server will only read the first line of the HTTP header. It does not check that the header is correct and it does not attempt to read the body. Hence, the server is only interested in the type of the request and the path. Instead of requesting a file, the path contains the RPC command.

RPC command encoding

Information

The RPC command must be encoded in a special way because no spaces are allowed in the path.

Thus, the RPC command is encoded in this way :

/<obj name or type>/<func name>?arg1_name=arg1_val&arg2_name=arg2_val

or, if the RPC command does not have any arguments :

/<obj name or type>/<func name>

So, a complete URL might be :

http://10.2.200.116/led2/write?val=1

The name of the arguments does not appear in the final RPC command. So these 3 urls will do exactly the same thing :

http://10.2.200.116/led2/write?val=1
http://10.2.200.116/led2/write?test=1
http://10.2.200.116/led2/write?blabla=1

Also, the order of the arguments are preserved.

Request handlers

To process requests, the server relies on RequestHandler. Each RequestHandler is assigned to a request type. Each type of request is assigned to a certain role :

  • PUT requests to create new objects
  • DELETE requests to delete objects
  • GET requests to call a function of an object

However, there is a RequestHandler that accepts only GET requests but it can create/delete/call a function. This was necessary to provide an interactive web page that allows creation and deletion of objects.

Reply

The reply depends on the formatter. Currently, three formatters are available :

  • The most basic one does not modify the RPC reply. Hence, if you consider sending request from python scripts, this formatter is the most appropriate one.
  • A simple HTML formatter will allow the user to view the RPC reply and a list of RPC objects currently alive from a browser.
  • Finally, a more complex HTML formatter creates an entire web page where the user can create and delete an object as well as calling functions on these objects.

Configure the server

The configuration of the server consists on choosing the formatter and adding one or more request handlers to the server. The main function initializes the server in order to produce HTML code and to receive data only using GET requests. If you want to use a simpler and different version of the server you can change the content of the main function (located in main.cpp) by this code :

main

    RPCType::instance().register_types();    

    EthernetInterface eth;
    eth.init();
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    HTTPServer srv = create_simple_server();

    if(!srv.init(SERVER_PORT))
    {
        printf("Error while initializing the server\n");
        eth.disconnect();
        return -1;
    }

    srv.run();

    return 0;

However, this configuration will not work with the following examples.

Examples

I assume that the server is using the InteractiveHTMLFormatter (which should be the case if you did not make any changes).

Using a browser

Here is a quick guide how to run this program :

  1. Compiles this program and copies it to the mbed
  2. Open TeraTerm (install it if you don't have it), select serial and choose the port named "mbed Serial Port"
  3. Reset your mbed
  4. The IP address should appear in teraterm. In this example, I will use 10.2.200.116.
  5. Open your browser and go to http://10.2.200.116/.
  6. If everything is ok, you should see a webpage.

Now, let's switch on a led. First, we need to create an object to control a led :

Then, let's write an RPC command to switch on led :


Using python

This program creates and switches on led2.

Sending RPC commands over HTTP with Python

import httplib

SERVER_ADDRESS = '10.2.200.38'

h = httplib.HTTPConnection(SERVER_ADDRESS)
h.request("GET", "/DigitalOut/new?arg=LED2&name=led2")
r = h.getresponse()
print r.read()

h.request("GET", "/led2/write?arg=1")
r = h.getresponse()
print r.read()

h.close()

Of course, you might have to change the server address in order to make it work.

Committer:
feb11
Date:
Wed Jul 17 10:15:05 2013 +0000
Revision:
0:9e4bcb10b3e3
Child:
4:624527ebc0fa
Initial import

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