Server that executes RPC commands through HTTP.

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

You are viewing an older revision! See the latest version

Homepage

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.

Formatter may split reply

The formatter might break the reply into several chunks in order to avoid allocating a very large object.

Examples

Using a browser

It's possible to call functions from a browser using the simple HTML formatter. To put it simply, you just have to write the right URL and press enter. For instance, let's assume that the mbed ip address is 10.2.200.116 and a DigitalOut led1 has been created before, then by writing http://10.2.200.116/led1/write/?arg=0, this will switch off led1. Now, if you are using the InteractiveHTMLFormatter, you can write any RPC command (written as a normal RPC command) in the text line for "Call a function" then press the Send button. Some javascript will encode the RPC command for you.

Let's create a new object DigitalOut called led to control LED2


http://s24.postimg.org/m32h5af51/image.png
Then, let's switch on this led


http://s18.postimg.org/ahu0gdjp5/image.png

Using python

This program switches on led2.

Sending RPC commands over HTTP with Python

import socket

SERVER_ADDRESS = '10.2.200.140'
ECHO_PORT = 80


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((SERVER_ADDRESS, ECHO_PORT))

request = "PUT /DigitalOut/new?arg=LED2&name=led2 HTTP/1.1\r\n"
s.send(request)
response = s.recv(1024)
print response 


request = "GET /led2/write?val=1 HTTP/1.1\r\n"
s.send(request)
response = s.recv(1024)
print response 

s.close()

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

Receiving several packets

This code does not work if the server is using the InteractiveHTMLFormatter because the reply will consist of several packets and so the response will contain only a portion of the entire reply. Moreover, the server may wait indefinitely if you don't receive everything.


All wikipages