Index: trunk/FACT++/dim/src/dns.c
===================================================================
--- trunk/FACT++/dim/src/dns.c	(revision 18097)
+++ trunk/FACT++/dim/src/dns.c	(revision 18920)
@@ -116,5 +116,5 @@
 		{
 			dim_print_date_time();
-			printf(" Disconnect received - conn: %d to %s@%s\n", conn_id,
+			printf("Disconnect received - conn: %d to %s@%s\n", conn_id,
 				Net_conns[conn_id].task,Net_conns[conn_id].node );
 		}
@@ -126,5 +126,5 @@
 		{
 			dim_print_date_time();
-			printf(" Connection request received - conn: %d\n", conn_id);
+			printf("Connection request received - conn: %d\n", conn_id);
 		}
 		/* handle_conn( conn_id ); */
@@ -141,5 +141,5 @@
 		default:
 			dim_print_date_time();
-			printf(" conn: %d to %s@%s, Bad packet\n", conn_id,
+			printf("conn: %d to %s@%s, Bad packet\n", conn_id,
 				Net_conns[conn_id].task,Net_conns[conn_id].node );
 			printf("packet->size = %d\n", vtohl(packet->size));
@@ -155,5 +155,5 @@
 	default:	
 		dim_print_date_time();
-		printf( " - DIM panic: recv_rout(): Bad switch, exiting...\n");
+		printf( "- DIM panic: recv_rout(): Bad switch, exiting...\n");
 		abort();
 	}
@@ -172,4 +172,139 @@
 	}
 */
