00001
00002
00003
00004
00005
00006
00007
00008
00009
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
00022
00023
00024
00025
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
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
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)