Index: trunk/FACT++/dim/src/webDid/webServer.c
===================================================================
--- trunk/FACT++/dim/src/webDid/webServer.c	(revision 18058)
+++ trunk/FACT++/dim/src/webDid/webServer.c	(revision 18920)
@@ -5,5 +5,6 @@
 #include <fcntl.h>
 
-int WebDID_Debug = 0;
+extern int WebDID_Debug;
+extern int IsWebDID;
 
 #define BUFSIZE 8096
@@ -83,5 +84,5 @@
 }
 
-int getParameters(char *buffer, char (*pars)[], char *ptrs[])
+int getParameters(char *buffer, char (*pars)[], char *ptrs[], int nmandatory)
 {
 	char *ptr, *parptr;
@@ -106,5 +107,5 @@
 	for(i = 0; ptrs[i]; i++)
 	{
-	        if((ptr = strchr(ptrs[i],'&')))
+	    if((ptr = strchr(ptrs[i],'&')))
 			*ptr = '\0';
 		while((ptr = strchr(ptrs[i],'%')))
@@ -121,8 +122,14 @@
 		}
 	}
-	if(found == n)
-		return 1;
-	else
-		return 0;
+	if(nmandatory == -1)
+	{
+		if(found == n)
+			return 1;
+	}
+	else if(found >= nmandatory)
+	{
+		return found;
+	}
+	return 0;
 }
 
@@ -136,5 +143,5 @@
 	strcpy(pars[1],"browser=");
 	pars[2][0] = '\0';
-	ret = getParameters(buffer, pars, ptrs);
+	ret = getParameters(buffer, pars, ptrs, -1);
 	if(!ret)
 		return 0;
@@ -158,5 +165,5 @@
 	strcpy(pars[3],"browser=");
 	pars[4][0] = '\0';
-	ret = getParameters(buffer, pars, ptrs);
+	ret = getParameters(buffer, pars, ptrs, -1);
 	if(!ret)
 		return 0;
@@ -181,5 +188,5 @@
 	strcpy(pars[3],"force=");
 	pars[4][0] = '\0';
-	ret = getParameters(buffer, pars, ptrs);
+	ret = getParameters(buffer, pars, ptrs, -1);
 	if(!ret)
 		return 0;
@@ -190,4 +197,70 @@
 if(WebDID_Debug)
 printf("\nparse service pars - service %s %d %d %d\n\n",service, *req, *browser, *force);
+	return 1;
+}
+
+int getServiceParametersDim(char *buffer, char *service, void *nolink, int *nolinksize, int *update)
+{
+	char pars[10][32];
+	char *ptrs[10];
+	int ret;
+	char nolinkstr[MAX_NAME] = { '\0' }, updatestr[10] = { '\0' };
+	char servicestr[MAX_NAME];
+
+	strcpy(pars[0], "service=");
+	strcpy(pars[1], "nolink=");
+	strcpy(pars[2], "update=");
+	pars[3][0] = '\0';
+	ret = getParameters(buffer, pars, ptrs, 1);
+	if (!ret)
+		return 0;
+	strcpy(servicestr, ptrs[0]);
+	if (ptrs[1])
+	    strcpy(nolinkstr, ptrs[1]);
+	if (ptrs[2])
+	    strcpy(updatestr, ptrs[2]);
+//	sscanf(ptrs[1], "%d", req);
+//	sscanf(ptrs[2], "%d", browser);
+//	sscanf(ptrs[3], "%d", force);
+	if (servicestr[0] == '"')
+	{
+		strcpy((char *)service, &servicestr[1]);
+		((char *)service)[strlen(servicestr) - 2] = '\0';
+	}
+	else
+	{
+		strcpy(service, ptrs[0]);
+	}
+	if ((nolink) && (nolinksize))
+	{
+		if(nolinkstr[0] != '\0')
+		{
+			if (nolinkstr[0] == '"')
+			{
+				strcpy((char *)nolink, &nolinkstr[1]);
+				((char *)nolink)[strlen(nolinkstr) - 2] = '\0';
+				*nolinksize = strlen(nolinkstr) - 2 + 1;
+			}
+			else
+			{
+				sscanf(nolinkstr, "%d", (int *)nolink);
+				*nolinksize = sizeof(int);
+			}
+		}
+		else
+		{
+			*((int *)nolink) = -1;
+			*nolinksize = sizeof(int);
+		}
+	}
+	if (update)
+	{
+		if (updatestr[0] != '\0')
+		{
+			sscanf(updatestr, "%d", (int *)update);
+		}
+	}
+	if (WebDID_Debug)
+		printf("\nparse service pars - service %s %s %s\n\n", service, nolinkstr, updatestr);
 	return 1;
 }