+}
+
+void create_service_entry( int conn_id, DIS_DNS_PACKET *packet, int i, int state)
+{
+	DNS_SERVICE *servp;
+	int name_too_long = 0;
+	char *ptrt;
+	void service_insert();
+
+	if((int)strlen(Dns_conns[conn_id].task_name) == MAX_TASK_NAME-4-1)
+		name_too_long = 1;
+	servp = (DNS_SERVICE *)malloc(sizeof(DNS_SERVICE));
+	if(name_too_long)
+	{
+		if(strstr(packet->services[i].service_name,"/CLIENT_LIST"))
+		{
+			strncpy(Dns_conns[conn_id].long_task_name, packet->services[i].service_name,
+				(size_t)MAX_NAME);
+			ptrt = strstr(Dns_conns[conn_id].long_task_name,"/CLIENT_LIST");
+			*ptrt = '\0';
+		}
+	}
+	strncpy( servp->serv_name,
+		packet->services[i].service_name,
+		(size_t)MAX_NAME );
+	strncpy( servp->serv_def,
+		packet->services[i].service_def,
+		(size_t)MAX_NAME );
+	servp->state = state;
+	servp->conn_id = conn_id;
+	servp->server_format = vtohl(packet->format);
+	servp->serv_id = vtohl(packet->services[i].service_id);
+	dll_insert_queue( (DLL *)
+		Dns_conns[conn_id].service_head, 
+		(DLL *) servp );
+	servp->node_head = (RED_NODE *) malloc(sizeof(NODE));
+	dll_init( (DLL *) servp->node_head );
+	servp->next = 0;
+	if(state == 1)
+	{
+		service_insert( &(servp->next) );
+		Dns_conns[conn_id].n_services++;
+	}
+} 
+
+int warn_server(int conn_id)
+{
+	DNS_SERVICE *servp;
+	DNS_DIS_PACKET *dis_packet;
+	int n_dupl, dupl_conn_id;
+	char vname[MAX_NAME], dupl_server[MAX_NAME];
+	int type, size, dupl_id, kill_anyway, ret;
+	char *ptr;
+		
+	ret = 1;
+	type = DNS_DIS_KILL;
+	n_dupl = 0;
+	strcpy(vname, Dns_conns[conn_id].task_name);
+	strcat(vname, "/VERSION_NUMBER");
+	dupl_id = 0;
+	kill_anyway = 0;
+	dupl_conn_id = Dns_conns[conn_id].already;
+	servp = (DNS_SERVICE *)Dns_conns[conn_id].service_head;
+	while( (servp = (DNS_SERVICE *) dll_get_next(
+				(DLL *) Dns_conns[conn_id].service_head,
+				(DLL *) servp)) )
+	{
+		if(servp->state == -2)
+		{
+			if(!dupl_id)
+			{
+				dupl_id = servp->serv_id;
+				sprintf(dupl_server,"%s(%d)@%s:%d",
+					Dns_conns[dupl_conn_id].task_name,
+					Dns_conns[dupl_conn_id].pid,
+					Dns_conns[dupl_conn_id].node_name,
+					Dns_conns[dupl_conn_id].port);
+			}
+			if(!strcmp(servp->serv_name, vname))
+				kill_anyway = 1;
+			n_dupl++;
+		}
+	}
+/* allocate Header + space for <server_name>:<pid>@<node_name>:<port_number> + n duplicated + duplicated ids */
+	dis_packet = (DNS_DIS_PACKET *)malloc(DNS_DIS_HEADER + MAX_NAME*2 + (n_dupl+1)*10);
+	sprintf(dis_packet->dup_info, "%s %d", dupl_server, n_dupl);
+	ptr = dis_packet->dup_info + strlen(dis_packet->dup_info);
+	if(kill_anyway)
+	{
+		type |= (0x01 << (int)16) & (int)0xFFFF0000;
+		dim_print_date_time();
+printf("Server already declared by conn %d - %s@%s:%d (PID %d), Killing server conn %d - %s@%s:%d (PID %d) \n",
+			dupl_conn_id, 
+			Dns_conns[dupl_conn_id].task_name,
+			Dns_conns[dupl_conn_id].node_name,
+			Dns_conns[dupl_conn_id].port,
+			Dns_conns[dupl_conn_id].pid,
+			conn_id,
+			Dns_conns[conn_id].task_name,
+			Dns_conns[conn_id].node_name,
+			Dns_conns[conn_id].port,
+			Dns_conns[conn_id].pid);
+		fflush(stdout);
+	}
+	else
+	{
+		servp = (DNS_SERVICE *)Dns_conns[conn_id].service_head;
+		while( (servp = (DNS_SERVICE *) dll_get_next(
+					(DLL *) Dns_conns[conn_id].service_head,
+					(DLL *) servp)) )
+		{
+			if(servp->state == -2)
+			{
+				dupl_id = servp->serv_id;
+				sprintf(ptr," %08X", dupl_id);
+				ptr += 9;
+			}
+		}
+	}
+	dis_packet->type = htovl(type);
+	size = DNS_DIS_HEADER + strlen(dis_packet->dup_info);
+/* Unfortunatelly the size is wrong, this is for backward compatibility on the server side (it tested the size)*/
+	dis_packet->size = htovl(DNS_DIS_HEADER);
+/*
+printf("Sendig packet %s\n", dis_packet->dup_info);
+*/
+	if( !dna_write_nowait(conn_id, dis_packet, size) )
+	{
+		dim_print_date_time();
+		printf("Kill Server: Couldn't write, releasing %d\n",conn_id);
+		fflush(stdout);
+		ret = 0;
+	}
+	free(dis_packet);
+	return ret;
 }
 
@@ -180,5 +315,5 @@
 	int i, service_id;
 	int n_services;
-	char *ptr, *ptr1, *ptrt;
+	char *ptr, *ptr1;
 	int found;
 	void do_update_did();
@@ -195,11 +330,14 @@
 #endif
 	int update_did = 0;
+/*
 	int name_too_long = 0;
+*/
 	int rem_only = 0;
