L:/channel/cvm/examples/cvm_instance/tasks/pong/PongTask.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 <PongTask.h>
00013 #include <PongTask_export.h>
00014 
00015 //----------------------------------------------
00016 // the following 3 methods should be overwritten
00017 // to implement application logic
00018 //----------------------------------------------
00019 
00020 //app hooks for resource reservation & cleanup
00021 Status Pong_Task::prepare(void)
00022 {  
00023   my_port()->publish_msg(PING_MSG, SCOPE_GLOBAL);
00024   //subscribe msg
00025   my_port()->subscribe_msg(PONG_MSG);
00026 
00027   return SUCCESS;
00028 }
00029 
00030 Status Pong_Task::cleanup(void)
00031 {
00032   ACE_DEBUG ((LM_DEBUG,
00033               "(%t) Pong_Task::cleanup(void)...\n"));
00034   my_port()->unpublish_msg(PING_MSG);
00035   //unsubscribe msg 
00036   my_port()->unsubscribe_msg(PONG_MSG);
00037   return SUCCESS;
00038 }
00039 
00040 
00041 int Pong_Task::work() {
00042   ACE_DEBUG ((LM_DEBUG,
00043               "(%t) %s pong_task coming up ...\n", my_name().c_str()));
00044   Msg *msg;
00045   Ping_Pong_Msg *sm, *rsp;
00046   int next_count = 1;
00047 
00048   //wait for sys ready
00049   ACE_OS::sleep(ACE_Time_Value(1));
00050 
00051  //start ping-pong
00052   rsp = new Ping_Pong_Msg();
00053   rsp->count = 0;
00054   strcpy(rsp->data,"Lets ping pong...");
00055   rsp->len = strlen("Lets ping pong...");
00056   Msg *m = new Msg(PING_MSG, rsp);
00057   my_port()->send_msg(m);
00058   ACE_DEBUG ((LM_DEBUG, "pong sends one msg..."));
00059 
00060   for(;;) {
00061     if(my_port()->recv_msg(msg) == SUCCESS) {
00062       //handle_msg(msg);
00063       if(msg->type == PONG_MSG) {
00064         sm = (Ping_Pong_Msg *)msg->data();
00065         sm->data[sm->len-1] = '\0';
00066 
00067         if (next_count != sm->count)
00068           {
00069             delete sm;
00070           }
00071         else 
00072           {
00073             ACE_DEBUG ((LM_DEBUG, "(%t) (%s) receive the Pong_Pong_Msg[%d]: \n%s\n",
00074                         my_name().c_str(),sm->count,sm->data));
00075 
00076             //reuse sm
00077             sm->len = sm->len;
00078             sm->count = sm->count+1;
00079             if(sm->count > 4096) rsp->count=0;
00080             next_count = sm->count + 1;
00081 
00082             ACE_OS::sleep(ACE_Time_Value(1));
00083 
00084             Msg *m = new Msg(PING_MSG, sm);
00085             my_port()->send_msg(m);
00086           }
00087       }
00088     } else {
00089       //handle_error();
00090       break;
00091     }
00092   }
00093 
00094   ACE_DEBUG ((LM_DEBUG,
00095               "(%t) %s pong_task exits...\n", my_name().c_str()));
00096 
00097   return 0;
00098 }
00099 
00100 ACE_FACTORY_DEFINE (PongTask, Pong_Task)

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