L:/channel/cvm/examples/cvm_instance/tasks/ctrl/CtrlTask.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  
00012 #include <CtrlTask.h>
00013 #include <CtrlTask_export.h>
00014 #include <Task_Msg.h>
00015 
00016 //----------------------------------------------
00017 // the following 3 methods should be overwritten
00018 // to implement application logic
00019 //----------------------------------------------
00020 
00021 //app hooks for resource reservation & cleanup
00022 Status Ctrl_Task::prepare(void)
00023 {  
00024   // Register ourselves to receive signals so we can shut down
00025   // gracefully.
00026   ACE_Sig_Set sig_set;
00027   sig_set.sig_add (SIGINT);
00028   sig_set.sig_add (SIGQUIT);
00029   sig_set.sig_add (SIGHUP);
00030 
00031   if (ACE_Reactor::instance ()->register_handler (sig_set,
00032                                                   this) == -1)
00033     ACE_ERROR_RETURN ((LM_ERROR,
00034                        "(%t) %p\n",
00035                        "register_handler"),
00036                       FAILURE);
00037 
00038   //publish msgs
00039   my_port()->publish_msg(TEST_STRING_MSG, SCOPE_GLOBAL);
00040 
00041   return SUCCESS;
00042 }
00043 
00044 Status Ctrl_Task::cleanup(void)
00045 {
00046   //unsubscribe msg 
00047   my_port()->unpublish_msg(TEST_STRING_MSG);
00048   return SUCCESS;
00049 }
00050 
00051 void Ctrl_Task::showTestMenu()
00052 {
00053   ACE_DEBUG((LM_DEBUG, "                  Channel Test Menu      \n"));
00054   ACE_DEBUG((LM_DEBUG, "     1. connect to remote node.\n"));
00055   ACE_DEBUG((LM_DEBUG, "     2. publish text message. \n"));
00056   ACE_DEBUG((LM_DEBUG, "     3. dump local routing tables. \n"));
00057   ACE_DEBUG((LM_DEBUG, "     4. quit \n\n"));
00058   ACE_DEBUG((LM_DEBUG, " Please enter :\n"));
00059   //ACE_OS::fflush(stdout);
00060 }
00061 
00062 int Ctrl_Task::work() {
00063   ACE_DEBUG ((LM_DEBUG,
00064               "(%t) %s ctrl_task coming up ...\n", my_name().c_str()));
00065   ACE_Reactor *reactor = ACE_Reactor::instance();
00066 
00067   UnixSockConnector *unix_conn = NULL;
00068   TcpSockConnector *tcp_conn = NULL;
00069 
00070   for(;;) {
00071     std::string conn_name;
00072     std::string user_input;
00073     showTestMenu();
00074     std::getline (std::cin, user_input, '\n');
00075     if (user_input == "4") {
00076       ::Quit_Handler *quit_handler = 0;
00077       ACE_NEW_RETURN (quit_handler, ::Quit_Handler (reactor), 0);
00078       reactor->notify (quit_handler);
00079       break;
00080     } else if(user_input == "3") {
00081       my_chan()->dump_routing_tables();
00082     } else if(user_input == "1") {
00083       ACE_DEBUG((LM_DEBUG, "--- Please enter Connector name: "));
00084       std::getline (std::cin, conn_name, '\n');
00085       ACE_DEBUG((LM_DEBUG, "--- Please enter destination (ip:port or unix_addr): "));
00086       std::getline (std::cin, user_input, '\n');
00087       ConnInfo ci(user_input);
00088       if(ci.valid()) {
00089         switch(ci.type()) {
00090         case INET_SOCK:
00091           tcp_conn = CvmTcpConnector::find_connector(conn_name.c_str());
00092           if (tcp_conn != NULL) 
00093             tcp_conn->connect(ci);
00094           else
00095             ACE_DEBUG((LM_DEBUG,  "Failed to find tcp connector [%s]", conn_name.c_str()));
00096           break;
00097         case UNIX_SOCK:
00098           unix_conn = CvmUnixConnector::find_connector(conn_name.c_str());
00099           if (unix_conn != NULL) 
00100             unix_conn->connect(ci);
00101           else
00102             ACE_DEBUG((LM_DEBUG,  "Failed to find unix connector [%s]", conn_name.c_str()));
00103           break;
00104         }
00105       }
00106     } else if(user_input == "2") {
00107       ACE_DEBUG((LM_DEBUG,  "--- Please enter one line msg: "));
00108       Test_String_Msg *tm =  new Test_String_Msg();
00109       if (!gets (tm->data))
00110         break;
00111       tm->len = strlen(tm->data)+1;
00112       ACE_DEBUG((LM_DEBUG,  "You entered: %s",tm->data));
00113       Msg *m = new Msg(TEST_STRING_MSG, tm);
00114       my_port()->send_msg (m);
00115     } 
00116   }
00117 
00118   ACE_DEBUG ((LM_DEBUG,
00119               "(%t) %s ctrl_task exits...\n", my_name().c_str()));
00120 
00121   return 0;
00122 }
00123 
00124 int
00125 Ctrl_Task::handle_signal (int signum, siginfo_t *, ucontext_t *)
00126 {
00127   switch(signum) {
00128     //SIGINT|SIGQUIT|SIGTERM cause proc/channel to exit, no need to broadcast to app tasks
00129   case SIGINT:
00130   case SIGQUIT:
00131     {
00132       ACE_Reactor *reactor = ACE_Reactor::instance ();
00133       // Shut down
00134       ::Quit_Handler *quit_handler = 0;
00135       ACE_NEW_RETURN (quit_handler, ::Quit_Handler (reactor), 0);
00136       reactor->notify (quit_handler);
00137     }
00138     break;
00139   default:  //other signal will be dropped
00140     {
00141       ACE_DEBUG((LM_DEBUG,  "--- recv signal[%d] , drpped ---\n",signum));
00142     }
00143     break;
00144   }
00145   return 0;
00146 }
00147 
00148 
00149 int Ctrl_Task::fini ()
00150 { 
00151   ACE_DEBUG((LM_DEBUG, "(%t) Task [%s] fini...\n", my_name().c_str()));
00152 
00153   //we still need wait some time for OS cleanup Task threads
00154   ACE_OS::sleep(1);
00155 
00156   //clean up port
00157   delete my_port_;
00158 
00159   return 0;
00160 }
00161 
00162 
00163 ACE_FACTORY_DEFINE (CtrlTask, Ctrl_Task)

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