Hello World with the Application Space

Asynchronously sending messages between distributed components is really easy with the Application Space. The following example shows that you don't need more than two or three lines of code on each side!

Server Code

Take a look at the following code exmple to see how the server looks like:
using (var space = new XcoAppSpace("tcp.port=8000"))
   space.Run<string>("echo", msg => Console.WriteLine("Echo: {0}", msg));

First we create a new instance of the XcoAppSpace class, which can host our services and manage incoming and outgoing messages. It uses a connection string tcp.port=8000 which says that is should communicate over TCP sockets and listen for incoming messages on port 8000.
It's getting interesting in the next line: Here the Run() method is called to open a so-called Port at the space. This port is given the name echo, and it is defined that it can received messages of the type string. Additionally, an action is defined that should be executed whenever a message arrives at this port. In other words, this line means: "Whenever a message arrives at the port echo, write this message to the console."
That's it! Let's take a look at the client code now - it's just as simple.

Client Code

The following example shows the client code:
using (var space = new XcoAppSpace("tcp.port=0"))
   var echoPort = space.Connect<string>("myserver:8000/echo");
   echoPort.Post("hello, world!");

You can see that the code is very similar to the server. A new instance of the XcoAppSpace class is created. Again TCP socket communication is configured, but this time no specific port is used (the client doesn't need one since no one directly wants to connect to it). Then, the Connect() method is used to connect to the echo port that has been defined at the server. The only thing that is needed for that is the address of the other AppSpace instance ("myserver:8000"), the name of the port ("echo") and the type of message the port can receive (string). This call returns an instance of type Port<T>, where T is the message type, in this case string. By that, access to the port at the server has been established. This object can now be used to send messages to the server using the Post() method. The call echoPort.Post("hello, world!") will send the given message to the server, where it will in the end be displayed in the console window.

That's all you need to know for sending your first message between two AppSpace instances! And even with this simple example sending and receiving already works completely asynchronous. If multiple messages arrive at the server at the same time, they will be processed in parallel (by default the AppSpace will process as many messages in parallel as you have CPU cores in your machine). On the client side, the Post method works completely asynchronous, meaning that you don't need to wait until the message is really sent, it all happens in the background!

Of course sending messages it by far not all you can do with the AppSpace. Just take a look at the further examples, you won't be disappointed!

Last edited Jul 19, 2010 at 1:49 PM by thomass, version 2


No comments yet.