scManager.c 27.4 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 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
/*****************************************************************************
 *  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:		scManager.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:	Provides the core service connection management routines

#include <stdlib.h>
#include <pthread.h>
#include "nodeManagerInterface/nodeManagerInterface.h"

SupportedScMessage scFindSupportedScMsgInList(SupportedScMessage, unsigned short);
ServiceConnection scFindScInList(ServiceConnection, ServiceConnection);
int scGetAvailableInstanceId(ServiceConnection, ServiceConnection);

ServiceConnection serviceConnectionCreate(void)
{
	ServiceConnection sc;
	
	sc = (ServiceConnection) malloc(sizeof(ServiceConnectionStruct));
	if(sc)
	{
		sc->address = jausAddressCreate();
		if(!sc->address)
		{
			// Failed to init address
			free(sc);
			return NULL;
		}
		
		sc->queue = queueCreate();
		if(!sc->queue)
		{
			// Failed to init queue
			jausAddressDestroy(sc->address);
			free(sc);
			return NULL;
		}
				
		return sc;
	}
	else
	{
		return NULL;
	}
}

void serviceConnectionDestroy(ServiceConnection sc)
{
	jausAddressDestroy(sc->address);
	queueDestroy(sc->queue, (void *)jausMessageDestroy);
	free(sc);
}

ServiceConnectionManager scManagerCreate(void)
{
	ServiceConnectionManager scm = NULL;
	int retVal;

	scm = (ServiceConnectionManager)malloc(sizeof(ServiceConnectionManagerStruct));
	if(scm == NULL)
	{
		return NULL;
	}
	
	scm->supportedScMsgList = NULL;
	scm->incomingSc = NULL;
	scm->supportedScMsgCount = 0;
	scm->outgoingScCount = 0;
	scm->incomingScCount = 0;
	
	retVal = pthread_mutex_init(&scm->mutex, NULL);
	if(retVal != 0)
	{
		// Failed to init mutex
		free(scm);
		return NULL;
	}
	
	return scm;	
}

void scManagerDestroy(ServiceConnectionManager scm)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	
	// Only attempt to destroy the scm if it is a non-NULL pointer
	if(scm == NULL)
	{
		return;
	}
	
	// Free the supported message list
	while(scm->supportedScMsgList)
	{
		supportedScMsg = scm->supportedScMsgList;
		scm->supportedScMsgList = supportedScMsg->nextSupportedScMsg;
		
		// Free all the service connections
		while(supportedScMsg->scList)
		{
			sc = supportedScMsg->scList;
			supportedScMsg->scList = sc->nextSc;
			serviceConnectionDestroy(sc);
		}
		
		free(supportedScMsg);
	}
	
	pthread_mutex_destroy(&scm->mutex);
	free(scm);	
}

void scManagerProcessConfirmScMessage(NodeManagerInterface nmi, ConfirmServiceConnectionMessage message)
{
	ServiceConnection prevSc = NULL;
	ServiceConnection sc;
	TerminateServiceConnectionMessage terminateSc;	
	JausMessage txMessage;
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	sc = nmi->scm->incomingSc;
	
	while(sc)
	{
		if(	sc->commandCode == message->serviceConnectionCommandCode && jausAddressEqual(sc->address, message->source) )
		{
			if( message->responseCode == JAUS_SC_SUCCESSFUL )
			{
				sc->confirmedUpdateRateHz = message->confirmedPeriodicUpdateRateHertz;
				sc->instanceId = message->instanceId;
				sc->isActive = JAUS_TRUE;
				sc->sequenceNumber = 65535;
				sc->lastSentTime = ojGetTimeSec();
			}
			else
			{
				// Set SC Inactive
				sc->isActive = JAUS_FALSE;

				// Remove Service Connection
				if(prevSc)
				{
					prevSc->nextSc = sc->nextSc;
					sc->nextSc = NULL;
				}
				else
				{
					nmi->scm->incomingSc = sc->nextSc;
					sc->nextSc = NULL;
				}
				nmi->scm->incomingScCount--;
			}
			
			pthread_mutex_unlock(&nmi->scm->mutex);
			return;
		}
		prevSc = sc;
		sc = sc->nextSc;
	}

	// The SC was not found, so send a terminate to prevent streaming
	if( message->responseCode == JAUS_SC_SUCCESSFUL )
	{
		terminateSc = terminateServiceConnectionMessageCreate();
		jausAddressCopy(terminateSc->source, nmi->cmpt->address);
		jausAddressCopy(terminateSc->destination, message->source);
		terminateSc->serviceConnectionCommandCode = message->serviceConnectionCommandCode;
		terminateSc->instanceId = message->instanceId;
		
		txMessage = terminateServiceConnectionMessageToJausMessage(terminateSc);
		nodeManagerSend(nmi, txMessage);
		jausMessageDestroy(txMessage);
	
		terminateServiceConnectionMessageDestroy(terminateSc);
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerProcessCreateScMessage(NodeManagerInterface nmi, CreateServiceConnectionMessage message)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection newSc;
	JausMessage txMessage;
	ConfirmServiceConnectionMessage confScMsg;
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, message->serviceConnectionCommandCode);
	if(supportedScMsg == NULL)
	{
		confScMsg = confirmServiceConnectionMessageCreate();
		jausAddressCopy(confScMsg->source, nmi->cmpt->address);
		jausAddressCopy(confScMsg->destination, message->source);
		confScMsg->serviceConnectionCommandCode = message->serviceConnectionCommandCode;
		confScMsg->instanceId = 0;
		confScMsg->confirmedPeriodicUpdateRateHertz = 0;
		confScMsg->responseCode = JAUS_SC_COMPONENT_NOT_CAPABLE;
	
		txMessage = confirmServiceConnectionMessageToJausMessage(confScMsg); 
		nodeManagerSend(nmi, txMessage);
		jausMessageDestroy(txMessage);	
	
		confirmServiceConnectionMessageDestroy(confScMsg);
		
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}
	
	newSc = (ServiceConnection)malloc( sizeof(ServiceConnectionStruct) );
	if(newSc == NULL) 
	{
		// Send negative conf (could not create sc)
		confScMsg = confirmServiceConnectionMessageCreate();
		jausAddressCopy(confScMsg->source, nmi->cmpt->address);
		jausAddressCopy(confScMsg->destination, message->source);
		confScMsg->serviceConnectionCommandCode = message->serviceConnectionCommandCode;
		confScMsg->instanceId = 0;
		confScMsg->confirmedPeriodicUpdateRateHertz = 0;
		confScMsg->responseCode = JAUS_SC_CONNECTION_REFUSED;
	
		txMessage = confirmServiceConnectionMessageToJausMessage(confScMsg); 
		nodeManagerSend(nmi, txMessage);
		jausMessageDestroy(txMessage);	
	
		confirmServiceConnectionMessageDestroy(confScMsg);
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}
	
	newSc->commandCode = message->serviceConnectionCommandCode;
	newSc->presenceVector = message->presenceVector;
	newSc->address = jausAddressCreate();
	jausAddressCopy(newSc->address, message->source);
	newSc->instanceId = scGetAvailableInstanceId(supportedScMsg->scList, newSc);
	if(newSc->instanceId == -1)
	{
		// Send negative conf (could not create sc)
		confScMsg = confirmServiceConnectionMessageCreate();
		jausAddressCopy(confScMsg->source, nmi->cmpt->address);
		jausAddressCopy(confScMsg->destination, message->source);
		confScMsg->serviceConnectionCommandCode = message->serviceConnectionCommandCode;
		confScMsg->instanceId = 0;
		confScMsg->confirmedPeriodicUpdateRateHertz = 0;
		confScMsg->responseCode = JAUS_SC_CONNECTION_REFUSED;
	
		txMessage = confirmServiceConnectionMessageToJausMessage(confScMsg); 
		nodeManagerSend(nmi, txMessage);
		jausMessageDestroy(txMessage);	
	
		confirmServiceConnectionMessageDestroy(confScMsg);

		jausAddressDestroy(newSc->address);
		free(newSc);
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}
	newSc->queue = NULL;
	newSc->queueSize = 0;

	sc = scFindScInList(supportedScMsg->scList, newSc);
	if(sc == NULL) // Test to see if the sc does not already exist	
	{
		// The sc doesent exist, so we insert the new one into the list
		sc = newSc;
		sc->nextSc = supportedScMsg->scList;
		supportedScMsg->scList = sc;
		nmi->scm->outgoingScCount++;
	}
	else
	{
		jausAddressDestroy(newSc->address);
		free(newSc);
	}

	sc->requestedUpdateRateHz = message->requestedPeriodicUpdateRateHertz;
	sc->lastSentTime = 0.0;
	sc->sequenceNumber = 0;
	sc->isActive = JAUS_TRUE;
	sc->confirmedUpdateRateHz = message->requestedPeriodicUpdateRateHertz;	// TODO: calculate confirmedUpdateRateHz
	
	confScMsg = confirmServiceConnectionMessageCreate();
	jausAddressCopy(confScMsg->source, nmi->cmpt->address);
	jausAddressCopy(confScMsg->destination, message->source);
	confScMsg->serviceConnectionCommandCode = sc->commandCode;
	confScMsg->instanceId = (JausByte)sc->instanceId;
	confScMsg->confirmedPeriodicUpdateRateHertz = sc->confirmedUpdateRateHz;
	confScMsg->responseCode = JAUS_SC_SUCCESSFUL;

	txMessage = confirmServiceConnectionMessageToJausMessage(confScMsg); 
	nodeManagerSend(nmi, txMessage);
	jausMessageDestroy(txMessage);	

	confirmServiceConnectionMessageDestroy(confScMsg);
	
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerProcessActivateScMessage(NodeManagerInterface nmi, ActivateServiceConnectionMessage message)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection messageSc;

	pthread_mutex_lock(&nmi->scm->mutex);

	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, message->serviceConnectionCommandCode);
	if(supportedScMsg == NULL)
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	messageSc = (ServiceConnection)malloc( sizeof(ServiceConnectionStruct) );
	if(messageSc == NULL) 
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	messageSc->address = jausAddressCreate();
	jausAddressCopy(messageSc->address, message->source);
	messageSc->instanceId = message->instanceId;

	sc = scFindScInList(supportedScMsg->scList, messageSc);
	if(sc != NULL)
	{
		sc->isActive = JAUS_TRUE;
	}
		
	jausAddressDestroy(messageSc->address);
	free(messageSc);
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerProcessSuspendScMessage(NodeManagerInterface nmi, SuspendServiceConnectionMessage message)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection messageSc;

	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, message->serviceConnectionCommandCode);
	if(supportedScMsg == NULL)
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	messageSc = (ServiceConnection)malloc( sizeof(ServiceConnectionStruct) );
	if(messageSc == NULL) 
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	messageSc->address = jausAddressCreate();
	jausAddressCopy(messageSc->address, message->source);
	messageSc->instanceId = message->instanceId;

	sc = scFindScInList(supportedScMsg->scList, messageSc);
	if(sc != NULL)
	{
		sc->isActive = JAUS_FALSE;
	}
	
	jausAddressDestroy(messageSc->address);
	free(messageSc);
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerProcessTerminateScMessage(NodeManagerInterface nmi, TerminateServiceConnectionMessage message)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection prevSc;
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, message->serviceConnectionCommandCode);
	if(supportedScMsg == NULL)
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	if( supportedScMsg->scList == NULL )
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}

	sc = supportedScMsg->scList;
	
	if(	jausAddressEqual(sc->address, message->source) &&
		sc->commandCode == message->serviceConnectionCommandCode &&
		sc->instanceId == message->instanceId )
	{
		// Remove sc from list
		supportedScMsg->scList = sc->nextSc;
		serviceConnectionDestroy(sc);
		nmi->scm->outgoingScCount--;
		pthread_mutex_unlock(&nmi->scm->mutex);
		return;
	}
	
	prevSc = sc;
	sc = prevSc->nextSc;
	while(sc)
	{
		if(	jausAddressEqual(sc->address, message->source) &&
			sc->commandCode == message->serviceConnectionCommandCode &&
			sc->instanceId == message->instanceId )
		{
			// Remove sc from list
			prevSc->nextSc = sc->nextSc;
			serviceConnectionDestroy(sc);
			nmi->scm->outgoingScCount--;
			pthread_mutex_unlock(&nmi->scm->mutex);
			return;
		}	
		prevSc = sc;
		sc = sc->nextSc;
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerProcessUpdatedSubystem(NodeManagerInterface nmi, JausSubsystem subsystem)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection prevSc;

	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = nmi->scm->supportedScMsgList;
	while(supportedScMsg)
	{
		prevSc = NULL;
		sc = supportedScMsg->scList;
		while(sc)
		{
			if(	!nodeManagerVerifyAddress(nmi, sc->address) )
			{
				// Remove sc from list
				if(prevSc)
				{
					prevSc->nextSc = sc->nextSc;
					serviceConnectionDestroy(sc);
					sc = prevSc->nextSc;
				}
				else
				{
					supportedScMsg->scList = sc->nextSc;
					serviceConnectionDestroy(sc);
					sc = supportedScMsg->scList;
				}
				nmi->scm->outgoingScCount--;
			}
			else
			{
				prevSc = sc;	
				sc = sc->nextSc;
			}
		}
		supportedScMsg = supportedScMsg->nextSupportedScMsg;
	}

	prevSc = NULL;
	sc = nmi->scm->incomingSc;
	while(sc)
	{
		if( !nodeManagerVerifyAddress(nmi, sc->address) )
		{
			// Remove sc from list
			sc->isActive = JAUS_FALSE;			

			// Clear out Inbound Queue
			queueEmpty(sc->queue, (void *)jausMessageDestroy);
			
			if(prevSc)
			{
				prevSc->nextSc = sc->nextSc;
				sc->nextSc = NULL;
				sc = prevSc->nextSc;
			}
			else
			{
				nmi->scm->incomingSc = sc->nextSc;
				sc->nextSc = NULL;
				sc = nmi->scm->incomingSc;
			}
			nmi->scm->incomingScCount--;
		}
		else
		{
			prevSc = sc;
			sc = sc->nextSc;
		}
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);	
}

void scManagerAddSupportedMessage(NodeManagerInterface nmi, unsigned short commandCode)
{
	SupportedScMessage supportedScMsg;

	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, commandCode);
	if(supportedScMsg == NULL)
	{																					
		supportedScMsg = (SupportedScMessage)malloc(sizeof(struct SupportedScMessageStruct));
		if(supportedScMsg == NULL) 
		{
			// TODO: Throw error
			pthread_mutex_unlock(&nmi->scm->mutex);
			return;
		}

		supportedScMsg->commandCode = commandCode; 
		supportedScMsg->scList = NULL;
		
		supportedScMsg->nextSupportedScMsg = nmi->scm->supportedScMsgList;
		nmi->scm->supportedScMsgList = supportedScMsg;
		nmi->scm->supportedScMsgCount++;
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);
}

void scManagerRemoveSupportedMessage(NodeManagerInterface nmi, unsigned short commandCode)
{
	SupportedScMessage prevSupportedScMsg = NULL;
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	TerminateServiceConnectionMessage terminateSc;	
	JausMessage txMessage;
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	supportedScMsg = nmi->scm->supportedScMsgList;
	
	while(supportedScMsg)
	{
		if(supportedScMsg->commandCode == commandCode)
		{
			// Remove Service Connection
			if(prevSupportedScMsg)
			{
				prevSupportedScMsg->nextSupportedScMsg = supportedScMsg->nextSupportedScMsg;
			}
			else
			{
				nmi->scm->supportedScMsgList = supportedScMsg->nextSupportedScMsg;
			}
			
			// Terminate and free all the service connections
			while(supportedScMsg->scList)
			{
				sc = supportedScMsg->scList;
				supportedScMsg->scList = sc->nextSc;
				
				terminateSc = terminateServiceConnectionMessageCreate();
				jausAddressCopy(terminateSc->source, nmi->cmpt->address);
				jausAddressCopy(terminateSc->destination, sc->address);
				terminateSc->serviceConnectionCommandCode = sc->commandCode;
				terminateSc->instanceId = sc->instanceId;
	
				txMessage = terminateServiceConnectionMessageToJausMessage(terminateSc);
				nodeManagerSend(nmi, txMessage);
				jausMessageDestroy(txMessage);

				terminateServiceConnectionMessageDestroy(terminateSc);

				serviceConnectionDestroy(sc);
			}
			free(supportedScMsg);
			nmi->scm->supportedScMsgCount--;
			pthread_mutex_unlock(&nmi->scm->mutex);
			return;
		}
		prevSupportedScMsg = supportedScMsg;
		supportedScMsg = supportedScMsg->nextSupportedScMsg;
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);
}

JausBoolean scManagerQueryActiveMessage(NodeManagerInterface nmi, unsigned short commandCode)
{
	SupportedScMessage supportedScMsg;

	pthread_mutex_lock(&nmi->scm->mutex);	
	
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, commandCode);

	pthread_mutex_unlock(&nmi->scm->mutex);	
	
	if(supportedScMsg != NULL && supportedScMsg->scList != NULL)
	{
		return JAUS_TRUE;
	}
	else
	{
		return JAUS_FALSE;
	}
}

ServiceConnection scManagerGetSendList(NodeManagerInterface nmi, unsigned short commandCode)
{
	SupportedScMessage supportedScMsg;
	ServiceConnection sc;
	ServiceConnection newSc = NULL;
	ServiceConnection firstSc = NULL;
	double currentTime = ojGetTimeSec();

	pthread_mutex_lock(&nmi->scm->mutex);	
	
	// find the SC object associated with this command code
	supportedScMsg = scFindSupportedScMsgInList(nmi->scm->supportedScMsgList, commandCode);
	if(supportedScMsg)
	{
		sc = supportedScMsg->scList;
	}
	else
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return NULL;
	}
	
	while(sc)
	{
		// Check for update rate
		if(sc->isActive && sc->lastSentTime < (currentTime - 1.0/sc->confirmedUpdateRateHz))
		{
			sc->lastSentTime = currentTime;
			if(newSc == NULL)
			{
				newSc = (ServiceConnection)malloc(sizeof(ServiceConnectionStruct));
				firstSc = newSc;
			}
			else
			{
				newSc->nextSc = (ServiceConnection)malloc(sizeof(ServiceConnectionStruct));
				newSc = newSc->nextSc;
			}
			
			*newSc = *sc;
			newSc->nextSc = NULL;
			sc->sequenceNumber++;
		}

		sc = sc->nextSc;
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);
	return firstSc;
}

void scManagerDestroySendList(ServiceConnection sc)
{
	ServiceConnection deadSc;
	
	while(sc)
	{
		deadSc = sc;
		sc = sc->nextSc;
		free(deadSc);
	}
}

SupportedScMessage scFindSupportedScMsgInList(SupportedScMessage supportedScMsg, unsigned short commandCode)
{	
	while(supportedScMsg)
	{
		if(supportedScMsg->commandCode == commandCode)
		{
			return supportedScMsg;
		}
		supportedScMsg = supportedScMsg->nextSupportedScMsg;
	}
	
	return NULL;
}

ServiceConnection scFindScInList(ServiceConnection sc, ServiceConnection newSc)
{
	// NOTE: destination, commandCode and presenceVector define the sc equals (==) functionality
	//		 the updateRate is not included 
	
	while(sc)
	{
		if(	jausAddressEqual(sc->address, newSc->address) && sc->instanceId == newSc->instanceId )
		{
			return sc;
		}		
		sc = sc->nextSc;
	}
	
	return NULL;
}

int scGetAvailableInstanceId(ServiceConnection sc, ServiceConnection newSc)
{
	int i;
	unsigned char instanceAvailable[256];
	
	memset(instanceAvailable, 1, 256);
	
	while(sc)
	{
		if(jausAddressEqual(sc->address, newSc->address))
		{
			if(sc->presenceVector == newSc->presenceVector)
			{
				return sc->instanceId;
			}
			else
			{
				instanceAvailable[sc->instanceId] = 0;
			}
		}
		sc = sc->nextSc;
	}
	
	for(i = 0; i<256; i++)
	{
		if(instanceAvailable[i])
		{
			return i;
		}
	}

	return -1;
}

JausBoolean scManagerCreateServiceConnection(NodeManagerInterface nmi, ServiceConnection sc)
{
	CreateServiceConnectionMessage createSc;
	JausMessage txMessage;
	JausAddress localAddress;
	
	ServiceConnection prevSc = NULL;
	ServiceConnection testSc = NULL;
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	testSc = nmi->scm->incomingSc;
	
	if(!sc)
	{
		pthread_mutex_unlock(&nmi->scm->mutex);
		return JAUS_FALSE;
	}
	
	// Remove this service connection from the list of incoming service connections
	while(testSc)
	{                      
		if(sc == testSc)
		{               
			if(prevSc)
			{
				prevSc->nextSc = testSc->nextSc;
			}
			else
			{
		        nmi->scm->incomingSc = testSc->nextSc;
			}
			nmi->scm->incomingScCount--;
		}
		
		prevSc = testSc;
		testSc = testSc->nextSc;
	}
	
	sc->confirmedUpdateRateHz = 0;
	sc->lastSentTime = 0;
	sc->sequenceNumber = 65535;
	sc->instanceId = -1;
	sc->isActive = JAUS_FALSE;
	sc->nextSc = NULL;

	createSc = createServiceConnectionMessageCreate();
	jausAddressCopy(createSc->source, nmi->cmpt->address);
	createSc->serviceConnectionCommandCode = sc->commandCode;
	createSc->requestedPeriodicUpdateRateHertz = sc->requestedUpdateRateHz;
	createSc->presenceVector = sc->presenceVector;
	
	// If the subsystem for this service connection is known
	if(sc->address && sc->address->subsystem != 0)
	{
		jausAddressCopy(createSc->destination, sc->address);
			
		txMessage = createServiceConnectionMessageToJausMessage(createSc);
		nodeManagerSend(nmi, txMessage);
		jausMessageDestroy(txMessage);
		createServiceConnectionMessageDestroy(createSc);

		// Add the service connection to the front of the incoming service connection list
		sc->nextSc = nmi->scm->incomingSc;
		nmi->scm->incomingSc = sc;		
		nmi->scm->incomingScCount++;
		
		pthread_mutex_unlock(&nmi->scm->mutex);
		return JAUS_TRUE;
	}
	// otherwise the subsystem is unknown so we assume it is the same subsystem?
	else
	{		
		localAddress = jausAddressCreate();
		localAddress->subsystem = nmi->cmpt->address->subsystem;
		localAddress->node = 0;
		localAddress->component = sc->address->component;
		localAddress->instance = 0;
		
		// Lookup Address from nodeManager
		// Tests if the target component exists or not
		if(nodeManagerLookupAddress(nmi, localAddress))
		{
			jausAddressCopy(createSc->destination, localAddress);
			jausAddressCopy(sc->address, localAddress);
				
			txMessage = createServiceConnectionMessageToJausMessage(createSc);
			nodeManagerSend(nmi, txMessage);
			jausMessageDestroy(txMessage);
			
			jausAddressDestroy(localAddress);
			createServiceConnectionMessageDestroy(createSc);
	
			// Add the service connection to the front of the incoming service connection list
			sc->nextSc = nmi->scm->incomingSc;
			nmi->scm->incomingSc = sc;		
			nmi->scm->incomingScCount++;
		
			pthread_mutex_unlock(&nmi->scm->mutex);
			return JAUS_TRUE;
		}
		else
		{
			jausAddressDestroy(localAddress);
			createServiceConnectionMessageDestroy(createSc);
			
			pthread_mutex_unlock(&nmi->scm->mutex);
			return JAUS_FALSE;
		}
	}
	
	pthread_mutex_unlock(&nmi->scm->mutex);	
}

JausBoolean scManagerTerminateServiceConnection(NodeManagerInterface nmi, ServiceConnection deadSc)
{
	TerminateServiceConnectionMessage terminateSc;
	JausMessage txMessage;
	ServiceConnection prevSc = NULL;
	ServiceConnection sc = NULL;

	pthread_mutex_lock(&nmi->scm->mutex);
	
	sc = nmi->scm->incomingSc;
	
	while(sc)
	{
		if(sc->commandCode == deadSc->commandCode && jausAddressEqual(sc->address, deadSc->address) )
		{
			if(sc->instanceId > -1)
			{
				terminateSc = terminateServiceConnectionMessageCreate();
				jausAddressCopy(terminateSc->source, nmi->cmpt->address);
				jausAddressCopy(terminateSc->destination, deadSc->address);
				terminateSc->serviceConnectionCommandCode = deadSc->commandCode;
				terminateSc->instanceId = deadSc->instanceId;
	
				txMessage = terminateServiceConnectionMessageToJausMessage(terminateSc);
				nodeManagerSend(nmi, txMessage);
				jausMessageDestroy(txMessage);

				terminateServiceConnectionMessageDestroy(terminateSc);
			}			

			// Set SC to inactive
			sc->isActive = JAUS_FALSE;
			
			// Empty any Remaining Queue
			queueEmpty(sc->queue, (void *)jausMessageDestroy);

			// Remove Service Connection
			if(prevSc)
			{
				prevSc->nextSc = sc->nextSc;
				sc->nextSc = NULL;
			}
			else
			{
				nmi->scm->incomingSc = sc->nextSc;
				sc->nextSc = NULL;
			}
			
			nmi->scm->incomingScCount--;
			pthread_mutex_unlock(&nmi->scm->mutex);
			return JAUS_TRUE;
		}
		else
		{
			prevSc = sc;
			sc = sc->nextSc;
		}
	}
	pthread_mutex_unlock(&nmi->scm->mutex);
	return JAUS_FALSE;
}

JausBoolean scManagerReceiveServiceConnection(NodeManagerInterface nmi, ServiceConnection requestSc, JausMessage *message)
{
	ServiceConnection prevSc;
	ServiceConnection sc;

	pthread_mutex_unlock(&nmi->scm->mutex);	
	
	sc = nmi->scm->incomingSc;
	prevSc = NULL;
	while(sc)
	{
		if(sc->commandCode == requestSc->commandCode && jausAddressEqual(sc->address, requestSc->address) )
		{
			if(ojGetTimeSec() > (sc->lastSentTime + sc->timeoutSec))
			{
				// Connection has Timed Out
				sc->isActive = JAUS_FALSE;
				queueEmpty(sc->queue, (void *)jausMessageDestroy);				

				// Remove Service Connection
				if(prevSc)
				{
					prevSc->nextSc = sc->nextSc;
					sc->nextSc = NULL;
				}
				else
				{
					nmi->scm->incomingSc = sc->nextSc;
					sc->nextSc = NULL;
				}
				nmi->scm->incomingScCount--;
				pthread_mutex_unlock(&nmi->scm->mutex);
				return JAUS_FALSE;
			}

			if(sc->queue->size)
			{
				*message = (JausMessage)queuePop(sc->queue);
				pthread_mutex_unlock(&nmi->scm->mutex);
				return JAUS_TRUE;
			}
			else
			{
				pthread_mutex_unlock(&nmi->scm->mutex);
				return JAUS_FALSE;
			}
		}
		else
		{
			prevSc = sc;
			sc = sc->nextSc;
		}
	}

	pthread_mutex_unlock(&nmi->scm->mutex);
	return JAUS_FALSE;
}

void scManagerReceiveMessage(NodeManagerInterface nmi, JausMessage message)
{
	ServiceConnection sc;
	char string[32] = {0};
	
	pthread_mutex_lock(&nmi->scm->mutex);
	
	sc = nmi->scm->incomingSc;
	while(sc)
	{
		if(sc->commandCode == message->commandCode && jausAddressEqual(sc->address, message->source) )
		{
			if(sc->isActive)
			{
				sc->lastSentTime = ojGetTimeSec();
				
				if(sc->queueSize && sc->queueSize == sc->queue->size)
				{
					jausMessageDestroy(queuePop(sc->queue));
					queuePush(sc->queue, (void *)message);
				}
				else
				{
					queuePush(sc->queue, (void *)message);
				} 
			}			
			else
			{
				// TODO: Error? received a message for inactive SC
				jausMessageDestroy(message);
			}
			pthread_mutex_unlock(&nmi->scm->mutex);
			return;		
		}
		sc = sc->nextSc;
	}

	jausAddressToString(message->source, string);
	jausMessageDestroy(message);
	pthread_mutex_unlock(&nmi->scm->mutex);
}

int scManagerUpdateServiceConnection(ServiceConnection sc, unsigned short sequenceNumber)
{
	int returnValue = JAUS_FALSE;

	sc->lastSentTime = ojGetTimeSec();

	if(sequenceNumber == sc->sequenceNumber)
	{
		returnValue =  SC_ERROR_SEQUENCE_NUMBERS_EQUAL;
	}
	else if(sequenceNumber == 0 && sc->sequenceNumber != 65535)
	{
		returnValue = SC_ERROR_SEQUENCE_NUMBER_OUT_OF_SYNC;
	}
	else if(sequenceNumber - sc->sequenceNumber != 1 && sequenceNumber !=0)
	{
		returnValue = SC_ERROR_SEQUENCE_NUMBER_OUT_OF_SYNC;
	}
	else
	{
		returnValue = JAUS_TRUE;
	}
	
	sc->sequenceNumber = sequenceNumber;
	
	return returnValue;
}