nodeManagerInterface.c 26.2 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
/*****************************************************************************
 *  Copyright (c) 2008, University of Florida
 *  All rights reserved.
 *  
 *  This file is part of OpenJAUS.  OpenJAUS is distributed under the BSD 
 *  license.  See the LICENSE file for details.
 * 
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of the University of Florida nor the names of its 
 *       contributors may be used to endorse or promote products derived from 
 *       this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
// File:		libNodeManager.c
// Version:		3.3 BETA
// Written by:	Tom Galluzzo (galluzzo AT gmail DOT com) and Danny Kent (jaus AT dannykent DOT com)
// Date:		04/15/08
// Description:	The libnodeManager provides an interface for a commponent to access core node management,
//				service connections, large message handling and default message processing

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined (WIN32)
	#define WIN32_LEAN_AND_MEAN
	#include <winsock2.h>
	#include <windows.h>
#elif defined(__linux) || defined(linux) || defined(__linux__) || defined(__APPLE__)
	#include <unistd.h>
	#include <sys/time.h>
	#include <errno.h>
#endif

#include "nodeManagerInterface/nodeManagerInterface.h"

#define JAUS_OPC_UDP_HEADER				"JAUS01.0"
#define JAUS_OPC_UDP_HEADER_SIZE_BYTES	8 

#define NODE_MANAGER_INTERFACE_PORT 24627
#define NODE_MANAGER_MESSAGE_PORT 24629
#define NODE_MANAGER_TIMEOUT_SEC 3.0

#define INTERFACE_THREAD_TIMEOUT_SEC	3.0
#define INTERFACE_SOCKET_TIMEOUT_SEC	1.0
#define MESSAGE_SOCKET_TIMEOUT_SEC		0.5

#define INTERFACE_MESSAGE_SIZE_BYTES 							8
#define INTERFACE_MESSAGE_CHECK_IN								0x01
#define INTERFACE_MESSAGE_REPORT_ADDRESS						0x02
#define INTERFACE_MESSAGE_CHECK_OUT								0x03
#define INTERFACE_MESSAGE_VERIFY_ADDRESS						0x04
#define INTERFACE_MESSAGE_ADDRESS_VERIFIED						0x05
#define INTERFACE_MESSAGE_GET_COMPONENT_ADDRESS_LIST			0x06
#define INTERFACE_MESSAGE_COMPONENT_ADDRESS_LIST_RESPONSE		0x07
#define INTERFACE_MESSAGE_LOOKUP_ADDRESS						0x08
#define INTERFACE_MESSAGE_LOOKUP_ADDRESS_RESPONSE				0x09
#define INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS				0x0A
#define INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS_RESPONSE		0x0B
#define INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS_LIST			0x0C
#define INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS_LIST_RESPONSE	0x0D

static int checkIntoNodeManager(NodeManagerInterface);
static int checkOutOfNodeManager(NodeManagerInterface);
void *heartbeatThread(void *);
void *receiveThread(void *);

NodeManagerInterface nodeManagerOpen(JausComponent cmpt)
{
	NodeManagerInterface nmi = NULL;

	if(cmpt == NULL)
	{
		return NULL;
	}
	
	nmi = (NodeManagerInterface)malloc(sizeof(NodeManagerInterfaceStruct));
	if(nmi == NULL)
	{
		return NULL;
	}
	
	nmi->cmpt = cmpt;
	nmi->timestamp = ojGetTimeSec();
	pthread_cond_init(&nmi->recvCondition, NULL);
	pthread_cond_init(&nmi->hbWakeCondition, NULL);
	
	nmi->ipAddress = inetAddressGetLocalHost();
	if(nmi->ipAddress == NULL)
	{
		free(nmi);
		return NULL;
	}

	nmi->interfaceSocket = datagramSocketCreate(0, nmi->ipAddress);
	if(nmi->interfaceSocket == NULL)
	{
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}
	datagramSocketSetTimeout(nmi->interfaceSocket, INTERFACE_SOCKET_TIMEOUT_SEC);
	
	memset(&nmi->messageSocket, 0, sizeof(nmi->messageSocket));
	nmi->messageSocket = datagramSocketCreate(0, nmi->ipAddress);
	if(nmi->messageSocket == NULL)
	{
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}
	datagramSocketSetTimeout(nmi->messageSocket, MESSAGE_SOCKET_TIMEOUT_SEC);

	if(checkIntoNodeManager(nmi))
	{
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}

	nmi->receiveQueue = queueCreate();
	
	nmi->scm = scManagerCreate();
	if(nmi->scm == NULL)
	{
		queueDestroy(nmi->receiveQueue, NULL);
		checkOutOfNodeManager(nmi);
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}
		
	nmi->lmh = lmHandlerCreate();
	if(nmi->lmh == NULL)
	{
		scManagerDestroy(nmi->scm);
		queueDestroy(nmi->receiveQueue, NULL);
		checkOutOfNodeManager(nmi);
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}

	nmi->isOpen = JAUS_TRUE;

	if(pthread_create(&nmi->heartbeatThreadId, NULL, heartbeatThread, (void *)nmi))
	{
		lmHandlerDestroy(nmi->lmh);
		scManagerDestroy(nmi->scm);
		queueDestroy(nmi->receiveQueue, NULL);
		checkOutOfNodeManager(nmi);
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}

	if(pthread_create(&nmi->receiveThreadId, NULL, receiveThread, (void *)nmi))
	{
		pthread_cancel(nmi->heartbeatThreadId);
		lmHandlerDestroy(nmi->lmh);
		scManagerDestroy(nmi->scm);
		queueDestroy(nmi->receiveQueue, NULL);
		checkOutOfNodeManager(nmi);
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		free(nmi);
		return NULL;
	}
	
	scManagerAddSupportedMessage(nmi, JAUS_REPORT_COMPONENT_STATUS);
	scManagerAddSupportedMessage(nmi, JAUS_REPORT_COMPONENT_AUTHORITY);

	return nmi;
}

int nodeManagerClose(NodeManagerInterface nmi)
{
	int result = 0;
	
	if(nmi == NULL)
	{
		return -1;
	}
	
	if(nmi->isOpen) // Execute the close only if the connection is open
	{
		scManagerRemoveSupportedMessage(nmi, JAUS_REPORT_COMPONENT_STATUS);
		scManagerRemoveSupportedMessage(nmi, JAUS_REPORT_COMPONENT_AUTHORITY);

		nmi->isOpen = 0;

		pthread_cond_signal(&nmi->hbWakeCondition);
		pthread_join(nmi->heartbeatThreadId, NULL);

		CLOSE_SOCKET(nmi->messageSocket->descriptor);
		pthread_join(nmi->receiveThreadId, NULL);

		lmHandlerDestroy(nmi->lmh);
		scManagerDestroy(nmi->scm);
		queueDestroy(nmi->receiveQueue, (void*)jausMessageDestroy); 
		checkOutOfNodeManager(nmi);
		datagramSocketDestroy(nmi->messageSocket);
		datagramSocketDestroy(nmi->interfaceSocket);
		inetAddressDestroy(nmi->ipAddress);
		pthread_cond_destroy(&nmi->recvCondition);
		pthread_cond_destroy(&nmi->hbWakeCondition);
		free(nmi);
	}
	else
	{
		return -2;
	}

	return result;
};

static int checkIntoNodeManager(NodeManagerInterface nmi)
{
	DatagramPacket packet;

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0]= INTERFACE_MESSAGE_CHECK_IN;
	packet->buffer[1]= nmi->cmpt->address->component;
	packet->buffer[2] = (unsigned char)(nmi->messageSocket->port & 0xFF);
	packet->buffer[3] = (unsigned char)((nmi->messageSocket->port & 0xFF00) >> 8);

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;

	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	if(packet->buffer[0] == INTERFACE_MESSAGE_REPORT_ADDRESS)
	{
		nmi->cmpt->address->instance = packet->buffer[1];
		nmi->cmpt->address->component = packet->buffer[2];
		nmi->cmpt->address->node = packet->buffer[3];
		nmi->cmpt->address->subsystem = packet->buffer[4];

		free(packet->buffer);
		datagramPacketDestroy(packet);
		return 0;
	}
	else
	{
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return -1;
	}
}

static int checkOutOfNodeManager(NodeManagerInterface nmi)
{
	DatagramPacket packet;

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES * sizeof(unsigned char);
	packet->buffer = (unsigned char*)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0]= INTERFACE_MESSAGE_CHECK_OUT;
	packet->buffer[1]= nmi->cmpt->address->instance;
	packet->buffer[2] = nmi->cmpt->address->component;
	packet->buffer[3] = nmi->cmpt->address->node;
	packet->buffer[4] = nmi->cmpt->address->subsystem;
	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;

	datagramSocketSend(nmi->interfaceSocket, packet);

	// datagramSocketReceive(nmi->interfaceSocket, &packet);  TODO: Implement reply

	free(packet->buffer);
	datagramPacketDestroy(packet);
	return 1;
}

void *heartbeatThread(void *threadArgument)
{
	pthread_mutex_t hbMutex = PTHREAD_MUTEX_INITIALIZER;
	int condition = -1;
	struct timespec timeLimitSpec;
	double timeLimitSec = 0;
	NodeManagerInterface nmi = (NodeManagerInterface)threadArgument;
	JausMessage txMessage;
	ReportHeartbeatPulseMessage heartbeat = reportHeartbeatPulseMessageCreate();
	
	nmi->heartbeatThreadRunning = 1;
	nmi->heartbeatCount = 0;
	
	jausAddressCopy(heartbeat->source, nmi->cmpt->address);
	heartbeat->destination->subsystem = nmi->cmpt->address->subsystem;
	heartbeat->destination->node = nmi->cmpt->address->node;
	heartbeat->destination->component = JAUS_NODE_MANAGER_COMPONENT;
	heartbeat->destination->instance = 1;

	txMessage = reportHeartbeatPulseMessageToJausMessage(heartbeat);

	while(nmi->isOpen)
	{
		nodeManagerSend(nmi, txMessage);
		nmi->heartbeatCount++;
		if( (ojGetTimeSec() - NODE_MANAGER_TIMEOUT_SEC) > nmi->timestamp)
		{
			// TODO: Capture Error
			//printf("libNodeManager: Node Manager Has Timed Out\n");
			//nmi->cmpt->state = JAUS_FAILURE_STATE;
			break;
		}

		timeLimitSec = ojGetTimeSec() + 1.0;
		timeLimitSpec.tv_sec = (long)timeLimitSec;
		timeLimitSpec.tv_nsec = (long)(1e9 * (timeLimitSec - (double)timeLimitSpec.tv_sec));

		pthread_mutex_lock(&hbMutex);
		condition = pthread_cond_timedwait(&nmi->hbWakeCondition, &hbMutex, &timeLimitSpec);
		pthread_mutex_unlock(&hbMutex);
	}

	jausMessageDestroy(txMessage);	
	reportHeartbeatPulseMessageDestroy(heartbeat);
	nmi->heartbeatThreadRunning = 0;
	
	return NULL;
}

void *receiveThread(void *threadArgument)
{
	int index;
	
	NodeManagerInterface nmi = (NodeManagerInterface)threadArgument;
	DatagramPacket packet;

	JausMessage message;

	nmi->receiveThreadRunning = 1;

	nmi->receiveCount = 0;
	
	packet = datagramPacketCreate();

	packet->bufferSizeBytes = JAUS_HEADER_SIZE_BYTES + JAUS_MAX_DATA_SIZE_BYTES;
	packet->buffer = (unsigned char*)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);
	
	while(nmi->isOpen)
	{
		if(datagramSocketReceive(nmi->messageSocket, packet) > 0)
		{	
			index = 0;
			if(!strncmp((char *)packet->buffer, JAUS_OPC_UDP_HEADER, JAUS_OPC_UDP_HEADER_SIZE_BYTES)) // equals 1 if same
			{
				index += JAUS_OPC_UDP_HEADER_SIZE_BYTES;
			}

			message = jausMessageCreate();
			if(jausMessageFromBuffer(message, packet->buffer + index, packet->bufferSizeBytes - index))
			{
				if(message->dataFlag)
				{
					lmHandlerReceiveLargeMessage(nmi, message);
				}
				else
				{
					if(message->properties.scFlag)
					{
						if(	(message->commandCode >= JAUS_CREATE_SERVICE_CONNECTION && 
							message->commandCode <= JAUS_TERMINATE_SERVICE_CONNECTION) ||
							message->commandCode == JAUS_CREATE_EVENT ||
							message->commandCode == JAUS_CONFIRM_EVENT_REQUEST ||
							message->commandCode == JAUS_CANCEL_EVENT)
						{
							// This is to take Service Connection Control messages and send them on through
							// to the regular receiveQueue. JAUS 3.2 RA says to set the properties.scFlag bit if it is
							// a Service Connection Control message, but logically they do not need to go
							// to the scManager and instead to the component
							queuePush(nmi->receiveQueue, (void *)message);
						}
						else
						{
							scManagerReceiveMessage(nmi, message);
						}
					}
					else
					{
						queuePush(nmi->receiveQueue, (void *)message);
					}
				}
				pthread_cond_signal(&nmi->recvCondition);
				nmi->receiveCount++;
			}
			else
			{
				jausMessageDestroy(message);
			}
		}
	}
	
	free(packet->buffer);

	datagramPacketDestroy(packet);

	nmi->receiveThreadRunning = 0;
	
	return NULL;
}

JausAddressList *nodeManagerGetComponentAddressList(NodeManagerInterface nmi, unsigned char componentId)
{	
	JausAddressList *addressList = NULL;
	JausAddressList *currentAddress = NULL;
	DatagramPacket packet;

	if(!nmi || !nmi->isOpen || !componentId)
	{
		return NULL;
	}

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0] = INTERFACE_MESSAGE_GET_COMPONENT_ADDRESS_LIST;
	packet->buffer[1] = componentId;
	packet->buffer[2] = 0;
	packet->buffer[3] = 0;
	packet->buffer[4] = 0;

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;
	
	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	while(packet->buffer[0] == INTERFACE_MESSAGE_COMPONENT_ADDRESS_LIST_RESPONSE && packet->buffer[4])
	{
		if(currentAddress)
		{
			currentAddress->nextAddress = (JausAddressList *)malloc(sizeof(JausAddressList));
			currentAddress = currentAddress->nextAddress;
			currentAddress->address = jausAddressCreate();
			currentAddress->address->instance = packet->buffer[1];
			currentAddress->address->component = packet->buffer[2];
			currentAddress->address->node = packet->buffer[3];
			currentAddress->address->subsystem = packet->buffer[4];			
			currentAddress->nextAddress = NULL;
		}
		else
		{
			addressList = (JausAddressList *)malloc(sizeof(JausAddressList));
			currentAddress = addressList;
			currentAddress->address = jausAddressCreate();
			currentAddress->address->instance = packet->buffer[1];
			currentAddress->address->component = packet->buffer[2];
			currentAddress->address->node = packet->buffer[3];
			currentAddress->address->subsystem = packet->buffer[4];			
			currentAddress->nextAddress = NULL;
		}

		packet->buffer[0] = INTERFACE_MESSAGE_GET_COMPONENT_ADDRESS_LIST;
		packet->buffer[1] = 0;
		packet->buffer[2] = 0;
		packet->buffer[3] = 0;
		packet->buffer[4] = 0;

		datagramSocketSend(nmi->interfaceSocket, packet);
		datagramSocketReceive(nmi->interfaceSocket, packet);
	}
	
	free(packet->buffer);
	datagramPacketDestroy(packet);

	return addressList;	
}

JausBoolean nodeManagerLookupAddress(NodeManagerInterface nmi, JausAddress lookupAddress)
{	
	DatagramPacket packet;

	if(!nmi || !nmi->isOpen)
	{
		return JAUS_FALSE;
	}

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0] = INTERFACE_MESSAGE_LOOKUP_ADDRESS;
	packet->buffer[1] = lookupAddress->instance;
	packet->buffer[2] = lookupAddress->component;
	packet->buffer[3] = lookupAddress->node;
	packet->buffer[4] = lookupAddress->subsystem;

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;
	
	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	if(	packet->buffer[0] == INTERFACE_MESSAGE_LOOKUP_ADDRESS_RESPONSE && packet->buffer[5])
	{
		lookupAddress->instance = packet->buffer[1];
		lookupAddress->component = packet->buffer[2];
		lookupAddress->node = packet->buffer[3];
		lookupAddress->subsystem = packet->buffer[4];			
		
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return JAUS_TRUE;
	}
	else
	{
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return JAUS_FALSE;
	}
}

JausAddressList *nodeManagerLookupServiceAddressList(NodeManagerInterface nmi, JausAddress lookupAddress, unsigned short commandCode, int serviceCommandType)
{	
	JausAddressList *addressList = NULL;
	JausAddressList *currentAddress = NULL;
	DatagramPacket packet;

	if(!nmi || !nmi->isOpen || !commandCode)
	{
		return NULL;
	}

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0] = INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS_LIST;
	packet->buffer[1] = lookupAddress->instance;
	packet->buffer[2] = lookupAddress->component;
	packet->buffer[3] = lookupAddress->node;
	packet->buffer[4] = lookupAddress->subsystem;
	packet->buffer[5] = (unsigned char)(commandCode & 0xFF);
	packet->buffer[6] = (unsigned char)((commandCode & 0xFF00) >> 8);
	packet->buffer[7] = (unsigned char)(serviceCommandType & 0xFF);

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;
	
	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	while(packet->buffer[0] == INTERFACE_MESSAGE_COMPONENT_ADDRESS_LIST_RESPONSE && packet->buffer[5])
	{
		if(currentAddress)
		{
			currentAddress->nextAddress = (JausAddressList *)malloc(sizeof(JausAddressList));
			currentAddress = currentAddress->nextAddress;
			currentAddress->address = jausAddressCreate();
			currentAddress->address->instance = packet->buffer[1];
			currentAddress->address->component = packet->buffer[2];
			currentAddress->address->node = packet->buffer[3];
			currentAddress->address->subsystem = packet->buffer[4];			
			currentAddress->nextAddress = NULL;
		}
		else
		{
			addressList = (JausAddressList *)malloc(sizeof(JausAddressList));
			currentAddress = addressList;
			currentAddress->address = jausAddressCreate();
			currentAddress->address->instance = packet->buffer[1];
			currentAddress->address->component = packet->buffer[2];
			currentAddress->address->node = packet->buffer[3];
			currentAddress->address->subsystem = packet->buffer[4];			
			currentAddress->nextAddress = NULL;
		}

		packet->buffer[0] = INTERFACE_MESSAGE_GET_COMPONENT_ADDRESS_LIST;
		packet->buffer[1] = lookupAddress->instance;
		packet->buffer[2] = lookupAddress->component;
		packet->buffer[3] = lookupAddress->node;
		packet->buffer[4] = lookupAddress->subsystem;
		packet->buffer[5] = 0;
		packet->buffer[6] = 0;
		packet->buffer[7] = (unsigned char)(serviceCommandType & 0xFF);

		datagramSocketSend(nmi->interfaceSocket, packet);
		datagramSocketReceive(nmi->interfaceSocket, packet);
	}
	
	free(packet->buffer);
	datagramPacketDestroy(packet);

	return addressList;	
}

JausBoolean nodeManagerLookupServiceAddress(NodeManagerInterface nmi, JausAddress lookupAddress, unsigned short commandCode, int serviceCommandType)
{	
	DatagramPacket packet;

	if(!nmi || !nmi->isOpen)
	{
		return JAUS_FALSE;
	}

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0] = INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS;
	packet->buffer[1] = lookupAddress->instance;
	packet->buffer[2] = lookupAddress->component;
	packet->buffer[3] = lookupAddress->node;
	packet->buffer[4] = lookupAddress->subsystem;
	packet->buffer[5] = (unsigned char)(commandCode & 0xFF);
	packet->buffer[6] = (unsigned char)((commandCode & 0xFF00) >> 8);
	packet->buffer[7] = (unsigned char)(serviceCommandType & 0xFF);

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;
	
	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	if(	packet->buffer[0] == INTERFACE_MESSAGE_LOOKUP_SERVICE_ADDRESS_RESPONSE && packet->buffer[5])
	{
		lookupAddress->instance = packet->buffer[1];
		lookupAddress->component = packet->buffer[2];
		lookupAddress->node = packet->buffer[3];
		lookupAddress->subsystem = packet->buffer[4];			
		
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return JAUS_TRUE;
	}
	else
	{
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return JAUS_FALSE;
	}
}

void nodeManagerDestroyAddressList(JausAddressList *addressList)
{
	JausAddressList *deadAddressList;

	while(addressList)
	{
		deadAddressList = addressList;
		addressList = addressList->nextAddress;
		
		jausAddressDestroy(deadAddressList->address);
		free(deadAddressList);
	}
}

int nodeManagerVerifyAddress(NodeManagerInterface nmi, JausAddress address)
{
	DatagramPacket packet;
	int response = 0;

	packet = datagramPacketCreate();

	packet->bufferSizeBytes = INTERFACE_MESSAGE_SIZE_BYTES;
	packet->buffer = (unsigned char *)malloc(packet->bufferSizeBytes);
	memset(packet->buffer, 0, packet->bufferSizeBytes);	
	packet->buffer[0] = INTERFACE_MESSAGE_VERIFY_ADDRESS;
	packet->buffer[1] = (unsigned char)address->instance;
	packet->buffer[2] = (unsigned char)address->component;
	packet->buffer[3] = (unsigned char)address->node;
	packet->buffer[4] = (unsigned char)address->subsystem;

	packet->port = NODE_MANAGER_INTERFACE_PORT;
	packet->address->value = nmi->ipAddress->value;
	
	datagramSocketSend(nmi->interfaceSocket, packet);
	datagramSocketReceive(nmi->interfaceSocket, packet);

	if(packet->buffer[0] == INTERFACE_MESSAGE_ADDRESS_VERIFIED)
	{
		response = packet->buffer[1];

		free(packet->buffer);
		datagramPacketDestroy(packet);
		return response;
	}
	else
	{
		free(packet->buffer);
		datagramPacketDestroy(packet);
		return 0;
	}
		
}

int nodeManagerReceive(NodeManagerInterface nmi, JausMessage *message)
{
	if(nmi->isOpen && nmi->receiveQueue->size)
	{
		*message = (JausMessage)queuePop(nmi->receiveQueue);
		return 1;		
	}
	else
	{
		return 0;
	}
}

int nodeManagerTimedReceive(NodeManagerInterface nmi, JausMessage *message, double timeLimitSec)
{
	pthread_mutex_t recvMutex = PTHREAD_MUTEX_INITIALIZER;
	int condition = -1;
	struct timespec timeLimitSpec;
		
	if(nmi->isOpen)
	{
		if(ojGetTimeSec() > timeLimitSec)
		{
			return NMI_RECEIVE_TIMED_OUT;			
		}
		else if(nmi->receiveQueue->size)
		{
			*message = (JausMessage)queuePop(nmi->receiveQueue);
			return NMI_MESSAGE_RECEIVED;
		}
		else
		{
			timeLimitSpec.tv_sec = (long)timeLimitSec;
			timeLimitSpec.tv_nsec = (long)(1e9 * (timeLimitSec - (double)timeLimitSpec.tv_sec));

			pthread_mutex_lock(&recvMutex);
			condition = pthread_cond_timedwait(&nmi->recvCondition, &recvMutex, &timeLimitSpec);
			pthread_mutex_unlock(&recvMutex);
			
			switch(condition)
			{
				case 0: // Conditional Signaled
					*message = (JausMessage)queuePop(nmi->receiveQueue);
					return NMI_MESSAGE_RECEIVED;
				
				case ETIMEDOUT: // our time is up
					return NMI_RECEIVE_TIMED_OUT;
					
				default: // Some other error occured
					return NMI_CONDITIONAL_WAIT_ERROR;
			}
		}
	}
	else
	{
		return NMI_CLOSED_ERROR;
	}
}

int nodeManagerSend(NodeManagerInterface nmi, JausMessage message)
{
	int result = -1;
	
	if(nmi->isOpen)
	{
		result = lmHandlerSendLargeMessage(nmi, message);
	}
		
	return result;
}

int nodeManagerSendSingleMessage(NodeManagerInterface nmi, JausMessage message)
{
	DatagramPacket packet;
	int result = -1;
	
	if(nmi->isOpen)
	{
		packet = datagramPacketCreate();
		packet->bufferSizeBytes = (int)jausMessageSize(message);
		packet->buffer = (unsigned char*)malloc(packet->bufferSizeBytes);
		packet->port = NODE_MANAGER_MESSAGE_PORT;
		packet->address->value = nmi->ipAddress->value;
		memset(packet->buffer, 0, packet->bufferSizeBytes);
		
		if(jausMessageToBuffer(message, packet->buffer, packet->bufferSizeBytes))
		{
			result = datagramSocketSend(nmi->messageSocket, packet);
		}
		free(packet->buffer);
		datagramPacketDestroy(packet);
	}
		
	return result;
}

void nodeManagerSendCoreServiceConnections(NodeManagerInterface nmi)
{
	JausMessage message = NULL;
	ServiceConnection scList = NULL;
	ServiceConnection sc;
	ReportComponentStatusMessage reportStatus;	
	ReportComponentAuthorityMessage reportAuthority;	

	// Respond to a ReportComponentStatus Service Connection
	scList = scManagerGetSendList(nmi, JAUS_REPORT_COMPONENT_STATUS);
	if(scList)
	{
		reportStatus = reportComponentStatusMessageCreate();
		jausAddressCopy(reportStatus->source, nmi->cmpt->address);
		reportStatus->properties.scFlag = JAUS_SERVICE_CONNECTION_MESSAGE;
		reportStatus->primaryStatusCode = nmi->cmpt->state;

		sc = scList;
		while(sc)
		{
				jausAddressCopy(reportStatus->destination, sc->address);
				reportStatus->sequenceNumber = sc->sequenceNumber;

				message = reportComponentStatusMessageToJausMessage(reportStatus);
				nodeManagerSend(nmi, message);
				jausMessageDestroy(message);

				sc = sc->nextSc;
		}
		reportComponentStatusMessageDestroy(reportStatus);
	}
	scManagerDestroySendList(scList);

	scList = NULL;
	// Respond to a ReportComponentAuthorityMessage Service Connection
	scList = scManagerGetSendList(nmi, JAUS_REPORT_COMPONENT_AUTHORITY);
	if(scList)
	{
		reportAuthority = reportComponentAuthorityMessageCreate();
		jausAddressCopy(reportAuthority->source, nmi->cmpt->address);
		reportAuthority->properties.scFlag = JAUS_SERVICE_CONNECTION_MESSAGE;
		reportAuthority->authorityCode = nmi->cmpt->authority;

		sc = scList;
		while(sc)
		{
				jausAddressCopy(reportAuthority->destination, sc->address);
				reportAuthority->sequenceNumber = sc->sequenceNumber;

				message = reportComponentAuthorityMessageToJausMessage(reportAuthority);
				nodeManagerSend(nmi, message);
				jausMessageDestroy(message);

				sc = sc->nextSc;
		}
		reportComponentAuthorityMessageDestroy(reportAuthority);
	}
	scManagerDestroySendList(scList);
}