@@ -238,5 +311,5 @@
 }
 
-void sendData(int conn_id, char *buffer, int type)
+void sendData(int conn_id, char *buffer, int type, int oper)
 {
 	static char date_buffer[128];
@@ -250,6 +323,11 @@
 	extern char *getJSONHeader();
 	extern char *getJSONBuffer();
+	extern char *getJSONDimBuffer();
 	char datatype[128];
 	char *conv_buffer;
+	int conv_size = 0;
+	char nolink[MAX_NAME];
+	int nolinksize;
+	int update;
 
 	conv_buffer = buffer;
@@ -287,5 +365,5 @@
 		else
 		{
-			update_service_data(service, conn_id, 0, req, browser, force);
+			update_service_data(service, conn_id, 0, req, browser, force, 1, 0, 0);
 			return;
 		}
@@ -296,4 +374,30 @@
 if(WebDID_Debug)
 		printf("%s\n",ptr);
+		strcpy(datatype,"text/html");
+	}
+	else if(type == 10)
+	{
+		ret = getServiceParametersDim(conv_buffer, service, nolink, &nolinksize, &update);
+		if(!ret)
+		{
+			strcpy(snd_data_buffer,"{}");
+			ptr = snd_data_buffer;
+		}
+		else
+		{
+			if (oper == 0)
+				update = -1;
+			else if (oper == -1)
+				update = -2;
+			update_service_data(service, conn_id, update, 0, conn_id, 1, 0, nolink, nolinksize);
+			return;
+		}
+	}
+	else if(type == 11)
+	{
+		ptr = conv_buffer;
+if(WebDID_Debug)
+		printf("%s\n",ptr);
+//		strcpy(datatype,"application/octet-stream");
 		strcpy(datatype,"text/html");
 	}
@@ -330,8 +434,29 @@
 		}
 	}
-	getTime(date_buffer);
-	(void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
-		      date_buffer, (int)strlen(ptr), datatype);
-    (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
+	else if (type == 12)
+	{
+		ret = getServiceParametersDim(conv_buffer, service, 0, 0, 0);
+		if (!ret)
+		{
+			strcpy(snd_data_buffer, "{}");
+			ptr = snd_data_buffer;
+		}
+		else
+		{
+			find_services(service, conn_id, conn_id, 1);
+			ptr = getJSONDimBuffer("src", conn_id);
+		}
+	}
+	if (IsWebDID)
+	{
+		getTime(date_buffer);
+/*
+		(void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nKeep-Alive: timeout=1000\r\nConnection: keep-alive\r\n\r\n",
+			date_buffer, (int)strlen(ptr), datatype);
+*/
+		(void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
+			date_buffer, (int)strlen(ptr), datatype);
+		(void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
+	}
 if(WebDID_Debug)
 	printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
@@ -391,5 +516,5 @@
 		else
 		{
-			update_service_data(service, conn_id, 0, req, browser, force);
+			update_service_data(service, conn_id, 0, req, browser, force, 0, 0);
 			return;
 		}
@@ -434,8 +559,11 @@
 		}
 	}
-	getTime(date_buffer);
-	(void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
-		      date_buffer, (int)strlen(ptr), datatype);
-    (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
+	if (IsWebDID)
+	{
+		getTime(date_buffer);
+		(void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
+			date_buffer, (int)strlen(ptr), datatype);
+		(void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer));
+	}
 if(WebDID_Debug)
 	printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
@@ -453,4 +581,6 @@
         static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
 		static char date_buffer[128];
+		char *ptr;
+		int operation = 0;
 
 
@@ -462,10 +592,12 @@
 if(WebDID_Debug)
 printf("Got %s\n", buffer);
