Dear Customers,

The PolySync Support Center is now just for submitting tickets to our support team. All articles and tutorials have been moved to the new PolySync Help Center and the content below will be removed shortly.

Thank you,
The PolySync Team

Hello World with two nodes

If you have not done so already, please look at the “Hello World” tutorial to get a basic understanding of what a Node is and how you can work with it.

In this tutorial, we will extend the functionality to encompass two nodes that are resident on the PolySync bus. One will function as a node that sends messages and the other will listen for them. You will have to be able to initiate two different command-line sessions to see this work.

What you will need

  • The example source code.
  • A computer capable of running PolySync (see the PolySync Installation Guide)
    • The system should be compliant with the System Requirements
    • It should be running Ubuntu Linux version 14.04
    • It should have compliant hardware with at least 4GB of ram
    • It should have all of the run-time dependencies available; these are included in the installation process
  • The system should have PolySync installed
  • The system should have a license
  • Development tools capable of compiling the tutorial (e.g. sudo apt-get install build-essential)

Compiling the Source Code

The default installation directory for PolySync is /usr/local/polysync. Relative paths used should be considered to originate from the default directory.

The tutorial source code is located in the examples/cpp/ directory with two sub-directories called HelloWorldPublisher and HelloWorldSubscriber.

Use the following command sequence to compile the code:

$ git clone https://github.com/PolySync/CPP-Examples
$ cd CPP-Examples/HelloWorldPublisher $ mkdir build $ cd build $ cmake .. $ make
$ cd ../HelloWorldSubscriber
$ mkdir build
$ cd build
$ cmake ..
$ make

What is Happening in this code

This tutorial extends what we learned in the first Hello World example to demonstrate how to get two nodes to communicate. We are using PolySync implicitly in this process as it functions as the backbone for allowing these two nodes to communicate. Each node independently performs what is required for it to get started and connect to the virtual “bus”.

In this instance, instead of a single node, we now have two nodes that can connect to the bus and either send or receive messages. In PolySync terminology this is known as publish and subscribe; PolySync allows nodes to connect and avoid having to create a specific connection to another node in order to communicate. Instead, a node either publishes messages or subscribes to messages of specific types.

For this tutorial, we have two applications that connect to the bus, one is a publisher, HelloWorldPublisher, the other is a subscriber, HelloWorldSubscriber. You will need to have two terminals open in order to perform this tutorial.

Hello World Publisher

In one of the terminals run:

$ polysync-helloworld-publisher-cpp

As with the first tutorial, the publisher is a node that inherits from the Node base class. The publisher overloads the implementation of the okStateEvent function, which during normal operation is called periodically by the PolySync system. In this implementation, the publisher creates a “ByteArrayMessage,” copies the “Hello World” data into the message, and publishes it to the bus. Because, for the sake of this tutorial, we would prefer to limit the frequency that we send messages, the function contains a sleep command at the end of the function.

We would like to point out here, explicitly that the publisher has no specific connection to anything other than the PolySync bus. It could register with the bus to receive messages of this exact type and, in effect, become a subscriber for its own messages.

We note here also, that the the publisher itself does not produce any specific output to the console so the user should not expect to see any output in that terminal.



Hello World Subscriber

In the other the terminal run:

$ polysync-helloworld-subscriber-cpp

The subscriber also inherits from the Node base class. The subscriber, however, overloads different base class functionality. In this instance, the subscriber overloads the initStateEvent function in order to register for the “ps_byte_array_msg” message type. It then overloads the messageEvent to process messages that it receives from the bus. By registering for a specific message type, the PolySync bus will send messages of that type to this function for the node to process. However, in this tutorial, most of the work has already been done in the base classes for processing the incoming message; the function has only to check the type of the message and print it out.

The user should see a stream of “Hello World” messages in the subscriber terminal, coming in at about one per second.

Conclusion

Clearly, there are more sophisticated communication methods to come, but this should give a basic idea of how nodes communicate on the PolySync bus.

Thanks for using PolySync!

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.