+	int all_services_registered = -1;
 
 	Dns_conns[conn_id].validity = (int)time(NULL);
 	if( !Dns_conns[conn_id].service_head ) 
 	{
-
+		all_services_registered = 0;
 		if(vtohl(packet->n_services) > 0)
 		{
@@ -212,5 +350,5 @@
 	{
 			dim_print_date_time();
-			printf( " !!!! New Conn %3d : Server %s@%s (PID %d) registering %d services, to delete %d\n",
+			printf( "!!!! New Conn %3d : Server %s@%s (PID %d) registering %d services, to delete %d\n",
 				conn_id, packet->task_name,
 				packet->node_name, 
@@ -246,5 +384,5 @@
 	{
 			dim_print_date_time();
-			printf( " !!!! New Conn %3d : Server %s@%s (PID %d) registered %d services\n",
+			printf( "!!!! New Conn %3d : Server %s@%s (PID %d) registered %d services\n",
 				conn_id, Dns_conns[conn_id].task_name,
 				Dns_conns[conn_id].node_name, 
@@ -293,9 +431,9 @@
 				{
 					dim_print_date_time();
-					printf(" Stop Server: Couldn't write, releasing %d\n",conn_id);
+					printf("Stop Server: Couldn't write, releasing %d\n",conn_id);
 					fflush(stdout);
 				}
 				dim_print_date_time();
-				printf(" Connection from %s refused, stopping server %s\n",
+				printf("Connection from %s refused, stopping server %s\n",
 						Dns_conns[conn_id].node_name, 
 						Dns_conns[conn_id].task_name);
@@ -332,5 +470,5 @@
 				dna_set_test_write(conn_id, dim_get_keepalive_timeout());
 			dim_print_date_time();
-			printf( " Server %s out of error\n",
+			printf( "Server %s out of error\n",
 				Dns_conns[conn_id].task_name );
 			fflush(stdout);
@@ -339,6 +477,8 @@
 	}
 	n_services = vtohl(packet->n_services);
+/*
 	if((int)strlen(Dns_conns[conn_id].task_name) == MAX_TASK_NAME-4-1)
 		name_too_long = 1;
+*/
 	for( i = 0; i < n_services; i++ ) 
 	{
@@ -347,5 +487,5 @@
 	{
 			dim_print_date_time();
-			printf( " Conn %3d : Server %s@%s (PID %d) registered %s\n",
+			printf( "Conn %3d : Server %s@%s (PID %d) registered %s\n",
 				conn_id, Dns_conns[conn_id].task_name,
 				Dns_conns[conn_id].node_name, 
@@ -359,4 +499,5 @@
 			if(!strcmp(packet->services[i].service_name, "DUMMY_UPDATE_PACKET"))
 			{
+				all_services_registered = 1;
 				do_inform_clients(conn_id);
 				break;
@@ -378,9 +519,9 @@
 					{
 						dim_print_date_time();
-						printf(" Couldn't write, releasing %d\n",servp->conn_id);
+						printf("Couldn't write, releasing %d\n",servp->conn_id);
 						fflush(stdout);
 					}
 					dim_print_date_time();
-					printf(" Service %s already declared, killing server %s\n",
+					printf("Service %s already declared, killing server %s\n",
 						servp->serv_name, Dns_conns[servp->conn_id].task_name);
 					fflush(stdout);
@@ -395,5 +536,5 @@
 					{
 						dim_print_date_time();
-printf(" Service %s already declared by conn %d - %s@%s:%d (PID %d), Redeclared by conn %d - %s@%s:%d (PID %d)(same server) - Closing both conns %d %d\n",
+printf("Service %s already declared by conn %d - %s@%s:%d (PID %d), Redeclared by conn %d - %s@%s:%d (PID %d)(same server) - Closing both conns %d %d\n",
 							servp->serv_name, servp->conn_id, 
 							Dns_conns[servp->conn_id].task_name,
@@ -418,6 +559,7 @@
 					else
 					{
-						Dns_conns[conn_id].already = 1;
-
+						Dns_conns[conn_id].already = servp->conn_id;
+						create_service_entry( conn_id, packet, i, -2);			
+/*DUPL
 						if( !dna_write_nowait(conn_id, &dis_packet, DNS_DIS_HEADER) )
 						{
@@ -426,6 +568,7 @@
 							fflush(stdout);
 						}
+*/
 						dim_print_date_time();
-printf(" Service %s already declared by conn %d - %s@%s:%d (PID %d), killing server conn %d - %s@%s:%d (PID %d) \n",
+printf("Service %s already declared by conn %d - %s@%s:%d (PID %d), Warning server conn %d - %s@%s:%d (PID %d) \n",
 							servp->serv_name, servp->conn_id, 
 							Dns_conns[servp->conn_id].task_name,
@@ -439,8 +582,10 @@
 							Dns_conns[conn_id].pid);
 						fflush(stdout);
-
+/*DUPL
 						release_conn(conn_id);
-
 						return(0);
+*/
+						if((!all_services_registered) && (n_services < MAX_REGISTRATION_UNIT))
+							all_services_registered = 1;
 					}
 #ifdef VMS
@@ -481,5 +626,4 @@
 						dll_remove((DLL *) servp);
 						service_remove(&(servp->next));
-						Curr_n_services--;
 						free(servp);
 						Dns_conns[conn_id].n_services--;
@@ -489,5 +633,5 @@
 							{
 								dim_print_date_time();
-								printf( " Conn %3d : Server %s@%s unregistered All services, releasing it.\n",
+								printf("Conn %3d : Server %s@%s unregistered All services, releasing it.\n",
 									conn_id, Dns_conns[conn_id].task_name,
 									Dns_conns[conn_id].node_name );
@@ -521,34 +665,14 @@
 		if(!(servp = service_exists(packet->services[i].service_name)))
 		{
-			servp = (DNS_SERVICE *)malloc(sizeof(DNS_SERVICE));
-			if(name_too_long)
-			{
-				if(strstr(packet->services[i].service_name,"/CLIENT_LIST"))
-				{
-					strncpy(Dns_conns[conn_id].long_task_name, packet->services[i].service_name,
-						(size_t)MAX_NAME);
-					ptrt = strstr(Dns_conns[conn_id].long_task_name,"/CLIENT_LIST");
-					*ptrt = '\0';
-				}
-			}
-			strncpy( servp->serv_name,
-				packet->services[i].service_name,
-				(size_t)MAX_NAME );
-			strncpy( servp->serv_def,
-				packet->services[i].service_def,
-				(size_t)MAX_NAME );
-			servp->state = 1;
-			servp->conn_id = conn_id;
-			servp->server_format = vtohl(packet->format);
-			servp->serv_id = vtohl(packet->services[i].service_id);
-			dll_insert_queue( (DLL *)
-					  Dns_conns[conn_id].service_head, 
-					  (DLL *) servp );
-			Dns_conns[conn_id].n_services++;
-			service_insert( &(servp->next) );
-			servp->node_head = (RED_NODE *) malloc(sizeof(NODE));
-			dll_init( (DLL *) servp->node_head );
-			Curr_n_services++;
+			create_service_entry( conn_id, packet, i, 1);			
 		} 
+	}
+	if((vtohl(packet->n_services) != 0) && Dns_conns[conn_id].already && all_services_registered)
+	{
+/*
+		dim_print_date_time();
+printf("Services already declared Warning server\n");
+*/
+		warn_server(conn_id);
 	}
 	if(update_did)
@@ -559,5 +683,5 @@
 		{
 			dim_print_date_time();
-			printf( " Conn %3d : Server %s@%s (PID %d) registered %d services\n",
+			printf("Conn %3d : Server %s@%s (PID %d) registered %d services\n",
 				conn_id, Dns_conns[conn_id].task_name,
 				Dns_conns[conn_id].node_name, 
@@ -620,5 +744,5 @@
 		{
 			dim_print_date_time();
-			printf(" Server %s (%s@%s) has been set in error\n",
+			printf("Server %s (%s@%s) has been set in error\n",
 				Dns_conns[conn_id].task_name, Net_conns[conn_id].task, Net_conns[conn_id].node);
 			fflush(stdout);
@@ -639,5 +763,5 @@
 		{
 			dim_print_date_time();
-			printf(" Conn %3d : Server %s@%s Registration Requested\n",
+			printf("Conn %3d : Server %s@%s Registration Requested\n",
 				conn_id, Net_conns[conn_id].task, Net_conns[conn_id].node);
 			fflush(stdout);
@@ -647,5 +771,5 @@
 		{
 			dim_print_date_time();
-			printf(" Server Validity: Couldn't write, releasing Conn %3d : Server %s@%s\n",conn_id,
+			printf("Server Validity: Couldn't write, releasing Conn %3d : Server %s@%s\n",conn_id,
 				Net_conns[conn_id].task, Net_conns[conn_id].node);
 			fflush(stdout);
@@ -676,5 +800,5 @@
 	{
 		dim_print_date_time();
-		printf(" Conn %3d : Client %s@%s requested %s\n",
+		printf("Conn %3d : Client %s@%s requested %s\n",
 			conn_id, Net_conns[conn_id].task, Net_conns[conn_id].node,
 			serv_regp->service_name);
@@ -720,5 +844,5 @@
 			dic_packet.size = htovl(DNS_DIC_HEADER);
 			dim_print_date_time();
-			printf(" Connection from %s refused, stopping client pid=%s\n",
+			printf("Connection from %s refused, stopping client pid=%s\n",
 					Net_conns[conn_id].node,
 					Net_conns[conn_id].task);
@@ -727,5 +851,5 @@
 			{
 				dim_print_date_time();
-				printf(" Stop Client: Couldn't write, releasing Conn %3d : Client %s@%s\n",conn_id,
+				printf("Stop Client: Couldn't write, releasing Conn %3d : Client %s@%s\n",conn_id,
 					Net_conns[conn_id].task,
 					Net_conns[conn_id].node);
@@ -773,5 +897,4 @@
 				}
 				service_remove(&(servp->next));
-				Curr_n_services--;
 				free(servp);
 			}
@@ -815,5 +938,4 @@
 				}
 				service_remove(&(servp->next));
-				Curr_n_services--;
 				free(servp);
 			}
@@ -850,5 +972,4 @@
 		servp->conn_id = 0;
 		service_insert(&(servp->next));
-		Curr_n_services++;
 		servp->node_head = (RED_NODE *)malloc(sizeof(NODE));
 		dll_init( (DLL *) servp->node_head );
@@ -930,5 +1051,5 @@
 	{
 		dim_print_date_time();
-		printf(" Client Request: Couldn't write, releasing Conn %3d : Client %s@%s\n",conn_id,
+		printf("Client Request: Couldn't write, releasing Conn %3d : Client %s@%s\n",conn_id,
 					Net_conns[conn_id].task,
 					Net_conns[conn_id].node);
@@ -962,5 +1083,8 @@
 				(DLL *) servp)) )
 	{
+/* DUPL
 		if( servp->state != -1 ) 
+*/
+		if( servp->state > -1 ) 
 		{
 			if( !dll_empty((DLL *) servp->node_head)) 
@@ -1014,5 +1138,5 @@
 		{
 			dim_print_date_time();
-			printf(" Inform Client: Couldn't write, releasing Conn %3d : Client %s@%s\n",nodep->conn_id,
+			printf("Inform Client: Couldn't write, releasing Conn %3d : Client %s@%s\n",nodep->conn_id,
 					Net_conns[nodep->conn_id].task,
 					Net_conns[nodep->conn_id].node);
@@ -1063,5 +1187,5 @@
 					{
 						dim_print_date_time();
-						printf(" Releasing client on conn %d - %s@%s\n",
+						printf("Releasing client on conn %d - %s@%s\n",
 							i, Net_conns[i].task, Net_conns[i].node);
 						fflush(stdout);
@@ -1088,5 +1212,5 @@
 		{
 			dim_print_date_time();
-			printf( " Conn %3d : Server %s@%s died\n",
+			printf("Conn %3d : Server %s@%s died\n",
 				conn_id, Dns_conns[conn_id].task_name,
 				Dns_conns[conn_id].node_name);
@@ -1098,5 +1222,5 @@
 			{
 				dim_print_date_time();
-				printf( " Conn %3d : Server %s@%s died\n",
+				printf("Conn %3d : Server %s@%s died\n",
 					conn_id, Dns_conns[conn_id].task_name,
 					Dns_conns[conn_id].node_name);
@@ -1119,5 +1243,4 @@
 			{
 				service_remove(&(servp->next));
-				Curr_n_services--;
 				old_servp = servp;
 				servp = servp->server_prev;
@@ -1152,5 +1275,5 @@
 		{
 			dim_print_date_time();
-			printf(" Conn %3d : Client %s@%s died\n",
+			printf("Conn %3d : Client %s@%s died\n",
 				conn_id, Net_conns[conn_id].task, Net_conns[conn_id].node);
 			fflush(stdout);
@@ -1172,5 +1295,4 @@
 				{
 					service_remove(&(servp->next));
-					Curr_n_services--;
 					free( servp );
 				}
@@ -1187,5 +1309,5 @@
 		{
 			dim_print_date_time();
-			printf(" Conn %3d : Undefined Type %s@%s died\n",
+			printf("Conn %3d : Undefined Type %s@%s died\n",
 				conn_id, Net_conns[conn_id].task,
 				Net_conns[conn_id].node);
@@ -1281,4 +1403,6 @@
 						    (DLL *) servp)) )
 	{
+		if(servp->state == -2)
+			continue;
 		strncpy(dns_service_info->name, servp->serv_name, (size_t)MAX_NAME); 
 		dns_service_info->status = htovl(1);
@@ -1448,5 +1572,5 @@
 	get_node_name(node);
 	dim_print_date_time();
-	printf(" DNS version %d starting up on %s\n",DIM_VERSION_NUMBER, node); 
+	printf("DNS version %d starting up on %s\n",DIM_VERSION_NUMBER, node); 
 	fflush(stdout);
 
@@ -1506,5 +1630,5 @@
 
 	dim_print_date_time();
-	printf(" Connection Statistics :\n");
+	printf("Connection Statistics :\n");
 	for(i = 0; i< Curr_N_Conns; i++)
 	{
@@ -1595,5 +1719,5 @@
 				type |= (soft_code << (int)16) & (int)0xFFFF0000;
 				dim_print_date_time();
-				printf(" Killing server %s@%s with exit code %d\n",
+				printf("Killing server %s@%s with exit code %d\n",
 					Dns_conns[i].task_name, Dns_conns[i].node_name, soft_code);
 			}
@@ -1601,5 +1725,5 @@
 			{
 				dim_print_date_time();
-				printf(" Killing server %s@%s\n",
+				printf("Killing server %s@%s\n",
 					Dns_conns[i].task_name, Dns_conns[i].node_name);
 			}
@@ -1609,5 +1733,5 @@
 			{
 				dim_print_date_time();
-				printf(" Kill Server: Couldn't write, releasing %d\n",i);
+				printf("Kill Server: Couldn't write, releasing %d\n",i);
 				fflush(stdout);
 				release_conn(i);
@@ -1636,4 +1760,5 @@
 	dll_insert_queue((DLL *) Service_hash_table[index], 
 			 (DLL *) servp);
+	Curr_n_services++;
 }
 
@@ -1642,6 +1767,12 @@
 {
 	if( servp->node_head )
+	{
 		free( servp->node_head );
-	dll_remove( (DLL *) servp );
+	}
+	if(servp->next)
+	{
+		dll_remove( (DLL *) servp );
+		Curr_n_services--;
+	}
 }
 
@@ -1834,5 +1965,5 @@
 		dim_print_date_time();
 		conn_id = dis_get_conn_id();
-		printf(" Got Browse Request <%s> from conn: %d %s@%s\n", buffer, conn_id,
+		printf("Got Browse Request <%s> from conn: %d %s@%s\n", buffer, conn_id,
 			Net_conns[conn_id].task,Net_conns[conn_id].node);
 	}
@@ -1842,5 +1973,5 @@
 		dim_print_date_time();
 		conn_id = dis_get_conn_id();
-		printf(" Browse Request <%s> found %d services\n", buffer, n);
+		printf("Browse Request <%s> found %d services\n", buffer, n);
 	}
 	if(!Rpc_info_size)
