L:/channel/channel/examples/text_recv/recv.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 string IdType;
00030 
00031 typedef Channel<IdType> Chan;
00032 typedef Chan::Msg ChanMsg;
00033 
00034 //------------------------------------------------------------------
00035 // Message definitions :
00036 //
00037 //ids for channel using int as msg id/type 
00038 /*
00039 int PING_MSG=2000;
00040 int PONG_MSG=2001;
00041 int TEST_STRING_MSG=2002;
00042 */
00043 //ids for channel using string as msg id/type 
00044 std::string PING_MSG = "_PING_";
00045 std::string PONG_MSG = "_PONG_";
00046 std::string TEST_STRING_MSG = "_TEST_";
00047 
00048 struct Test_String_Msg {
00049   enum { MAX_STR_LEN = 1024 };
00050   int len;
00051   char data[MAX_STR_LEN];
00052   Test_String_Msg() {
00053   }
00054 };
00055 
00056 //----------------------------------------------------------------
00057 // Implementation
00058 //
00059 
00060 class My_Callback: public Chan::Callback {
00061 public:
00062   My_Callback(Chan *c) : Chan::Callback(c) {}
00063   Status process(Msg *msg) {
00064     Test_String_Msg *sm = (Test_String_Msg *)msg->data();
00065     cout << "text_recv/My_Callback recv the following: " << sm->data << endl;
00066     cout << "...... Please notice callback borrow Channel internal thread\n";
00067  
00068     delete msg;
00069 
00070     return SUCCESS;
00071   }
00072 };
00073 
00074 int main (int argc, char *argv[]) {
00075 
00076   if (argc < 2) {
00077     cout << "Usage: recv my_unix_sock_path peer_unix_sock_path\n";
00078     return -1;
00079   }
00080 
00081   //step1. create channel
00082   Chan * my_chan = new Chan(); 
00083   
00084   //step2. setup channels
00085   char *my_addr = argv[1];
00086   Chan::UnixSockConnector *unix_conn = new Chan::UnixSockConnector(my_chan, true);
00087   if (unix_conn->open (my_addr) == FAILURE) {
00088     cout << "failed to open my unix domain socket at " <<  my_addr << endl;
00089     return -1;
00090   }
00091   if (argc >= 3) {
00092     char *peer_addr = argv[2];
00093     if (unix_conn->connect (peer_addr) == FAILURE) { 
00094       cout << "failed to connect peer unix domain socket at " << peer_addr << endl;
00095       return -1;
00096     }
00097   }
00098 
00099   //step3. create ports/callbacks attached to channels
00100   Chan::Port my_port(my_chan); //use my own queue
00101   My_Callback my_calbak(my_chan);
00102  
00103   //step4. sub msgs thru ports/callbacks
00104   my_port.subscribe_msg(TEST_STRING_MSG);
00105   my_calbak.subscribe_msg(TEST_STRING_MSG);
00106 
00107   cout << "text_recv coming up ...\n";
00108 
00109   ChanMsg *msg;
00110   Test_String_Msg *sm;
00111  
00112   //step5. recv msgs from ports and process them
00113   for(;;) {
00114     if(my_port.recv_msg(msg) == SUCCESS) {
00115       //
00116       if (msg->type == TEST_STRING_MSG) {
00117         sm = (Test_String_Msg *)msg->data();
00118         sm->data[sm->len-1] = '\0';
00119 
00120         cout << "text_recv receive the following: " <<  sm->data << endl;
00121         delete msg;
00122       }
00123     } else {
00124       //handle_error();
00125       break;
00126     }
00127   }
00128 
00129   cout << "text_recv  exits...\n";
00130 
00131   return 0;
00132 }

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