Writing a SmartSockets Program in Visual Studio .NET


This section provides a procedure to write a simple C# SmartSockets program in Visual Studio .NET and descriptions of the classes used in the example.

The first part of the SmartSockets program invokes a connection to an RTserver and sets up the delegates that will process messages and errors from the server. The second part sends and receives a message to and from a subscribing RTclient.

Introduction to the Classes, Events, and Delegates used in this Program

This section briefly describes the classes used in the example program, which follows. For detailed information about all SmartSockets Classes, Events, and Delegates for .NET, see TIBCO SmartSockets .NET API Reference (HTML only).

TipcMt

The TipcMt methods create and retrieve information about message types, which are templates for messages. Many of the constants associated with TipcMt can be found in TipcMt fields; for example, the message type number for an “info” message would be TipcMt_Fields.INFO.

TipcMsg

The TipcMsg methods construct, manipulate and destroy messages as well as constructing and accessing the fields in the data section of a message.

TipcSvc

TipcSvc is a static factory class for creating instances of the TipcMt, TipcMsg, TipcConnClient, TipcConnServer, and TipcSrv classes. Objects of the required concrete classes that are created with this factory class.

   TipcMsg msgOut = TipcSvc.createMsg(TipcMt_Fields.INFO) 

TipcMsgEvent

TipcMsgEvent occurs when a message is processed, as in TipcSrv.mainloop.

Connection process events are raised while processing a message. This callback type is the most frequently used. A process delegate method is called for every of message received. When any message of any type is processed by calling process or mainLoop, the process callback is called.

TibcMsgHandler Delegate

Represents the method that will handle the TipcMsgEvent event.

   Srv.TipcMsgEvent += new TipcMsgHandler(this.msgHandler); 

TipcMsgEventArgs

This class contains event data for the TipcMsgEvent.

TipcSrv

TipcSrv methods communicate with RTserver to receive and process messages, for example, TipcSrv.mainLoop

TipcException

Superclass of all SmartSockets exceptions. This class defines an error number which may be set for some errors.

   catch (TipcException ex) { 
   } 

TipcDefs

The TipcDefs structure contains constants used across all classes.

Example: TipcDefs.CONN_FULL

TipcProcessCb

The user interface for connection process callbacks.

Connection process callbacks are executed while processing a message. This callback type is the most frequently used. A process callback can be called for a specific type of message, on a specific subject (destination), or created globally and called for all messages. For example, a process callback can be created for the INFO message type. When any message of that type is processed by calling process or mainLoop, the process callback is called. If the process callback is created globally, it is called for all INFO messages as well as any other type of message.

TipcProcessCB is included in this section for completeness only. TIBCO recommends that you use events and event handlers instead.

Writing the Program

Before you begin, start a new Visual C# Windows Application project and add the TIBCO.SS reference. (See Referencing the TIBCO SmartSockets .NET Assembly)

Perform these steps to create the RTserver connection program:

Task A Create a Quit Button
  1. From the Windows Form Controls, select the Button icon and place a button on the Form, as pictured in Figure 2.
Figure 2 Form Window

  1. In the Properties window, to the right of the text field, type: Quit. The button changes in real-time, reflecting what you type—the button should now have the label Quit.
  2. Double-click the Quit button you just created. The Code window appears, as shown in Figure 3.
Figure 3 Code Window

  1. At the current insertion point in the code window, type:
  2.    private bool running = false;
       Application.Exit();,

    Indent just as you would when writing other programs.

  3. Return to the Form window.
  4. Test the Quit button:
    1. On the toolbar, click Start. A window appears displaying the Quit button.
    2. Click Quit to close Form1 and return to the code window.
  5. Add a text box to the form, defining it as follows:
    • Set the Multiline Property to true.
    • Delete the Text property contents.
    • Size the window appropriately.
    • The resulting window should be similar to the one in Figure 4.

Figure 4 Form1 with Text Box

Task B Create a Message Handler
  1. Scroll up to the top of the code window and add the “using TIBCO.SMARTSOCKETS;” directive:
  2.    using System; 
       using System.Drawing; 
       using System.Collections; 
       using System.ComponentModel; 
       using System.Windows.Forms; 
       using System.Data; 
       using TIBCO.SMARTSOCKETS; 
    
  3. Add the following fields to the Form1 class:
  4.    public TipcSrv Srv; 
       private bool running = false; 
    
  5. Open the Method Wizard by right-clicking on the Form1 class in the project explorer, then select Add->Add Method.
  6. Create a message handler method with a the following characteristics:
    • Method access: public
    • Return type: void
    • Method name: msgHandler
    • Parameter name: object target
    • Parameter name: TipcMsgEventArgs e
    • The C# Method Wizard window should look similar to the one shown in Figure 5.

