What is a TIBCO SmartSockets Project?


A SmartSockets project is a group of RTclients working together with one or more RTservers to achieve the goals of a specific system. Within a project, RTclients and RTservers can communicate with other RTclients and RTservers on the same machine or over the network. However, RTclients in different projects cannot send messages to each other.

An RTclient can belong to only one project at a time. An RTserver process does not belong to any specific project, but it can provide message routing services for one or more projects simultaneously. A project can be thought of as a firewall that prevents messages from being dispatched outside the specified RTclient group.

A project is designated by a name, which must be an identifier (often the application’s name is used for the project name). The default project name is rtworks. You can change the default project name using the ss.project option. You should set this option to prevent your Java RTclients from becoming part of the default rtworks project; otherwise, unwanted messages may be received. Remember that in the Java library, all of the standard SmartSockets options are prefixed with ss.; in SmartSockets C programs, the equivalent option is simply project.

In the sending and receiving programs you wrote in Lesson 1, the ss.project option was not explicitly set. This resulted in these programs being part of the rtworks default project. You should set this option to build a firewall between your application and other SmartSockets applications. This example shows you how to change the project name:

The files for this lesson are located in the directories:

Windows:
%RTHOME%\java\tutorial\lesson2 
UNIX:
$RTHOME/java/tutorial/lesson2 

Step 1

Modify the sending program

Modify the sending program from the previous lesson to look like this example, or copy the send.java file into your working directory:

//-------------------------------------------------------- 
// send.java 
1 import java.io.*; 
2 import com.smartsockets.*; 
 
3 public class send { 
4   public static void main(String[] argv) { 
      try { 
5       Tut.setOption("ss.project", "smartsockets"); 
6       TipcSrv srv=TipcSvc.getSrv(); 
7       if (!srv.create()) { 
8         Tut.exitFailure("Couldn't connect to RTserver!"); 
        } 
9       TipcMsg msg = TipcSvc.createMsg(TipcMt.INFO); 
10      msg.setDest("/ss/tutorial/lesson2"); 
11      msg.appendStr("Hello World!"); 
12      srv.send(msg); 
13      srv.flush(); 
14      srv.destroy(); 
15    } catch (TipcException e) { 
16      Tut.warning(e); 
      } // catch 
     } // main  
  }  // send 

Let’s examine some of the key lines in your new sending program:

Line 5
Sets the project name to smartsockets. Now only processes that belong to the smartsockets project can communicate with your sending program. Note that all the members of the Tut class are static, so an instance of the Tut class need not (and should not) be created.
Line 7
Explicitly tries to make a connection to RTserver using the TipcSrv create() method. Be sure this method is used within a try block, so that the IOException that is potentially thrown can be handled. Also, check the return value as shown below, because a false result indicates a connection could not be made.
if (!srv.create()) { 
  Tut.exitFailure( 
    "Couldn’t connect to an RTserver!"); 
} 
In this example, the program exits if a connection cannot be made. Another alternative is to try connecting to an RTserver process on another network node.
Line 12
Publishes the INFO message to the /ss/tutorial/lesson2 subject.
Line 14
Explicitly disconnects from RTserver, ensuring the data is flushed and completes our connect-publish-disconnect cycle.

Compile and run the sending and receiving programs, as was done in the previous lesson. (If RTserver is not still running, start it now.)

Step 2

Compile the sending program

Compile the modified send.java program:

$ javac send.java 

Step 3

Start the receiving program first

Start the receiving and sending programs in separate windows, as you did in Lesson 1. First start the receiving program (the same one used in Lesson 1):

$ java receive 

Step 4

Start the sending program

After a few moments, start the sending program:

$ java send 

Step 5

Change the project and subscribe to the correct subject

Notice that the receiving program did not read nor print the message from the sending program. This is because you set the ss.project option in the sending program to smartsockets and have not yet set the ss.project option in the receiving program. The receiving program still belongs to the default rtworks project. RTserver prevents the message sent by the sending program from being delivered to the receiving program, because it is in a separate project. In addition, the receiving program is still subscribing to the /tutorial/lesson2 subject.

To fix these problems, modify receive.java to belong to the same project as send.java and to subscribe to the /tutorial/lesson2 subject.

Step 6

Modify the receiving program

Modify the receiving program from the previous lesson to match this example, or copy the receive.java file (from the lesson2 directory) into your working directory:

//--------------------------------------------------------- 
// Program 2: receive.java 
 
1 import java.io.*; 
2 import com.smartsockets.*; 
 
3 public class receive { 
 
4   public static void main(String[] argv) { 
5     TipcMsg msg = null; 
6     String text = null; 
 
      try { 
7       Tut.setOption("ss.project", "smartsockets"); 
8       TipcSrv srv=TipcSvc.getSrv(); 
9       if (!srv.create()) { 
10         Tut.exitFailure("Couldn't connect to RTserver!"); 
        } 
11      srv.setSubjectSubscribe("/ss/tutorial/lesson2", true); 
12      msg = srv.next(TipcDefs.TIMEOUT_FOREVER); 
 
13      msg.setCurrent(0); 
14      text = msg.nextStr(); 
15    } catch (TipcException e) { 
16    Tut.fatal(e); 
      } // catch 
 
17  System.out.println("Text from INFO message = " + text); 
    } // main 
} // receive 
 

Now you need to compile the modified receiving program, and then you can run it with the sending program you ran earlier.

Step 7

Compile the receiving program

Compile the receiving program:

$ javac receive.java  

Step 8

Start the receiving program first

Start the receiving and sending programs in separate windows as you did earlier in the lesson. Make sure RTserver is running. Start the receiving program:

$ java receive 

Step 9

Start the sending program

After a few moments, start the sending program:

$ java send 

This output is displayed by the receiving program:

Text from INFO message = Hello World!
The modified sending and receiving programs communicate using the /ss/tutorial/lesson2 subject in project smartsockets.
 

TIBCO SmartSockets™ Java Library User’s Guide and Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com