Typically, these three steps are required when constructing a message:
Many different types of fields can be appended to a message’s data. These field types include three sizes of integers, two sizes of real numbers, character strings, and arrays of the scalar field types (such as an array of four-byte integers). These field types are listed in the online documentation for the TipcMsg class and can be recognized by their FT_
prefix. Messages themselves can even be used as fields within other container messages; this allows operations such as large transactions to be represented with a single message. Once a message is constructed, it can be published to other RTclients using the TipcSrv.send method.
To get a better feel for working with the SmartSockets Java API for building and sending messages, you will enhance your sending program from the previous lesson to send a NUMERIC_DATA message. The data part of a NUMERIC_DATA message consists of one or more variable-value pairs, where a variable is a text string and a value is a floating point number.
The files for this lesson are located in the directories:
Copy the send2.java program
Copy the file send2.java into your working directory:
//----------------------------------------------------------
// send2.java -- write a NUMERIC_DATA message
1 import java.io.*; 2 import com.smartsockets.*; 3 public class send2 { 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!"); }// create a message of type NUMERIC_DATA
9 TipcMsg msg = TipcSvc.createMsg(TipcMt.NUMERIC_DATA);// set the destination subject of the message
10 msg.setDest("/ss/tutorial/lesson3");// build a NUMERIC_DATA msg with 3 variable-value pairs,
// set as follows (X, 10.0), (Y, 20.0) and (Z, 30.0)
11 msg.appendStr("X"); 12 msg.appendReal8(10.0); 13 msg.appendStr("Y"); 14 msg.appendReal8(20.0); 15 msg.appendStr("Z"); 16 msg.appendReal8(30.0);// send the message
17 srv.send(msg); 18 srv.flush();// disconnect from RTserver
19 srv.destroy(); } catch (TipcException e) { 20 Tut.warning(e); }// catch
}// main
}// send2
Let’s examine some of the key lines in this program:
Compile the send2.java program
Now compile the send2.java
program.
You now need to modify your receiving program so that it can read and print the contents of the NUMERIC_DATA message you are sending.
Copy the receive2.java program
Copy the file receive2.java
into your working directory. This is an example of the receive2.java
program:
//----------------------------------------------------------------
// receive2.java -- receive a NUMERIC_DATA message
1 import java.io.*; 2 import com.smartsockets.*; 3 public class receive2 { 4 public static void main(String[] argv) { 5 TipcMsg msg = null; 6 TipcSrv srv = null;// set the ss.project
try { 7 Tut.setOption("ss.project", "smartsockets"); 8 srv=TipcSvc.getSrv();// connect to RTserver
9 if (!srv.create()) { 10 Tut.exitFailure("Couldn't connect to RTserver!"); }// if
// subscribe to the appropriate subject
11 srv.setSubjectSubscribe("/ss/tutorial/lesson3", true); 12 msg = srv.next(TipcDefs.TIMEOUT_FOREVER); 13 } catch (TipcException e) { 14 Tut.fatal(e); }// catch
// position the field ptr to the beginning of the message
try { 15 msg.setCurrent(0); 16 } catch (TipcException e) { 17 Tut.fatal(e); }// catch
18 System.out.println("Contents of NUMERIC_DATA message:");// read the data part of the message
try { 19 String var_name; 20 while (true) { 21 var_name = msg.nextStr(); 22 double var_value; 23 var_value = msg.nextReal8(); 24 System.out.println("Var name = " + var_name + ", value = " + var_value); }// while
}// catch end-of-message-data exception, do nothing.
25 catch (TipcException e) {// we expect this exception from the while loop
}// catch
// drop our connection to RTserver
try { 26 srv.destroy(); 27 } catch (TipcException e) {// not concerned with problems dropping connection
}// catch
}// main
}// receive2
Let’s look at how this program extracts information from the data part of the message:
Compile the receive2.java program
Now compile the receive2.java
program using the command:
Start the receiving program
To demonstrate that everything is still working, start the receiving and sending programs in separate windows as you did earlier. First start the receive2
program using the command:
Start the sending program
After a few moments, start the sending program:
This message output is displayed when you run the receiving program:
Contents of NUMERIC_DATA message: --------------------------------- Var name = X, Var Value = 10 Var name = Y, Var Value = 20 Var name = Z, Var Value = 30
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 |