Figure 5 C# Method Wizard

  1. Add code to msgHandler such that the method looks like this:
  2.    public void msgHandler(object target, TipcMsgEventArgs e) { 
     
          try { 
             TipcMsg msgOut = TipcSvc.createMsg(TipcMt_Fields.INFO); 
             msgOut.Dest = e.Msg.Sender; 
             msgOut.appendStr("Message Received!"); 
             Srv.send(msgOut); 
             Srv.flush(); 
          } 
     
          catch (TipcException ex) { 
             MessageBox.Show(ex.Message, "msgHandler:  Exception", 
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
          } 
               
       } 
    
Task C Create Another Message Handler
  1. Create a second message handler, identical to the one you created in Task B, except name it msgHandler_Hail.
  2. Add code to msgHandler_Hail such that the method looks like this:
  3.    public void msgHandler_Hail(object target, TipcMsgEventArgs e) { 
     
          try { 
             if (e.Msg.Type.Num == TipcMt_Fields.INFO && 
                e.Msg.Dest.CompareTo("/hail") == 0) { 
                this.textBox1.Text += e.Msg.nextStr() +  
                                     System.Environment.NewLine; 
             } // if 
          } 
          catch (Exception ex) { 
             MessageBox.Show(ex.Message, "msgHandlerHail:  Exception", 
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
          } 
     
          } 
    
  4. Add code to the form’s constructor, so that the constructor looks like this:
  5. public Form1() 
    { 
       // 
       // Required for Windows Form Designer support 
       // 
       InitializeComponent(); 
     
        try { 
         Srv = TipcSvc.Srv; 
         Srv.setOption("ss.unique_subject", "/cs_example"); 
         Srv.setOption("ss.server_names", "tcp:_node"); 
     
         /* add the delegates */ 
         Srv.TipcMsgEvent += new TipcMsgHandler(this.msgHandler); 
         Srv.TipcMsgEvent += new TipcMsgHandler(this.msgHandler_Hail); 
     
         Srv.create(TipcDefs.CONN_FULL); 
         Srv.setSubjectSubscribe("/hail", true); 
        } 
        catch (Exception ex) { 
         MessageBox.Show(ex.Message, "msgHandlerHail:  Exception", 
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
             Application.Exit(); 
        } 
    } 
    
  6. Add code to the Form’s Activated event such that the event looks like this:

private void Form1_Activated(object sender, System.EventArgs e) {

   while (running) { 
      Srv.mainLoop(0.0); 
      Application.DoEvents(); 
   } 
} 

Note, the recommended method of handling the SmartSockets event loop (TipcSrv.mainLoop) is through another thread. The Form1_Activated event was used only for simplicity and brevity.

Task D Test the Program
  1. Verify that RTserver is running on the local machine.
  2. Launch Form1 by selecting Debug->Launch without Debugging from the menu bar, or enter CTRL+F5.
  3. Start RTmonitor with the –runtime option from the SmartSockets command prompt.
  4. Enter the following commands:
  5. MON> connect 
    09:01:10: TAL-SS-00088-I Connecting to project <rtworks> on 
    <tcp:_node:5555> RTserver 
    09:01:10: TAL-SS-00089-I Using tcp protocol 
    09:01:10: TAL-SS-00091-I Message from RTserver: Connection 
    established. 
    09:01:10: TAL-SS-00096-I Start subscribing to subject 
    </_CLSLAP01_2108> 
    09:01:10: TAL-SS-00111-I Start subscribing to subject </_CLSLAP01> 
    09:01:10: TAL-SS-00111-I Start subscribing to subject </_all> 
    09:01:10: TAL-SS-00111-I Start subscribing to subject </_mon> 
    MON> send info /hail "Hello!" 
    Sent info message to /hail subject. 
    MON> run 1 1 
    Received an unexpected message. 
    

The message below is what was constructed and sent back to RTmonitor in the msgHandler delegate. The messageHandler_Hail delegate displays the message contents in Form1’s text box.

type = info 
sender = </cs_example> 
sending server = </_CLSLAP01_2008> 
dest = </_CLSLAP01_2108> 
app = <rtworks> 
max = 2048 
size = 40 
current = 0 
read_only = false 
priority = 0 
compression = false 
delivery_mode = best_effort 
ref_count = 1 
seq_num = 0 
resend_mode = false 
user_prop = 0 
arrival_timestamp = 09:02:10 
data (num_fields = 1): 
  str "Message Received!" 
Processed a info message. 
MON> send info /hail "Hello Again!" 
  1. Form1 displays both messages:


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