L:/channel/cvm/examples/cvm_instance/tasks/ping/PingTask.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 <PingTask.h>
00013 #include <PingTask_export.h>
00014 #include <iostream>
00015 #include <Task_Msg.h>
00016 
00017 using namespace std;
00018 using namespace channel;
00019 
00020 //----------------------------------------------
00021 // the following 3 methods should be overwritten
00022 // to implement application logic
00023 //----------------------------------------------
00024 
00025 //app hooks for resource reservation & cleanup
00026 Status Ping_Task::prepare(void)
00027 {  
00028   my_port_->publish_msg(PONG_MSG, SCOPE_GLOBAL);
00029   my_port_->subscribe_msg(PING_MSG);
00030   return SUCCESS;
00031 }
00032  
00033 Status Ping_Task::cleanup(void)
00034 {
00035   my_port()->unpublish_msg(PONG_MSG);
00036   my_port()->unsubscribe_msg(PING_MSG);
00037   return SUCCESS;
00038 }
00039 
00040 
00041 int Ping_Task::work() {
00042   ACE_DEBUG ((LM_DEBUG,
00043               "(%t) %s ping_task coming up ...\n", my_name().c_str()));
00044   Msg *msg;
00045   Ping_Pong_Msg *sm, *rsp;
00046 
00047   for(;;) {
00048     if(my_port()->recv_msg(msg) == SUCCESS) {
00049       //handle_msg(msg);
00050       if(msg->type == PING_MSG) {
00051         sm = (Ping_Pong_Msg *)msg->data();
00052         sm->data[sm->len-1] = '\0';
00053 
00054         ACE_DEBUG ((LM_DEBUG, "(%t) (%s) receive the Ping_Pong_Msg[%d]: \n%s\n",
00055                     my_name().c_str(),sm->count,sm->data));
00056 
00057         rsp = new Ping_Pong_Msg();
00058         rsp->len = sm->len;
00059         rsp->count = sm->count+1;
00060         if(rsp->count > 4096) rsp->count=0;
00061         rsp->data[0] = '\0';
00062         strcpy(rsp->data,sm->data);
00063 
00064         Msg *m = new Msg(PONG_MSG, rsp);
00065         my_port()->send_msg(m);
00066 
00067         delete msg; 
00068         }
00069    } else {
00070       //handle_error();
00071       cerr << "recv_msg() failed once...\n";
00072       break;
00073     }
00074   }
00075 
00076   ACE_DEBUG ((LM_DEBUG,
00077               "(%t) %s ping_task exits...\n", my_name().c_str()));
00078 
00079   return 0;
00080 }
00081 
00082 ACE_FACTORY_DEFINE (PingTask, Ping_Task)

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