L:/channel/channel/examples/hier_id_text_recv/send.cpp

Go to the documentation of this file.
00001 
00002 // Copyright (c) 2005, 2006 Yigong Liu
00003 // Permission to use, copy, modify, distribute and sell this software for any 
00004 //     purpose is hereby granted without fee, provided that the above copyright 
00005 //     notice appear in all copies and that both that copyright notice and this 
00006 //     permission notice appear in supporting documentation.
00007 // The author makes no representations about the 
00008 //     suitability of this software for any purpose. It is provided "as is" 
00009 //     without express or implied warranty.
00011 
00018 #include <iostream>
00019 #include <string>
00020 
00021 #include <Channel.h>
00022 
00023 using namespace std;
00024 using namespace channel;
00025 
00026 //---------------------------------------------------------------
00027 // Channel specialization
00028 //
00029 typedef StringPathId<'/'> IdType;
00030 
00031 typedef Channel<IdType, IdTrait<IdType>, ACE_MT_SYNCH, TrieRouter<IdType, IdTrait<IdType>, ACE_MT_SYNCH> > Chan;
00032 typedef Chan::Msg ChanMsg;
00033 
00034 //------------------------------------------------------------------
00035 // Message definitions :
00036 //
00037 //app msgs ids 
00038 const char * PING_MSG = "/APP/PING";
00039 const char * PONG_MSG = "/APP/PONG";
00040 const char * TEST_STRING_MSG = "/APP/TEST";
00041 const char * APP_WILDCARD_MSG = "/APP/*";
00042 
00043 struct Test_String_Msg {
00044   enum { MAX_STR_LEN = 1024 };
00045   int len;
00046   char data[MAX_STR_LEN];
00047   Test_String_Msg() {
00048   }
00049 };
00050 
00051 //----------------------------------------------------------------
00052 // Implementation
00053 //
00054 
00055 int main (int argc, char **argv) {
00056   if (argc < 2) {
00057     std::cout << "Usage: hsend peer_host:port\n";
00058     return -1;
00059   } 
00060    
00061   //step1. create channel
00062   Chan * my_chan = new Chan(); 
00063   
00064   //step2. connect channels
00065   char *peer_addr = argv[1];
00066   Chan::TcpSockConnector *tcp_conn = new Chan::TcpSockConnector(my_chan, true);
00067   tcp_conn->open ();
00068   ConnInfo ci(peer_addr);
00069   if (tcp_conn->connect (ci) == FAILURE) { 
00070     cout << "failed to connect peer tcp socket at " << peer_addr << endl;
00071     return -1;
00072   }
00073 
00074   //step3. create src attached to channels
00075   Chan::Source my_src(my_chan); 
00076  
00077   //step4. pub msgs thru ports/callbacks
00078   my_src.publish_msg(APP_WILDCARD_MSG/*TEST_STRING_MSG*/);
00079 
00080   cout << "hier_sender coming up ...\n";
00081 
00082   //step5. send msgs
00083   for(;;) {
00084     cout << "--- Please enter one line msg [type(ping,pong,test)|data]  ";
00085       Test_String_Msg *tm =  new Test_String_Msg();
00086       if (!gets (tm->data))
00087         break;
00088       tm->len = strlen(tm->data)+1;
00089       string str(tm->data);
00090       size_t pos = str.find('|');
00091       const char *id;
00092       if (pos == string::npos) 
00093         id = TEST_STRING_MSG;
00094       else if (str.substr(0,pos) == "ping")
00095         id = PING_MSG;
00096       else if (str.substr(0,pos) == "pong")
00097         id = PONG_MSG;
00098       else
00099         id = TEST_STRING_MSG;
00100       
00101       my_src.send_msg (id, tm);
00102   }
00103 
00104   cout << "hier_sender  exits...\n";
00105 
00106   return 0;
00107 }

Generated on Mon Feb 27 19:59:21 2006 for channel by  doxygen 1.4.6-NO