00001
00002
00003
00004
00005
00006
00007
00008 #include "rcs_defs.hh"
00009
00010
00011 #ifdef EXTERN_C_STD_HEADERS
00012 extern "C"
00013 {
00014 #endif
00015
00016 #include <stdlib.h>
00017 #include <string.h>
00018
00019 #ifdef EXTERN_C_STD_HEADERS
00020 }
00021 #endif
00022
00023 #include "nml.hh"
00024 #include "nmlmsg.hh"
00025 #include "cmd_msg.hh"
00026
00027 #include "stat_msg.hh"
00028 #include "wm_msg.hh"
00029 #include "node.hh"
00030 #include "linklist.hh"
00031 #include "nodelink.hh"
00032 #include "rcs_prnt.hh"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 NODE_LINK::NODE_LINK (NML_FORMAT_PTR f_ptr, char *n, char *file, NODE * node,
00056 NODE_LINK_TYPE link_type)
00057 {
00058 char *command_buffer;
00059 char *status_buffer;
00060 char *wm_buffer;
00061
00062
00063
00064
00065 name = NULL;
00066 cmd = NULL;
00067 status = NULL;
00068 wm = NULL;
00069 parent_node = node;
00070
00071
00072 if (NULL == node)
00073 {
00074 rcs_print_error
00075 ("NODE_LINK::Constructor - parent node must not be NULL.\n");
00076 return;
00077 }
00078
00079 if (!node->valid)
00080 {
00081 return;
00082 }
00083
00084 type_of_link = link_type;
00085
00086
00087
00088 name = (char *) malloc (strlen (n) + 1);
00089 if (NULL == name)
00090 {
00091 node->valid = 0;
00092 rcs_print_error ("%s: Error constructing NODE_LINK %s.\n", node->name,
00093 n);
00094 return;
00095 }
00096 strcpy (name, n);
00097
00098 if ((NODE_SUBORDINATE_ONLY == link_type)
00099 || (NODE_WM_AND_SUBORDINATE == link_type))
00100 {
00101 if (-1 ==
00102 (sub_list_id =
00103 parent_node->sub_list->store_at_tail (this, sizeof (NODE_LINK),
00104 0)))
00105 {
00106 parent_node->valid = 0;
00107 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00108 node->name, n);
00109 return;
00110 }
00111
00112
00113 command_buffer =
00114 (char *) malloc (strlen (name) + RCS_COMMAND_SUFFIX_LENGTH + 1);
00115 if (NULL == command_buffer)
00116 {
00117 node->valid = 0;
00118 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00119 node->name, name);
00120 return;
00121 }
00122 strcpy (command_buffer, name);
00123 strcat (command_buffer, RCS_COMMAND_SUFFIX);
00124 cmd = new RCS_CMD_CHANNEL (f_ptr, command_buffer, node->name, file);
00125 free (command_buffer);
00126
00127 if (NULL == cmd)
00128 {
00129 node->valid = 0;
00130 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00131 node->name, name);
00132 return;
00133 }
00134 if (!cmd->valid ())
00135 {
00136 node->valid = 0;
00137 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00138 node->name, name);
00139 return;
00140 }
00141
00142
00143 generic_command = new RCS_GENERIC_CMD;
00144 if (NULL == generic_command)
00145 {
00146 node->valid = 0;
00147 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00148 node->name, name);
00149 return;
00150 }
00151
00152
00153 status_buffer =
00154 (char *) malloc (strlen (name) + RCS_STATUS_SUFFIX_LENGTH + 1);
00155 if (NULL == status_buffer)
00156 {
00157 node->valid = 0;
00158 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00159 node->name, name);
00160 return;
00161 }
00162 strcpy (status_buffer, name);
00163 strcat (status_buffer, RCS_STATUS_SUFFIX);
00164 status = new RCS_STAT_CHANNEL (f_ptr, status_buffer, node->name, file);
00165 free (status_buffer);
00166 if (NULL == status)
00167 {
00168 node->valid = 0;
00169 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00170 node->name, name);
00171 return;
00172 }
00173 if (!status->valid ())
00174 {
00175 node->valid = 0;
00176 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00177 node->name, name);
00178 return;
00179 }
00180
00181 node->num_of_subord++;
00182 }
00183
00184 if ((NODE_WM_ONLY == link_type) || (NODE_WM_AND_SUBORDINATE == link_type))
00185 {
00186 if (-1 ==
00187 (wm_list_id =
00188 parent_node->wm_in_list->store_at_tail (this, sizeof (NODE_LINK),
00189 0)))
00190 {
00191 parent_node->valid = 0;
00192 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00193 node->name, n);
00194 return;
00195 }
00196
00197
00198 wm_buffer = (char *) malloc (strlen (name) + RCS_WM_SUFFIX_LENGTH + 1);
00199 if (NULL == wm_buffer)
00200 {
00201 node->valid = 0;
00202 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00203 node->name, name);
00204 return;
00205 }
00206 strcpy (wm_buffer, name);
00207 strcat (wm_buffer, RCS_WM_SUFFIX);
00208 wm = new RCS_WM_CHANNEL (f_ptr, wm_buffer, node->name, file);
00209 free (wm_buffer);
00210
00211 if (NULL == wm)
00212 {
00213 node->valid = 0;
00214 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00215 node->name, name);
00216 return;
00217 }
00218 if (!wm->valid ())
00219 {
00220 node->valid = 0;
00221 rcs_print_error ("%s: Error constructing NODE_LINK to %s.\n",
00222 node->name, name);
00223 return;
00224 }
00225 }
00226
00227 serial_number = 0;
00228 new_cmd = 0;
00229
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239 NODE_LINK::~NODE_LINK ()
00240 {
00241 if (NULL != parent_node)
00242 {
00243 if (NULL != parent_node->sub_list)
00244 {
00245 if ((NODE_SUBORDINATE_ONLY == type_of_link)
00246 || (NODE_WM_AND_SUBORDINATE == type_of_link))
00247 {
00248 parent_node->sub_list->delete_node (sub_list_id);
00249 }
00250 if ((NODE_WM_ONLY == type_of_link)
00251 || (NODE_WM_AND_SUBORDINATE == type_of_link))
00252 {
00253 parent_node->wm_in_list->delete_node (wm_list_id);
00254 }
00255 }
00256 }
00257 if (NULL != cmd)
00258 {
00259 delete cmd;
00260 cmd = NULL;
00261 }
00262 if (NULL != status)
00263 {
00264 delete status;
00265 status = NULL;
00266 }
00267 if (NULL != wm)
00268 {
00269 delete wm;
00270 wm = NULL;
00271 }
00272 if (NULL != name)
00273 {
00274 free (name);
00275 name = NULL;
00276 }
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 int
00293 NODE_LINK::get_status ()
00294 {
00295 if (NULL == status)
00296 {
00297 return (-1);
00298 }
00299
00300 if (-1 == (status_type = status->read ()))
00301 {
00302 node_status = NODE_ERROR;
00303 return (-1);
00304 }
00305 else if (0 != status_type)
00306 {
00307 status_msg = status->get_address ();
00308 node_status = (NODE_STATUS_TYPE) status_msg->node_status;
00309 if ((node_status == NODE_DONE) &&
00310 (serial_number != status_msg->serial_number))
00311 {
00312 node_status = NODE_WAITING;
00313 }
00314
00315 rcs_print_debug (PRINT_STATUS_RECIEVED,
00316 "%s: Recieved new status from %s.\n",
00317 parent_node->name, name);
00318 rcs_print_debug (PRINT_STATUS_RECIEVED,
00319 " (type = %ld (0x%lX), serial_num = %d,",
00320 status_msg->type,
00321 status_msg->type, status_msg->serial_number);
00322 rcs_print_debug (PRINT_STATUS_RECIEVED, " status = %d)\n", node_status);
00323 status_new = 1;
00324 }
00325 else
00326 {
00327 status_new = 0;
00328 }
00329
00330 return (0);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 int
00347 NODE_LINK::read_wm ()
00348 {
00349 if (NULL == wm)
00350 {
00351 return (-1);
00352 }
00353
00354 if (-1 == (wm_type = wm->read ()))
00355 {
00356 node_status = NODE_ERROR;
00357 return (-1);
00358 }
00359 else if (0 != wm_type)
00360 {
00361 wm_msg = wm->get_address ();
00362 new_wm = 1;
00363 rcs_print_debug (PRINT_NEW_WM,
00364 "%s: Recieved new world model data from %s.\n",
00365 parent_node->name, name);
00366 rcs_print_debug (PRINT_NEW_WM, " (type = %ld (0x%lX))", wm_msg->type,
00367 wm_msg->type);
00368 }
00369 else
00370 {
00371 new_wm = 0;
00372 }
00373
00374 return (0);
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 int
00387 NODE_LINK::send_command ()
00388 {
00389 if ((NULL == cmd) || (NULL == parent_node))
00390 {
00391 return (-1);
00392 }
00393
00394 if (new_cmd)
00395 {
00396 serial_number++;
00397 cmd_msg->serial_number = serial_number;
00398
00399 rcs_print_debug (PRINT_COMMANDS_SENT,
00400 "%s: Sent new command to %s.\n", parent_node->name,
00401 name);
00402 rcs_print_debug (PRINT_COMMANDS_SENT,
00403 " (type = %ld (0x%lX), serial_num = %d)\n",
00404 cmd_msg->type, cmd_msg->type, serial_number);
00405 node_status = NODE_WAITING;
00406 if (parent_node->config_flags & NODE_WAIT_FOR_COMMANDS_TO_BE_READ)
00407 {
00408 if (-1 == cmd->write_if_read (cmd_msg))
00409 {
00410 if (cmd->error_type == NML_BUFFER_NOT_READ)
00411 {
00412 return (0);
00413 }
00414 else
00415 {
00416 node_status = NODE_ERROR;
00417 return (-1);
00418 }
00419 }
00420 }
00421 else
00422 {
00423 if (-1 == cmd->write (cmd_msg))
00424 {
00425 node_status = NODE_ERROR;
00426 return (-1);
00427 }
00428 }
00429 new_cmd = 0;
00430 }
00431
00432 return (0);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 void
00448 NODE_LINK::new_command (RCS_CMD_MSG * new_cmd_msg)
00449 {
00450 cmd_msg = new_cmd_msg;
00451 new_cmd = 1;
00452 node_status = NODE_WAITING;
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 void
00468 NODE_LINK::new_command (RCS_GENERIC_CMD_ID new_id)
00469 {
00470 generic_command->gen_id = new_id;
00471 cmd_msg = generic_command;
00472 new_cmd = 1;
00473 }