00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include <PongTask.h>
00013 #include <PongTask_export.h>
00014
00015
00016
00017
00018
00019
00020
00021 Status Pong_Task::prepare(void)
00022 {
00023 my_port()->publish_msg(PING_MSG, SCOPE_GLOBAL);
00024
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
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
00049 ACE_OS::sleep(ACE_Time_Value(1));
00050
00051
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
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
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
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)