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 Channel<IdType>::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 int main (int , char *argv[]) { 00061 00062 //step1. create channel 00063 Chan * my_chan = new Chan(); 00064 00065 //step2. connect channels 00066 char *peer_addr = argv[1]; 00067 Chan::UnixSockConnector *unix_conn = new Chan::UnixSockConnector(my_chan, true); 00068 unix_conn->open (); 00069 if (unix_conn->connect (peer_addr) == FAILURE) { 00070 cout << "failed to connect peer unix domain 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(TEST_STRING_MSG); 00079 00080 cout << "text_sender coming up ...\n"; 00081 00082 //step5. send msgs 00083 for(;;) { 00084 cout << "--- Please enter one line msg: " << endl; 00085 Test_String_Msg *tm = new Test_String_Msg(); 00086 if (!gets (tm->data)) { 00087 delete tm; 00088 break; 00089 } 00090 tm->len = strlen(tm->data)+1; 00091 my_src.send_msg (TEST_STRING_MSG, tm); 00092 } 00093 00094 cout << "text_sender exits...\n"; 00095 00096 return 0; 00097 }