00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00018 #include <iostream>
00019 #include <string>
00020
00021 #include <Channel.h>
00022
00023 using namespace std;
00024 using namespace channel;
00025
00026
00027
00028
00029 typedef string IdType;
00030
00031 typedef Channel<IdType> Chan;
00032 typedef Chan::Msg ChanMsg;
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
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
00082 Chan * my_chan = new Chan();
00083
00084
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
00100 Chan::Port my_port(my_chan);
00101 My_Callback my_calbak(my_chan);
00102
00103
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
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
00125 break;
00126 }
00127 }
00128
00129 cout << "text_recv exits...\n";
00130
00131 return 0;
00132 }