Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "MotoPlus.h"
00033 #include "ParameterExtraction.h"
00034 #include "CtrlGroup.h"
00035 #include "SimpleMessage.h"
00036 #include "Controller.h"
00037 #include "StateServer.h"
00038
00039
00040
00041
00042 void Ros_StateServer_StartNewConnection(Controller* controller, int sd);
00043 void Ros_StateServer_SendState(Controller* controller);
00044 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, SimpleMsg* sendMsg, int msgSize);
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 void Ros_StateServer_StartNewConnection(Controller* controller, int sd)
00056 {
00057 int connectionIndex;
00058
00059 printf("Starting new connection to the State Server\r\n");
00060
00061
00062 for (connectionIndex = 0; connectionIndex < MAX_STATE_CONNECTIONS; connectionIndex++)
00063 {
00064 if (controller->sdStateConnections[connectionIndex] == INVALID_SOCKET)
00065 {
00066
00067
00068
00069 controller->sdStateConnections[connectionIndex] = sd;
00070
00071
00072 if(controller->tidStateSendState == INVALID_TASK)
00073 {
00074
00075 controller->tidStateSendState = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE,
00076 (FUNCPTR)Ros_StateServer_SendState,
00077 (int)controller, 0, 0, 0, 0, 0, 0, 0, 0, 0);
00078
00079
00080 if(controller->tidStateSendState != INVALID_TASK)
00081 Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, TRUE);
00082 }
00083
00084 break;
00085 }
00086 }
00087
00088 if (connectionIndex == MAX_STATE_CONNECTIONS)
00089 {
00090 printf("Too many State server connections... not accepting last attempt.\r\n");
00091 mpClose(sd);
00092 }
00093 }
00094
00095
00096
00097
00098
00099 void Ros_StateServer_SendState(Controller* controller)
00100 {
00101 BOOL bHasConnections = TRUE;
00102 int groupNo;
00103 SimpleMsg sendMsg;
00104 int msgSize;
00105
00106 printf("Starting State Server Send State task\r\n");
00107 printf("Controller number of group = %d\r\n", controller->numGroup);
00108
00109 while(bHasConnections)
00110 {
00111
00112 for(groupNo=0; groupNo < controller->numGroup; groupNo++)
00113 {
00114 msgSize = Ros_SimpleMsg_JointFeedback(controller->ctrlGroups[groupNo], &sendMsg);
00115 if(msgSize > 0)
00116 {
00117 bHasConnections = Ros_StateServer_SendMsgToAllClient(controller, &sendMsg, msgSize);
00118 }
00119 else
00120 {
00121 printf("Ros_SimpleMsg_JointFeedback returned a message size of 0\r\n");
00122 }
00123 }
00124
00125
00126 if(bHasConnections)
00127 {
00128 msgSize = Ros_Controller_StatusToMsg(controller, &sendMsg);
00129 if(msgSize > 0)
00130 {
00131 bHasConnections = Ros_StateServer_SendMsgToAllClient(controller, &sendMsg, msgSize);
00132 }
00133 }
00134 mpTaskDelay(STATE_UPDATE_MIN_PERIOD);
00135 }
00136
00137
00138 controller->tidStateSendState = INVALID_TASK;
00139 Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, FALSE);
00140 printf("State Server Send State task was terminated\r\n");
00141 mpDeleteSelf;
00142 }
00143
00144
00145
00146
00147
00148
00149 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, SimpleMsg* sendMsg, int msgSize)
00150 {
00151 int index;
00152 int ret;
00153 BOOL bHasConnections = FALSE;
00154
00155
00156 for(index = 0; index < MAX_STATE_CONNECTIONS; index++)
00157 {
00158 if(controller->sdStateConnections[index] != INVALID_SOCKET)
00159 {
00160 ret = mpSend(controller->sdStateConnections[index], (char*)(sendMsg), msgSize, 0);
00161 if(ret <= 0)
00162 {
00163 printf("StateServer Send failure. Closing state server connection.\r\n");
00164 mpClose(controller->sdStateConnections[index]);
00165 controller->sdStateConnections[index] = INVALID_SOCKET;
00166 }
00167 else
00168 {
00169 bHasConnections = TRUE;
00170 }
00171 }
00172 }
00173 return bHasConnections;
00174 }