• Aucun résultat trouvé

Relaying Messages to and from the Netduino

Dans le document Getting Started with the Internet of Things (Page 101-106)

Isn’t there supposed to be a problem with making a device a web server, as mentioned in Part III? How can we sidestep the problems caused by firewalls, network address translations, and the shortage of IPv4 Internet addresses?

The HelloWeb program works thanks to a relay between the Netduino Plus and the client who wants to connect to it over the Internet. The client sends its request not directly to the device, but instead to this relay; from there, the request is forwarded to the device. The response comes back the same way, indirectly, via the relay (see Figure 10-2).

86 Getting Started with the Internet of Things Figure 10-2. Relay between client and device (application view)

You may wonder how that setup solves any problem. After all, the device (your Netduino Plus) is still buried behind a NAT and firewall, and it there-fore has no unique public Internet address to which requests could be sent.

A client can obviously send requests to the relay, but how can the relay forward it to the device if the device cannot even be addressed?

The relay solves this problem because it allows us to cheat: from the application’s point of view, the device indeed receives requests that come from somewhere on the Web. Under the hood, however, the device is actually an HTTP client that registers itself at the relay and keeps an open TCP/IP connection to the relay. When the relay receives a request from a client, it forwards it to the device over the open connection, receives the device’s response, and sends it back to the client. The client therefore never needs to “see” the device directly; it uses a URI pointing to the relay instead.

Since requests and responses travel “the wrong way” between device and relay, this approach is sometimes called reverse HTTP. There are several ways in which a reverse HTTP protocol can be defined—the important point is that it is possible to create a relay without running afoul of the rules of the HTTP protocol.

10/Hello Web 87

Yaler

My company, Oberon microsystems, developed a reverse HTTP relay called Yaler (relay spelled backwards) because several of our customers needed a robust and scalable relay for their devices. The source code for Yaler is available at http://yaler.org/. If you have a Windows or Linux server with a public Internet address, you can set up your own relay by downloading and running Yaler.

For the time being, a hosted Yaler instance is set up specifically for read-ers of Getting Started with the Internet of Things at http://try.yaler.net/. It is free for personal and educational use, and is without service or uptime guarantees. In the future, yaler.net might become a commercial, hosted service. It requires an authorization key similar to a Pachube API key.

Please see http://www.gsiot.info/yaler for up-to-date information on how to get your secret key, as well as a relay domain for your device (this is a unique identifier for your device, similar to a Pachube feed ID).

HelloWeb

Example 10-1 shows HelloWeb, our first web server program using a relay.

Note that the relay domain and secret key used below are just examples that won’t work for you, so you need to provide your own values. Alternatively, if you only want to try out the server within your home network, you can just omit the RelayDomain and RelaySecretKey lines completely (or comment out by prefixing them with //), thereby switching off the relay mechanism.

Example 10-1. HelloWeb

using Gsiot.Server;

public class HelloWeb {

public static void Main() {

var webServer = new HttpServer {

RelayDomain = "gsiot-FFMQ-TTD5", RelaySecretKey =

"o5fIIZS5tpD2A4Zp87CoKNUsSpIEJZrV5rNjpg89", RequestRouting =

88 Getting Started with the Internet of Things {

{

"GET /hello", context =>

{ context.SetResponse("Hello Web", "text/plain"); }

} } };

webServer.Run();

} }

To build and run the program, perform the following steps:

1. Make sure that your Netduino Plus is connected to your Ethernet router and that it is correctly configured for network access. If the Pachube client examples from Part II work, you are all set.

2. If you haven’t done so yet, download the Visual Studio project Gsiot.Server from http://www.gsiot.info/download/, unzip it, and put it into the Visual Studio 2010\Projects\ directory.

3. Create a new Visual Studio project (using the Netduino Plus template) and name it HelloWeb. Replace the contents of Program.cs with the code from Example 10-1.

4. Obtain your own relay domain and secret relay key by following the instructions on http://www.gsiot.info/yaler.

5. Assign your device’s relay domain to variable RelayDomain. 6. Assign your secret relay key to variable RelaySecretKey.

7. Right-click on References in the Solution Explorer. Select Add➝

New Reference…. In the Add Reference dialog box, click on the Browse tab. In the directory hierarchy, go up two levels to the Project directory. In the Gsiot.Server directory, open the Gsiot.Server subdirectory (yes, the same name again). In this directory, open the

10/Hello Web 89 bin subdirectory. From there, open the Release subdirectory. In this directory, select the Gsiot.Server.dll file. Click the OK button. You have now added the assembly Projects\Gsiot.Server\Gsiot.Server\bin\

Release\Gsiot.Server.dll.

8. Next, build the project and deploy it to your Netduino Plus, as described in the section “Deploying to the Device” in Chapter 1.

Viewing the Results

On the debug console, a typical output of HelloWeb may look like this:

DHCP enabled: True

MAC address: 3C­8A­4A­00­00­07 Device address: 192.168.5.100 Gateway address: 192.168.5.11 Primary DNS address: 192.168.5.11

Base URI: http://try.yaler.net/gsiot-FFMQ-TTD5/

Open your favorite web browser and enter the above base URI for access-ing your Netduino Plus. When you send a request with a URI that starts with this base URI, it will be sent via the relay to your device.

When you enter the URI in your program, be sure to change gsiot-FFMQ-TTD5 to your relay domain.

Something like the following output should be printed to the debug console:

memory available: 11424

GET /gsiot-FFMQ-TTD5/hello ­> 200

When you look at your browser window, it should show a web page with the string:

Hello Web

Congratulations! Your first server program is up and running, accessible from any corner of the earth!

90 Getting Started with the Internet of Things

Dans le document Getting Started with the Internet of Things (Page 101-106)

Documents relatifs