-        if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) )
-		{
-                log_it(SORRY,"Only simple GET operation supported",buffer,conn_id);
-				return;
-		}
-
+        if (IsWebDID)
+        {
+	        if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "get ", 4))
+	        {
+		        log_it(SORRY, "Only simple GET operation supported", buffer, conn_id);
+		        return;
+	        }
+        }
         for(i=4;i<BUFSIZE;i++) 
 		{ /* null terminate after the second space to ignore extra stuff */
@@ -479,5 +611,5 @@
 if(WebDID_Debug)
 printf("Got 1 %s\n", buffer);
-       for(j=0;j<i-1;j++)      /* check for illegal parent directory use .. */
+        for(j=0;j<i-1;j++)      /* check for illegal parent directory use .. */
 		{
                 if(buffer[j] == '.' && buffer[j+1] == '.')
@@ -516,46 +648,74 @@
 		printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
 */
+		ptr = &buffer[5];
+		if (!IsWebDID)
+		{
+			if (!strncmp(&buffer[0], "GET", 3) || !strncmp(&buffer[0], "get", 3))
+			{
+				operation = 0;
+			}
+			if (!strncmp(&buffer[0], "SUBSCRIBE", 9) || !strncmp(&buffer[0], "subscribe", 9))
+			{
+				operation = 1;
+				ptr = &buffer[11];
+			}
+			if (!strncmp(&buffer[0], "UNSUBSCRIBE", 11) || !strncmp(&buffer[0], "unsubscribe", 11))
+			{
+				operation = -1;
+				ptr = &buffer[13];
+			}
+		}
 		if(fstr == 0)
 		{
 if(WebDID_Debug)
 printf("Got %s\n", buffer);
-			if(!strncmp(&buffer[5],"didHeader",9))
-			{
-				sendData(conn_id, &buffer[5], 0);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"didData",7))
-			{
-				sendData(conn_id, &buffer[5], 1);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"didServices",11))
-			{
-				sendData(conn_id, &buffer[5], 2);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"didServiceData",14))
-			{
-				sendData(conn_id, &buffer[5], 3);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"didPoll",7))
-			{
-				sendData(conn_id, &buffer[5], 5);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"didQuery",8))
-			{
-				sendData(conn_id, &buffer[5], 6);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"smiData",7))
-			{
-				sendSmiData(conn_id, &buffer[5], 1);
-				return;
-			}
-			else if(!strncmp(&buffer[5],"smiObjects",10))
-			{
-				sendSmiData(conn_id, &buffer[5], 2);
+            if (!strncmp(ptr, "didHeader", 9))
+			{
+				sendData(conn_id, ptr, 0, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "didData", 7))
+			{
+				sendData(conn_id, ptr, 1, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "didServices", 11))
+			{
+				sendData(conn_id, ptr, 2, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "didServiceData", 14))
+			{
+				sendData(conn_id, ptr, 3, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "dimService", 10))
+			{
+				sendData(conn_id, ptr, 10, operation);
+				return;
+			}
+			else if (!strncmp(ptr, "didPoll", 7))
+			{
+				sendData(conn_id, ptr, 5, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "didQuery", 8))
+			{
+				sendData(conn_id, ptr, 6, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "dimBrowser", 10))
+			{
+				sendData(conn_id, ptr, 12, 0);
+				return;
+			}
+			else if (!strncmp(ptr, "smiData", 7))
+			{
+				sendSmiData(conn_id, ptr, 1);
+				return;
+			}
+			else if (!strncmp(ptr, "smiObjects", 10))
+			{
+				sendSmiData(conn_id, ptr, 2);
 				return;
 			}
@@ -635,4 +795,6 @@
 static void handler( int conn_id, char *packet, int size, int status )
 {
+	int close_browser();
+
 	switch(status)
 	{
@@ -644,5 +806,6 @@
 				Net_conns[conn_id].task,Net_conns[conn_id].node );
 }
-			web_close(conn_id);
+            close_browser(conn_id);
+            web_close(conn_id);
 		break;
 	case STA_CONN:     /* connection received */
@@ -707,6 +870,6 @@
 		*ptr = '\0';
 	}
-	chdir(currwd);
-        log_it(LOG,"webDid starting",argv[1],getpid());
+//	chdir(currwd);
+        log_it(LOG,"webDim starting",argv[1],getpid());
         /* setup the network socket */
 		proto = 1;
@@ -714,6 +877,14 @@
 		get_node_name(dns_node);
 		did_init(dns_node, DNS_PORT);
-		if(!web_open_server("DID",handler, &proto, &port, error_handler))
-			return(0);
+		if(IsWebDID)
+		{
+			if (!web_open_server("DID", handler, &proto, &port, error_handler))
+				return(0);
+		}
+		else
+		{
+			if (!web_open_server("DimClient", handler, &proto, &port, error_handler))
+				return(0);
+		}
 /*
 		ret = matchString("hello world","*ll*");
