L:/channel/channel/examples/local_chan_conn/local_chan_conn.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 
00016 #include "ace/OS_main.h"
00017 #include "ace/ACE.h"
00018 #include "ace/Auto_Ptr.h"
00019 #include "ace/OS_NS_string.h"
00020 #include "ace/OS_NS_stdio.h"
00021 #include "ace/Get_Opt.h"
00022 
00023 #include <iostream>
00024 #include <string>
00025 #include <Channel.h>
00026 
00027 using namespace std;
00028 using namespace channel;
00029 
00030 //------------------------------------------------------------------
00031 // Message definitions :
00032 // if we use int as IdType, define IdType as int
00033 //
00034 typedef string IdType;
00035 
00036 //ids for channel using int as msg id/type 
00037 /*
00038 int PING_MSG=2000;
00039 int PONG_MSG=2001;
00040 int TEST_STRING_MSG=2002;
00041 */
00042 //ids for channel using string as msg id/type 
00043 std::string PING_MSG = "_PING_";
00044 std::string PONG_MSG = "_PONG_";
00045 std::string TEST_STRING_MSG = "_TEST_";
00046 
00047 struct Test_String_Msg {
00048   enum { MAX_STR_LEN = 1024 };
00049   int len;
00050   char data[MAX_STR_LEN];
00051   Test_String_Msg() {
00052   }
00053 };
00054 
00055 //---------------------------------------------------------------
00056 // Channel specialization
00057 //
00058 typedef Channel<IdType> Chan;
00059 typedef Channel<IdType>::Msg ChanMsg;
00060 
00061 //----------------------------------------------------------------
00062 // Implementation
00063 //
00064 class My_Callback: public Chan::Callback {
00065 public:
00066   My_Callback(Chan *c) : Chan::Callback(c) {}
00067   Status process(Chan::Msg *msg) {
00068     Test_String_Msg *sm = (Test_String_Msg *)msg->data_holder_->data_;
00069     ACE_DEBUG ((LM_DEBUG, "(%t) text_recv/My_Callback recv the following: \n%s\n",
00070                 sm->data));
00071     ACE_DEBUG ((LM_DEBUG, "(%t) Please notice callback borrow Channel internal thread\n"));
00072  
00073     delete msg;
00074 
00075     return SUCCESS;
00076   }
00077 }; 
00078 
00079 int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) {
00080   ACE_UNUSED_ARG(argc);
00081   ACE_UNUSED_ARG(argv);
00082 
00083   ACE::debug('y');
00084 
00085   ACE_LOG_MSG->open
00086     (argv[0], ACE_Log_Msg::SYSLOG, ACE_TEXT (argv[0]));
00087   ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
00088 
00090   Chan * chan1 = new Chan(); 
00091   Chan * chan2 = new Chan(); 
00092   
00093   if (Chan::LocalConnector::connect (chan1, chan2) != SUCCESS)
00094     ACE_DEBUG ((LM_DEBUG, "(%t) failed to connect 2 local channels ...\n"));
00095 
00097   My_Callback my_calbak(chan2);
00098   Chan::Source my_src(chan1);
00099 
00101   my_src.publish_msg(TEST_STRING_MSG);
00102   my_calbak.subscribe_msg(TEST_STRING_MSG);
00103 
00104   ACE_DEBUG ((LM_DEBUG, "(%t) local_chan_conn coming up ...\n"));
00105 
00107 
00108   for(;;) {
00109       ACE_DEBUG((LM_DEBUG,  "--- Please enter one line msg: "));
00110       Test_String_Msg *tm =  new Test_String_Msg();
00111       if (!gets (tm->data))
00112         break;
00113       tm->len = strlen(tm->data)+1;
00114       my_src.send_msg (TEST_STRING_MSG, tm);
00115   }
00116 
00117   return 0;
00118 }

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