Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » QAnywhere » Tutorial: Exploring TestMessage

Lesson 3: Send a message Next Page

Lesson 4: Explore the TestMessage client source code


This section of the tutorial takes you on a brief tour of the source code behind the TestMessage client application.

A good deal of the code implements the Windows interface, through which you can send, receive, and view the messages. This portion of the tutorial, however, focuses on the portions of the code given to QAnywhere.

You can find the TestMessage source code in the samples-dir\QAnywhere.

Several versions of the TestMessage source code are provided. The following versions are provided for Windows 2000 and Windows XP:

The following version is provided for .NET Compact Framework:

Required software

Visual Studio .NET 2003 or later is required to open the solution files and build the .NET Framework projects and the .NET Compact Framework project.

Exploring the C# source

This section takes you through the C# source code. The two versions are structured in a very similar manner.

Rather than look at each line in the application, this lesson highlights particular lines that are useful for understanding QAnywhere applications. It uses the C# version to illustrate these lines.

  1. Open the version of the TestMessage project that you are interested in.

    Double-click the solution file to open the project in Visual Studio .NET. For example, Samples\QAnywhere\Desktop\.NET\CS\TestMessage\TestMessage.sln is a solution file. There are several solution files for different environments.

  2. Ensure the Solution Explorer is open.

    You can open the Solution Explorer from the View menu.

  3. Inspect the Source Files folder.

    There are two files of particular importance. The MessageList file (MessageList.cs) receives messages and lets you view them. The NewMessage file (NewMessage.cs) allows you to construct and send messages.

  4. From the Solution Explorer, open the MessageList file.

  5. Inspect the included namespaces.

    Every QAnywhere application requires the iAnywhere.QAnywhere.Client namespace. The assembly that defines this namespace is supplied as the DLL iAnywhere.QAnywhere.Client.dll. The locations for this file are (relative to your SQL Anywhere installation directory):

    For your own projects, you must include a reference to this DLL when compiling. The namespace is included using the following line at the top of each file:

    using iAnywhere.QAnywhere.Client;
  6. Inspect the startReceiver method.

    This method performs initialization tasks that are common to QAnywhere applications:

    When you set a message listener for the queue, the QAnywhere Manager passes messages that arrive on that queue to that listener. Only one listener can be set for a given queue. Setting with a null listener clears out any listener for that queue.

    The MessageListener implementation receives messages asynchronously. You can also receive messages synchronously; that is, the application explicitly goes and looks for messages on the queue, perhaps in response to a user action such as clicking a Refresh button, rather than being notified when messages appear.

    Other initialization tasks include:

  7. Inspect the addMessage() method in the same file.

    This method is called whenever the application receives a message. It gets properties of the message such as its reply-to address, preferred name, and the time it was sent (Timestamp), and displays the information in the TestMessage user interface. The following lines cast the incoming message into a QATextMessage object and get the reply-to address of the message:

    text_msg = ( QATextMessage )msg;
    from = text_msg.ReplyToAddress;

    This completes a brief look at some of the major tasks carried out in the MessageList file.

  8. From the Solution Explorer, open the NewMessage file.

  9. Inspect the sendMessage() method.

    This method takes the information entered in the New Message dialog and constructs a QATextMessage object. The QAManager then puts the message in the queue to be sent.

    Here are the lines that create a QATextMessage object and set its ReplyToAddress property:

    qa_manager = MessageList.GetQAManager();
    msg = qa_manager.CreateTextMessage();
    msg.ReplyToAddress = MessageList.getOptions().ReceiveQueueName;

    Here are the lines that put the message in the queue to be sent. The variable dest is the destination address, supplied as an argument to the function.

    qa_manager.PutMessage( dest, msg );
Further reading