Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

superSimpleComm.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 /*! \file superSimpleComm.cpp
00003  *  \brief This file provides an example of how to use comm. This is about as simple as it gets...
00004  *  \author  $Author: rsharo $
00005  *  \version $Revision: 1.1 $
00006  *  \date    $Date: 2004/08/04 04:07:03 $
00007  *//////////////////////////////////////////////////////////////////////////////
00008 
00009 #include <stdio.h>
00010 
00011 #include "ace/ACE.h"
00012 #include "Utils/SystemProperties.h"
00013 #include "Comm/CommFactory.h"
00014 #include "Comm/MessageClient.h"
00015 #include "Comm/MessageServer.h"
00016 
00017 
00018 
00019 //
00020 // This class talks over the network.  Objects of this class get any messages
00021 // sent to their address. The address is specified when the object is created.
00022 // This is because the address is passed into the constructor.
00023 class SomeMessageClient : public MessageClient
00024 {
00025 public:
00026         //
00027         // The constructor establishes this client's ID field.
00028         // Messages with this ID will be read by this client.
00029         // We're also registering with the Comm system right out 
00030         // of the constructor for this example.
00031         SomeMessageClient(int myID, MessageServer *pMsgSvr)
00032                 : MessageClient(myID)
00033         {
00034                 Register(pMsgSvr);
00035         }
00036 
00037         //
00038         // All clients must implement this method.  This is the one that
00039         // is called when the client gets a message.
00040         // If this method ever returns -1, then the client is disconncted
00041         // and won't receive any more messages. Its a good idea to make this
00042         // method return -1 when the client has outlived its usefulness.
00043         // Clients can be reconnected later with another call to Register().
00044         virtual int ReceiveMessage(const char *msg, size_t len)
00045         {
00046                 printf("SomeMessageClient just got: %s\n", msg);
00047 
00048                 return 0;
00049         }
00050 
00051         //
00052         // This method just takes in a string, figures out how many bytes
00053         // make up that string, and use the base class' method to send it out.
00054         int SendMessage(const char *msg, u_int dest)
00055         {
00056                 //
00057                 // Call the method from the MessageClient class that actually sends
00058                 // out the message.  The first argument is the message to be sent. 
00059                 // The second argument is the number of bytes that make up the message.
00060                 // For strings, this is always the number of characters in the string
00061                 // plus one (the extra one is the "null" character that marks the end
00062                 // of the string).  The third argument is the address to which the
00063                 // message will be sent.
00064                 return MessageClient::SendMessage(msg, strlen(msg)+1, dest);
00065         }
00066 };
00067 
00068 
00069 
00070 
00071 
00072 int main()
00073 {
00074         //
00075         // Disable some debug messages that clutter the console.
00076         {
00077                 u_long mask =
00078                         ACE_Log_Msg::instance()->priority_mask(ACE_Log_Msg::PROCESS);
00079 
00080                 ACE_Log_Msg::instance()->priority_mask(mask & ~LM_INFO,
00081                         ACE_Log_Msg::PROCESS);
00082         }
00083 
00084         printf("*** Code is running.\n");
00085 
00086         //
00087         // Load the system configuration from an INI-style
00088         // config file.
00089         SystemProperties *props = SystemProperties::Instance("System.config");
00090 
00091         //
00092         // All you need to do to set up comm is construct a factory
00093         // and tell it to open. Then all the clients can use
00094         // the factory to connect to the network.
00095         PropertyBasedCommFactory commFact;
00096 
00097         //
00098         // This tells the factory to get its properties from the
00099         // section of the config file labeled "Typhon\Comm".
00100         // If this fails, the program gives up and quits.
00101         if (commFact.Open(*props, "Typhon\\Comm") == -1)
00102         {
00103                 printf("Comm setup failed.\n");
00104                 exit(-1);
00105         }
00106 
00107         //
00108         // Get the message server from our factory. We use this to connect
00109         // our clients to the network.
00110         MessageServer *pMsgSvr = commFact.GetMessageServer();
00111 
00112         //
00113         // Construct the first client. It will receive any messages 
00114         // sent to address 100.
00115         SomeMessageClient client1(100, pMsgSvr);
00116 
00117         //
00118         // Construct the second client. It will receive any messages 
00119         // sent to address 101.
00120         SomeMessageClient client2(101, pMsgSvr);
00121 
00122 
00123         //
00124         // Send a message from client1 to client2.  Client2 will print
00125         // the message on the screen. The first argument is the message
00126         // being sent. The second argument is the address to send to
00127         // (in this case, 101 sends
00128         // it to client2).
00129         client1.SendMessage("Hello there!", 101);
00130 
00131         //
00132         // Send a message from client2 to client1.  Client1 will print
00133         // the message on the screen. The first argument is the message
00134         // being sent.  The second argument is the address to send to
00135         // (in this case, 100 sends it to client1).
00136         client2.SendMessage("Hello yourself!", 100);
00137         
00138 
00139         //
00140         // Wait a second before shutting down. This gives the clients
00141         // plenty of time to process the messages.
00142         ACE_OS::sleep(1);
00143 
00144         printf("*** Code is done.\n");
00145         
00146                 
00147         return 0;
00148         
00149 }
00150 
00151 
00152 
00153 // Do not change the comments below - they will be added automatically by CVS
00154 /*****************************************************************************
00155 *       $Log: superSimpleComm.cpp,v $
00156 *       Revision 1.1  2004/08/04 04:07:03  rsharo
00157 *       Initial version.  This is an example of how to use the comm code that
00158 *       doesn't depend on any other Whorl code.  "No hardware needed."
00159 *
00160 *
00161 *       
00162 *
00163 ******************************************************************************/
00164 
00165 
00166 
00167 

Generated on Wed Sep 5 12:54:25 2007 for DSACSS Operational Code by  doxygen 1.3.9.1