GizmoDistribution

gizmochat.cpp

GizmoChat example application.

A very simple chat program built with GizmoDistribution. It broadcast messages to other users by sending events on a global session.

The source code can also be found in the examples directory of the GizmoDistribution installation.

.

00001 //*****************************************************************************
00002 // File         : gizmochat.cpp
00003 // Module       : GizmoChat
00004 // Description  : A simple network chat program. (example application)
00005 // Author       : Christian Andersson
00006 // Product      : GizmoDistribution
00007 //      
00008 // Copyright © 2004 - Saab Training Systems AB, Sweden
00009 //          
00010 // NOTE:    GizmoDistribution is a toolkit used for implementing distributed
00011 //          objects and events in C++
00012 //
00013 //
00014 // Revision History...
00015 //
00016 // Who  Date    Description
00017 //
00018 // CAN  040513  Created file
00019 //
00020 //*****************************************************************************
00021 
00022 // Include all necessary files.
00023 #include "gzDistributionLibrary.h"
00024 
00025 
00026 // --------------------- The chat client class declaration ---------------------
00027 
00028 class CChatClient : public gzDistClientInterface
00029 {
00030     public:
00031         // Constructor declaration.
00032         CChatClient();
00033 
00034         // Destructor declaration.
00035         virtual ~CChatClient();
00036 
00037         // The run method.
00038         gzVoid run();
00039 
00040     protected:
00041 
00042         // Virtual callback reimplemented from gzDistClientInterface.
00043         gzVoid onEvent(gzDistEvent* event);
00044 };
00045 
00046 
00047 // --------------------- The code ---------------------
00048 
00049 // Constructor
00050 CChatClient::CChatClient() : gzDistClientInterface("ChatClient")
00051 {
00052 }
00053 
00054 // Destructor
00055 CChatClient::~CChatClient()
00056 {
00057 }
00058 
00059 gzVoid CChatClient::run()
00060 {
00061     // Create the distribution manager.
00062     gzDistManagerPtr manager = gzDistManager::getManager(TRUE);
00063 
00064     // Start the manager and use default channels.
00065     manager->start(gzDistCreateDefaultSessionChannel(), gzDistCreateDefaultServerChannel());
00066 
00067     // Initialize my distribution interface.
00068     initialize();
00069 
00070     // Create a global session where all chat messages will be sent.
00071     gzDistSessionPtr myChatSession = getSession("ChatSession", TRUE, TRUE);
00072 
00073     // Join the session.
00074     joinSession(myChatSession);
00075 
00076     // Subscribe all events on myChatSession.
00077     subscribeEvents(gzDistEvent::getClassType(), myChatSession);
00078 
00079     // Alloc a buffer for alias.
00080     gzChar alias[128];
00081     
00082     // Tell the user to enter his/her alias.
00083     printf("Enter your alias:");
00084 
00085     // Get user alias.
00086     gets(alias);
00087 
00088     // Print some information and a '>' at cursor position.
00089     printf("Write your messages here. Type 'exit' to exit.\r\n>");
00090 
00091     // Alloc a message buffer.
00092     gzChar message[1024];
00093 
00094     // Repeat until 'exit' is entered.
00095     while (gets(message))
00096     {
00097         // Quit if the user type 'exit'.
00098         if (gzString(message) == "exit")
00099         {
00100             // Break the 'while loop'.
00101             break;
00102         }
00103 
00104         // Don't send an empty message.
00105         if (strlen(message) == 0)
00106         {
00107             // Empty message. Put a cursor at cursor position and wait for new input.
00108             printf(">");
00109             continue;
00110         }
00111 
00112         // Put a cursor at cursor position.
00113         printf(">");
00114 
00115         // Create a new event.
00116         gzDistEvent* event = new gzDistEvent;
00117 
00118         // Put the alias in the event.
00119         event->setAttributeValue("Sender", alias);
00120 
00121         // Put the message in the event.
00122         event->setAttributeValue("Message", message);
00123 
00124         // Put an id for current process in the event.
00125         event->setAttributeValue("Id", gzDistGetCurrentProcessID());
00126 
00127         // Send my message on myChatSession.
00128         sendEvent(event, myChatSession);
00129     }
00130 
00131     // Release my reference to the chat session.
00132     myChatSession = NULL;
00133 
00134     // Uninitialize my distribution interface.
00135     uninitialize();
00136 
00137     // Shut down the manager.
00138     manager->shutDown();
00139 }
00140 
00141 gzVoid CChatClient::onEvent(gzDistEvent* event)
00142 {
00143     gzDouble id;
00144 
00145     // Get the id from the event
00146     if (event->getAttributeNumber("Id", id))
00147     {
00148         // Don't receive my own messages.
00149         if (id == gzDistGetCurrentProcessID())
00150         {
00151             // I'm the sender -> Don't display.
00152             return;
00153         }
00154     }
00155 
00156     // Get the sender name
00157     gzString sender = event->getAttributeString("Sender");
00158     
00159     // Get the message
00160     gzString message = event->getAttributeString("Message");
00161     
00162     // Display the message.
00163 #ifdef GZ_WIN32
00164     // Change this line to make the example use
00165     // message box output on other platforms than Win32.
00166     MessageBox(NULL, message, sender, MB_OK);
00167 #else
00168     // Default output to console.
00169     printf("\n");
00170     printf("%s: %s\n", (const char*)sender, (const char*)message);
00171 #endif
00172 
00173 }
00174 
00175 
00176 // --------------------- The main routine. ---------------------
00177 
00178 int main(int argc, char* argv[])
00179 {
00180     // Create the chat client.
00181     CChatClient chatClient;
00182 
00183     // Run it!
00184     chatClient.run();
00185 
00186     return 0;
00187 }

Documentation for GizmoDistribution generated at Wed Feb 20 11:59:20 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond