Print

Print


Commit in epics/trunk/epics_example/myexampleApp/src on MAIN
client_util.c+220-590818 -> 819
dbSubExample.c+155-118818 -> 819
dbSubExample.dbd-4818 -> 819
+375-712
3 modified files
Fixed com order with socket, added switches and cleaned up a lot

epics/trunk/epics_example/myexampleApp/src
client_util.c 818 -> 819
--- epics/trunk/epics_example/myexampleApp/src/client_util.c	2014-07-22 17:23:29 UTC (rev 818)
+++ epics/trunk/epics_example/myexampleApp/src/client_util.c	2014-07-24 00:09:53 UTC (rev 819)
@@ -3,24 +3,22 @@
 #include <ctype.h>
 #include "sys/ioctl.h"
 
-const unsigned int BUFFER_SIZE = 256;
-const unsigned int MAX_BUFFERS = 1000;
-const unsigned int XML_STRING_MAX = 65536;
 int client_util_debug = 1;
-char *xml_string_global = NULL;
+char *xml_string = NULL;
 xmlDoc* doc = NULL;
 xmlNode* xml_root = NULL;
+int xml_poll_status = 0;
 
 void free_xml_string() {
-  if(client_util_debug>0) printf("free_xml_string(): at %p\n",xml_string_global);
-  if(xml_string_global!=NULL) {
-    free(xml_string_global);
-    xml_string_global = NULL;
-    if(client_util_debug>0) printf("free_xml_string(): done\n");
+  if(client_util_debug>2) printf("free_xml_string(): at %p\n",xml_string);
+  if(xml_string!=NULL) {
+    free(xml_string);
+    xml_string = NULL;
+    if(client_util_debug>2) printf("free_xml_string(): done\n");
   } else {
-    if(client_util_debug>0) printf("free_xml_string(): already nil\n");
+    if(client_util_debug>2) printf("free_xml_string(): already nil\n");
   }
-  if(client_util_debug>0) printf("free_xml_string(): now at %p\n",xml_string_global);
+  if(client_util_debug>2) printf("free_xml_string(): now at %p\n",xml_string);
 }
 
 char* strToUpper( char* s )
@@ -130,20 +128,20 @@
   struct sockaddr_in serv_addr;
   struct hostent *server;
   int socketfd;
-  if(client_util_debug>0) printf("open_socket 1: open socket \n");
+  if(client_util_debug>0) printf("open_socket : open for %s:%d \n",hostname,portno);
   socketfd = socket(AF_INET, SOCK_STREAM, 0);
-  if(client_util_debug) printf("open_socket 1: opened socket %d \n",socketfd);
+  if(client_util_debug>1) printf("open_socket : opened socket %d \n",socketfd);
   if (socketfd < 0) {
     socket_error("ERROR opening socket");
     return socketfd;
   }
-  if(client_util_debug) printf("open_socket 1: set server\n");
+  if(client_util_debug>1) printf("open_socket : set server\n");
   server = gethostbyname(hostname);
   if (server == NULL) {
     socket_error("ERROR, no such host");
     return -1;
   }
-  if(client_util_debug) printf("open_socket 1: got server at %p\n",server);
+  if(client_util_debug>1) printf("open_socket : got server at %p\n",server);
   bzero((char *) &serv_addr, sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   bcopy((char *)server->h_addr, 
@@ -151,14 +149,14 @@
         server->h_length);
   serv_addr.sin_port = htons(portno);
 
-  if(client_util_debug) printf("open_socket 1: connect\n");
+  if(client_util_debug>1) printf("open_socket : connect\n");
   
   if (connect(socketfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
     socket_error("ERROR connecting");
     return -1;
   }
 
-  if(client_util_debug) printf("open_socket 1: return socket %d\n",socketfd);
+  if(client_util_debug>1) printf("open_socket : return socket %d\n",socketfd);
 
   return socketfd;
 
@@ -170,58 +168,109 @@
 
 
 
-void pollXmlStr(int sockfd) {
-  if(client_util_debug!=0) printf("pollXmlStr\n");
+void getXmlDoc(int sockfd, int read_status, int read_config) {
+
+  if(client_util_debug>0) printf("getXmlDoc: from socket %d (read_status %d read_config %d)\n",sockfd,read_status,read_config);
+  
+  // check that I'm not already polling it
+  if(xml_poll_status==1) {
+    if(client_util_debug>0) printf("getXmlDoc: already polling so don't continue for this process (%d,%p)\n",xml_poll_status,xml_string);    
+    return;
+  }
+  
+  // occupy the poll flag
+  xml_poll_status = 1;
+  
   //clear old documents
-  if(client_util_debug!=0) printf("pollXmlStr: free xml string %p\n",xml_string_global);
+  if(client_util_debug>1) printf("getXmlDoc: free xml string %p\n",xml_string);
   free_xml_string();
-  //if(xml_string_global!=NULL) {
-  //  free(xml_string_global);
+  //if(xml_string!=NULL) {
+  //  free(xml_string);
   //}
-  if(client_util_debug!=0) printf("pollXmlStr: free xml string done %p\n",xml_string_global);
-  if(client_util_debug!=0) printf("pollXmlStr: free xml doc\n");
+  if(client_util_debug>1) printf("getXmlDoc: free xml string done %p\n",xml_string);
+  if(client_util_debug>1) printf("getXmlDoc: free xml doc\n");
   if(doc!=NULL) {
     xmlFreeDoc(doc);
     xmlCleanupParser();
     doc=NULL;
     xml_root=NULL;
   }
-  if(client_util_debug!=0) printf("free xml doc done\n");
+  if(client_util_debug>1) printf("getXmlDoc: free xml doc done\n");
 
-  if(client_util_debug!=0) printf("ReadConfig\n");
+  // Do a status and config read to be sure to catch all info
+  // Need to see exactly how the falgs are set in the return string 
+  // from the server to see what to do here.
 
-  writeReadConfig(sockfd);
-  
-  if(client_util_debug!=0) printf("Before reading xml string (%p)\n",xml_string_global);
+  if(read_status>0) {
 
-  read_system_xml_string4(sockfd);
+    if(client_util_debug>0) printf("getXmlDoc: ReadStatus\n");
+    
+    writeReadStatus(sockfd);
 
-  if(client_util_debug!=0) printf("After reading xml string (%p)\n",xml_string_global);
+  }
+ 
+  if(read_config>0) {
 
-  if(xml_string_global!=NULL) {
-    if(client_util_debug!=0) printf("create xml document from xml strlen %ld\n",strlen(xml_string_global));
-    if(strlen(xml_string_global)>0) {
-      doc = xmlReadMemory(xml_string_global,strlen(xml_string_global),"noname.xml",NULL,0);
-      if(client_util_debug!=0) printf("xml doc done %p\n",doc);
+    if(client_util_debug>0) printf("getXmlDoc: ReadConfig\n");
+    
+    writeReadConfig(sockfd);
+
+  }
+
+  if(client_util_debug>1) printf("getXmlDoc: Before reading xml string (%p)\n",xml_string);
+
+  pollXmlString(sockfd);
+
+  if(client_util_debug>1) printf("getXmlDoc: After reading xml string (%p)\n",xml_string);
+
+  if(xml_string!=NULL) {
+    if(strlen(xml_string)>0) {
+      if(client_util_debug>1) printf("getXmlDoc: create xml document from xml string(strlen %ld)\n",strlen(xml_string));
+      if(client_util_debug>0) printf("getXmlDoc: xml string:\n%s\n",xml_string);
+      doc = xmlReadMemory(xml_string,strlen(xml_string),"noname.xml",NULL,0);
+      if(client_util_debug>1) printf("getXmlDoc: xml doc done %p\n",doc);
       if(doc!=NULL) {
 	xml_root = xmlDocGetRootElement(doc);
 	if(xml_root!=NULL) {
-	  if(client_util_debug!=0) printf("found xml_root name %s\n",(xml_root)->name);
+	  if(client_util_debug>1) printf("getXmlDoc: found xml_root name %s\n",(xml_root)->name);
 	}
       } else {
-	if(client_util_debug!=0) printf("problem building xml doc %p\n",doc);	
+	if(client_util_debug>1) printf("getXmlDoc: problem building xml doc %p\n",doc);	
       }
     } else {
-      if(client_util_debug!=0) printf("ERROR: xml_string has zero length!\n");	
+      printf("ERROR: getXmlDoc xml_string is there but has zero string length!\n");	
+      exit(1);
     }
   } else {
-    printf("WARNING: pollXmlStr: xml_string is NULL after reading from socket!\n");	
+    printf("WARNING: getXmlDoc: xml_string is NULL after reading from socket!\n");	
   }
-    
+  
+  //clear the flag
+  xml_poll_status = 0;
+  if(client_util_debug>0) printf("getXmlDoc: cleared the flag and return (%d)\n",xml_poll_status);
+
 }
 
 
 
+void writeHybridVSwitch(int sockid, int value, int hyb_id) {  
+  char buffer[256];
+  char hyb_tag[256];
+  char toggle[256];
+  int n;
+  memset(buffer,0,256);  
+  memset(hyb_tag,0,256);  
+  memset(toggle,0,256);  
+  if(value==1) sprintf(toggle,"%s","True");
+  else sprintf(toggle,"%s","False");
+  sprintf(hyb_tag,"Hybrid%dPwrEn",hyb_id);
+  sprintf(buffer,"<system><config><FrontEndTestFpga><FebCore><%s>%s</%s></FebCore></FrontEndTestFpga></config></system>\f",hyb_tag,toggle,hyb_tag);
+  if(client_util_debug > 0) printf("writeHybridVSwitch: Executing:\"%s\"\n",buffer);
+  n = write(sockid,buffer,strlen(buffer));
+  if(n<0) 
+    socket_error("writeHybridVSwitch: ERROR writing to socket");
+  return;  
+}
 
 
 
@@ -241,10 +290,10 @@
   sprintf(hyb_tag,"Hybrid%d%sTrim",hyb_id,ch_name_corr);
   //  sprintf(buffer,"<system><config><FrontEndTestFpga><FebCore><%s>0x%x</%s></FebCore></FrontEndTestFpga></config></system>\f",hyb_tag,value,hyb_tag);
   sprintf(buffer,"<system><config><FrontEndTestFpga><FebCore><%s>%d</%s></FebCore></FrontEndTestFpga></config></system>\f",hyb_tag,value,hyb_tag);
-  if(client_util_debug > 0) printf("Executing:\"%s\"\n",buffer);
+  if(client_util_debug > 0) printf("writeHybridVTrim: Executing:\"%s\"\n",buffer);
   n = write(sockid,buffer,strlen(buffer));
   if(n<0) 
-    socket_error("ERROR writing to socket");
+    socket_error("writeHybridVTrim: ERROR writing to socket");
   return;  
 }
 
@@ -254,30 +303,40 @@
   int n;
   //char buffer[] = "<system><command>ReadConfig</command></system>\f";
   char buffer[] = "<system><command><ReadConfig/></command></system>\f";
-  if(client_util_debug) printf("Executing:\"%s\"\n",buffer);
+  if(client_util_debug) printf("writeReadConfig: Executing:\"%s\"\n",buffer);
   n = write(sockfd,buffer,strlen(buffer));
   if(n<0) 
-    socket_error("ERROR writing to socket");
+    socket_error("writeReadConfig: ERROR writing to socket");
   return;
 }
 
+void writeReadStatus(int sockfd) {
+  int n;
+  char buffer[] = "<system><command><ReadStatus/></command></system>\f";
+  if(client_util_debug) printf("writeReadStatus: Executing:\"%s\"\n",buffer);
+  n = write(sockfd,buffer,strlen(buffer));
+  if(n<0) 
+    socket_error("writeReadStatus: ERROR writing to socket");
+  return;
+}
 
 
 
 
+
 void getXMLValue(char tag[], char value[],const unsigned int MAX) {
-  if(client_util_debug!=0) printf("getXMLValue for tag: %s\n",tag);
+  if(client_util_debug>0) printf("getXMLValue: tag \"%s\"\n",tag);
   if(doc==NULL) {
-    printf("couldn't get xml document %p\n",doc);
+    printf("WARNING: getXMLValue: couldn't get xml document %p\n",doc);
     return;
   }
   if(xml_root==NULL) {
-    printf("couldn't get root from document\n");
+    printf("WARNING: getXMLValue: couldn't get root from document\n");
     return;
   }  
-  if(client_util_debug!=0) printf("retrieve value from xml document\n");
+  if(client_util_debug>1) printf("getXMLValue: retrieve value from xml document\n");
   retrieveValue(doc,xml_root,tag,value,MAX);
-  if(client_util_debug!=0) printf("retrieved value \"%s\" from xml\n",value);
+  if(client_util_debug>1) printf("getXMLValue: retrieved value \"%s\" from xml\n",value);
 }
 
 
@@ -391,197 +450,176 @@
 
 
 
-void read_system_xml_string4(int socketfd) {
-  //const float wait_interval_usec = 100000;  
+void pollXmlString(int socketfd) {
   char* buf = NULL;
-  char* buf_more = NULL;
-  int buf_len = 0;
-  int read_i = 0;
+  char* buf_loop = NULL;
+  int buf_len;
+  int read_i;
   int read_n;
   int nsleep;
   int counter;
-  int debug = client_util_debug;
-  int n_endings = 0;
-  clock_t start_time;
-  clock_t diff;
+  int n_endings;
+  time_t timer;
+  time_t cur_time;
+  struct tm *lt;
+  int dt;
   char *pch;
 
   
-  if(debug!=0) printf("read_system_xml_string4: xml string at %p\n",xml_string_global);
+  if(client_util_debug>0) printf("pollXmlString:  from socket %d \n", socketfd);
+
   free_xml_string();
-  //  if(xml_string_global!=NULL) {
-  //  free(xml_string_global);
-  //  if(debug!=0) printf("read_system_xml_string4: xml string free done now at %p\n",xml_string_global);
-  //  //exit(1);
-  //}
-  if(client_util_debug!=0) printf("xml_string at %p \n",xml_string_global);
-  start_time = clock();
-  diff = (start_time - start_time);
   
-  if(debug>0) printf("start_time at %f clocks\n",(float)start_time);
+  time(&timer);
   
+  if(client_util_debug>0) {
+    lt = localtime(&timer);
+    printf("pollXmlString: start_time at %s\n",asctime(lt));
+  }
+  
   nsleep=0;
   counter=0;
-  //while(((float)(nsdiff/CLOCKS_PER_SEC))<5.0) { 
+  read_i=0;
+  buf_len=0;
+  n_endings=0;
+  dt=0;
+  
   while(nsleep<6) { 
-    if(debug>0) printf("at read_i %d clock time %f clocks %f time %f\n",read_i,(float)clock(),(float)diff,(float)((float)diff)/CLOCKS_PER_SEC);
     
-    //bzero(buf,READ_BUFFER_SIZE);
+    time(&cur_time);
+    dt = difftime(cur_time,timer);
     
+    if(client_util_debug>0) printf("pollXmlString: Try to read from socket (nsleep %d read_i %d time %ds)\n",nsleep,read_i,dt);
+    
     read_n = 0;
     ioctl(socketfd, FIONREAD, &read_n);
     
-    if(debug>0) {
-      printf("%d available on socket\n",read_n);
+    if(client_util_debug>0) {
+      printf("pollXmlString: %d chars available on socket\n",read_n);
     }
     
     if(read_n>0) {      
       
-      
       // allocate memory needed
-      if(debug>1) printf("Allocate %d array\n",read_n);      
+      if(client_util_debug>1) printf("pollXmlString: Allocate %d array\n",read_n);      
       
-      if(buf_more!=NULL) {
-	printf("buf_more is not null!\n");
+      // check that the buffer used is not weird
+      if(buf_loop!=NULL) {
+	printf("pollXmlString: ERROR: pollXmlString: buf_loop is not null!\n");
 	exit(1);
       }
       
-      buf_more = (char*) calloc(read_n+1,sizeof(char));
+      // allocate space to hold the input
+      buf_loop = (char*) calloc(read_n+1,sizeof(char));
       
-      if(debug>1) printf("Allocated buf_more array at %p strlen %ld with %d length \n",buf_more,strlen(buf_more),(int)sizeof(char)*(read_n+1));      
+      if(client_util_debug>1) printf("pollXmlString: Allocated buf_loop array at %p strlen %ld with %d length \n",buf_loop,strlen(buf_loop),(int)sizeof(char)*(read_n+1));      
       
-      read_n = read(socketfd,buf_more,read_n);
+      // Read from socket
+      read_n = read(socketfd,buf_loop,read_n);
       
-      if(debug>0) printf("read_n %d from socket and buf_more strlen %ld\n",read_n,strlen(buf_more));
+      if(client_util_debug>0) printf("pollXmlString: Read %d chars from socket\n",read_n);
       
+      if(client_util_debug>2) printf("pollXmlString: buf_loop strlen is %ld\n",strlen(buf_loop));
+      
       if (read_n < 0) {
-	printf("ERROR reading from socket");
+	printf("pollXmlString: ERROR: pollXmlString: read %d from socket\n",read_n);
 	exit(1);
       }
       
-      //fix terminations
+      //fix c-string terminations, cases where there is a termination in the middle..
       int k;
       for(k=0;k<read_n;++k) {
-      	//printf("'%c'\n",buf_more[k]);
-      	if(buf_more[k]=='\0') {
-      	  if(debug>1) printf("fix termination at idx %d in this buf_more!!!\n",k);
-	  buf_more[k]=' ';
+      	//printf("pollXmlString: '%c'\n",buf_loop[k]);
+      	if(buf_loop[k]=='\0') {
+      	  if(client_util_debug>2) printf("pollXmlString: fix termination at idx %d in this buf_loop\n",k);
+	  buf_loop[k]=' ';
       	}
       }
       
-      if(debug>1) printf("read_n %d from socket and fixed buf_more strlen %ld:\n%s\n",read_n,strlen(buf_more),buf_more);
+      if(client_util_debug>2) printf("pollXmlString: Fixed buf_loop strlen %ld:\n%s\n",strlen(buf_loop),buf_loop);
       
-      pch = strchr(buf_more,'\f'); 
+
+      // search for xml endings in this buffer
+      pch = strchr(buf_loop,'\f'); 
       while(pch!=NULL) { 
-	if(debug>1) printf("found ending at %p (array index %ld) in this buf!\n",pch,pch-buf_more); 
+	if(client_util_debug>1) printf("pollXmlString: found ending at %p (array index %ld) in this buf!\n",pch,pch-buf_loop); 
 	n_endings++; 
 	pch = strchr(pch+1,'\f'); 
       } 
       
-
-      // copy to other buffer      
-      if(debug>1) printf("Copy %d to other buffer \n",read_n);      
       
-      
-      if(debug>1) printf("buf at %p before realloc\n",buf);      
+      // copy to other buffer while looping            
+      if(client_util_debug>2) printf("pollXmlString: Copy %d to other buffer (at %p before realloc) \n",read_n,buf);      
+
+      // reallocate more memory
       buf = (char*) realloc(buf,sizeof(char)*(buf_len+read_n));
       if(buf==NULL) {
-	printf("failed to allocated more buf\n");
-	if(buf_more==NULL) {
-	  free(buf_more);
+	printf("pollXmlString: ERROR: pollXmlString: failed to allocated buf\n");
+	if(buf_loop==NULL) {
+	  free(buf_loop);
 	}
 	exit(1);
       }
       
-      if(debug>1) printf("Allocated longer buf at %p and copy to pointer %p (offset= %d) \n",buf,buf+buf_len,buf_len);      
+      if(client_util_debug>2) printf("pollXmlString: Allocated longer buf at %p and copy to pointer %p (offset= %d) \n",buf,buf+buf_len,buf_len);      
       
-      memcpy(buf+buf_len,buf_more,sizeof(char)*read_n);
 
-      if(debug>1) printf("memcpy done\n");
+      // do the copy
+      memcpy(buf+buf_len,buf_loop,sizeof(char)*read_n);
       
-      //update the buffer length
-      buf_len += read_n;
-
-      //terminate 
-      //buf[buf_len-1] = '\0';
+      if(client_util_debug>1) printf("pollXmlString: memcpy done\n");
       
-      if(debug>1) printf("free buf_more\n");
+      //update the buffer length counter
+      buf_len += read_n;      
       
-      // free buffer
-      if(buf_more!=NULL) {
-	free(buf_more);
-	buf_more=NULL;
+      if(client_util_debug>1) printf("pollXmlString: free buf_loop\n");
+      
+      // free loop buffer for next loop
+      if(buf_loop!=NULL) {
+	free(buf_loop);
+	buf_loop=NULL;
       }
-
-      if(debug>1) printf("end of read_i %d with buf strlen %ld\n",read_i,strlen(buf));
-
-
+      
+      if(client_util_debug>2) printf("pollXmlString: end of read_i %d with buf strlen %ld\n",read_i,strlen(buf));
+      
       read_i++;
       
     } // read_n>0
     else {
-      if(debug>0) printf("Nothing to read from socket. Sleep a little\n");      
-      //usleep(wait_interval_usec);
-      if(debug>0) {
-	printf("nsleep %d\n",nsleep);
-      }
+      if(client_util_debug>0) printf("pollXmlString: Nothing to read from socket. Sleep 1s..\n");      
       sleep(1);
       nsleep++;
     } 
     
-
     
+    
     if(n_endings>1) {
-      if(debug!=0) printf("\nfound %d endings at read_i %d with at len %d and strlen %ld\n",n_endings,read_i,buf_len,strlen(buf));      
+      if(client_util_debug>0) printf("pollXmlString: \nfound %d endings at read_i %d with at len %d and strlen %ld. Stop reading from buffer\n",n_endings,read_i,buf_len,strlen(buf));      
       break;
     }
-
-    if(debug>0) {
-      printf("update timing\n");
-    }
-
-
-    //update timing
-    diff = clock()-start_time;
     
+    
     counter++;
-
-    if(debug>0) {
-      printf("yepp\n");
-    }
-
     
+    // dummy check for time-out?
     if(counter>50) {
-      printf("Many (%d) iterations to get XML string. Something is strange\n",counter);
+      printf("pollXmlString: Many (%d) iterations to get XML string. Something is strange\n",counter);
       break;
     }
-
-    if(debug>0) {
-      printf("yepp3\n");
-    }
-
+    
   }
-
-
-  if(debug>0) {
-    printf("yepp2\n");
-  }
-
-  if(debug>0) {
-    printf("%d\n",buf_len);
-    printf("%p\n",buf);
-  }
   
   
-  if(debug>0) {
-    printf("\nDone reading from socket. Found %d endings and a buf_len of %d\n\n",n_endings, buf_len);
-    if(buf!=NULL) printf("strlen %ld\n", strlen(buf));
+  
+  if(client_util_debug>0) {
+    printf("pollXmlString: Done reading from socket. Found %d endings and a buf_len of %d\n",n_endings, buf_len);
+    if(buf!=NULL) printf("pollXmlString: strlen %ld\n", strlen(buf));
   }
   
   
   if(n_endings>=1) {
-    if(debug>0) printf("\nPick out config and status string between <system> and %d endings in string with strlen %ld and buf_len %d\n",n_endings,strlen(buf),buf_len);
-    if(debug>1) printf("\nbuf: \n%s\n",buf);
+    if(client_util_debug>0) printf("pollXmlString: \nPick out config and status string between <system> and %d endings in string with strlen %ld and buf_len %d\n",n_endings,strlen(buf),buf_len);
+    if(client_util_debug>1) printf("pollXmlString: \nbuf: \n%s\n",buf);
     
     //assume it's the first system tag string I can find
     
@@ -589,33 +627,33 @@
     int len = findSystemStr(buf, buf_len,&start);    
     if(len>0) {      
       char* stop = start+len;
-      if(debug>1) printf("len %d start at %p stop at %p\n",len,start, stop);
+      if(client_util_debug>1) printf("pollXmlString: len %d start at %p stop at %p\n",len,start, stop);
       char* config = strstr(start,"<config>");
       if(config!=NULL && ((config-start)<len)) {
 	char* status = strstr(start,"<status>");
 	if(status!=NULL && ((status-start)<len)) {
 	  // seems we found all of them. 
-	  if(debug>1) printf("\ncalloc xml string len %d\n",len);
-	  xml_string_global = (char*) calloc(len,sizeof(char));
-	  //xml_string_global = (char*) malloc(sizeof(char)*len);
-	  if(debug>1) printf("copy to xml string at %p\n",xml_string_global);	
-	  memcpy(xml_string_global,start,len);
-	  if(debug>1) printf("\ncopied %d chars to %p with strlen %ld\n%s\n",len,xml_string_global,strlen(xml_string_global),xml_string_global);
+	  if(client_util_debug>1) printf("pollXmlString: \ncalloc xml string len %d\n",len);
+	  xml_string = (char*) calloc(len,sizeof(char));
+	  //xml_string = (char*) malloc(sizeof(char)*len);
+	  if(client_util_debug>1) printf("pollXmlString: copy to xml string at %p\n",xml_string);	
+	  memcpy(xml_string,start,len);
+	  if(client_util_debug>1) printf("pollXmlString: \ncopied %d chars to %p with strlen %ld\n%s\n",len,xml_string,strlen(xml_string),xml_string);
 	  
 	} else {
-	   if(debug>0) printf("\n no status found\n");
+	   if(client_util_debug>0) printf("pollXmlString: \n no status found\n");
 	}
       } else {
-	if(debug>0) printf("\n no config found\n");
+	if(client_util_debug>0) printf("pollXmlString: \n no config found\n");
       }
     }
     else {
-      if(debug>0) printf("Couldn't find system string in xml buffer\n");
+      if(client_util_debug>0) printf("pollXmlString: Couldn't find system string in xml buffer\n");
     }
-  }
+  } 
   
-  if(xml_string_global==NULL) {
-    printf("Problem finding proper xml string: found %d endings.\n",n_endings);
+  if(xml_string==NULL) {
+    if(client_util_debug>0) printf("pollXmlString: No valid xml string extracted from this poll (%d endings)\n",n_endings);
   }
   
   return;
@@ -626,7 +664,6 @@
 
 
 void retrieveValue(xmlDoc* doc, xmlNode* node, char* tags, char value[], const unsigned int MAX) {
-  int debug = 0;
   xmlChar* value_str = NULL;
   char* pch;
   char* pch_prev=NULL;
@@ -635,25 +672,25 @@
   //use a copy since it modifies the original string
   //char* tags = (char*)malloc(strlen(tags)*sizeof(char));
   //strcpy(tags,tags);
-  if(debug!=0) printf("Splitting strings \"%s\" into tokens\n",tags);
+  if(client_util_debug>0) printf("retrieveValue: for tag \"%s\"\n",tags);
   if(strlen(tags)>0) {
     pch = strtok(tags," :,.-");
     while(pch!=NULL) {
-      if(debug!=0) printf("Find element %s \n",pch);
+      if(client_util_debug>2) printf("retrieveValue: Find element %s \n",pch);
       if(pch_prev!=NULL) {
-        if(debug!=0) printf("Find element %s from children of prev element at %p\n",pch,prev_node->name);
+        if(client_util_debug>2) printf("retrieveValue: Find element %s from children of prev element at %p\n",pch,prev_node->name);
         cur_node = retrieveElement(doc,prev_node->children,pch);
       }
       else {
-        if(debug!=0) printf("Find element %s from element %s\n",pch,prev_node->name);
+        if(client_util_debug>2) printf("retrieveValue: Find element %s from element %s\n",pch,prev_node->name);
         cur_node = retrieveElement(doc,prev_node,pch);
       }
 
       // check that we found it
       if(cur_node != NULL) {
-        if(debug!=0) printf("found cur_node name %s\n",cur_node->name);
+        if(client_util_debug>2) printf("retrieveValue: found cur_node name %s\n",cur_node->name);
       } else {
-        if(debug!=0) printf("couldn't find cur_node\n");
+        if(client_util_debug>2) printf("retrieveValue: couldn't find cur_node\n");
         break;
       }
 
@@ -667,432 +704,25 @@
     if(cur_node !=NULL) {
       value_str  = xmlNodeListGetString(doc,cur_node->children,0);
       if(value_str!=NULL) {
-        if(debug!=0) printf("Found value %s\n",value_str);
+        if(client_util_debug>2) printf("retrieveValue: Found value %s\n",value_str);
         if(strlen((char*) value_str)>=MAX) {
-  	  if(debug!=0) printf("the value for tags=%s is %ld i.e. larger than MAX=%d, return no value!\n",tags,strlen((char*)value_str),MAX);
+  	  if(client_util_debug>2) printf("retrieveValue: the value for tags=%s is %ld i.e. larger than MAX=%d, return no value!\n",tags,strlen((char*)value_str),MAX);
           value_str = NULL;
         } else {
-	  if(debug!=0) printf("copy the value_str=\"%s\" (%ld) to %p\n",value_str,strlen((char*)value_str),value);
+	  if(client_util_debug>2) printf("retrieveValue: copy the value_str=\"%s\" (%ld) to %p\n",value_str,strlen((char*)value_str),value);
           strcpy((char*)value,(char*)value_str);
         }
       } else {
         value_str = NULL;
-        if(debug!=0) printf("Found no value for tags %s\n",tags);
+        if(client_util_debug>2) printf("retrieveValue: Found no value for tags %s\n",tags);
       }
     } else {
 	value_str = NULL;
-	if(debug!=0) printf("cur_node is null so no value found for tags=%s\n",tags);
+	if(client_util_debug>2) printf("retrieveValue: cur_node is null so no value found for tags=%s\n",tags);
     }
   }
   if(value_str!=NULL) {
-    if(debug!=0) printf("free value_str\n");
+    if(client_util_debug>1) printf("retrieveValue: free value_str\n");
     xmlFree(value_str);
   }
 }
-
-
-/*
-
-char* retrieveTagTagValue(xmlDoc* doc, xmlNode* node, char* tag, char* tag_mother) {
-  xmlNode* cur_node = NULL;
-  char* temp_str = NULL;
-  xmlChar* key;
-
-  for(cur_node = node; cur_node; cur_node = cur_node->next) {
-    if(temp_str!=NULL) {
-      //printf("stop at cur_node %s prev %s parent %s  \n",cur_node->name,cur_node->prev->name,cur_node->parent->name);
-      break;
-    }
-    //printf("comparing %s with tag %s\n",cur_node->name,tag);
-    if( (!xmlStrcmp(cur_node->name,(const xmlChar*)tag)) ) {
-      //if( strstr(cur_node->name,tag)!=0 ) {
-      //printf("getting temp_str\n");
-      xmlNode* mother_node = cur_node->parent;
-      if(mother_node!=NULL) {
-        //printf("comparing mother %s with tag %s\n",mother_node->name,tag_mother);
-        if( (!xmlStrcmp(mother_node->name,(const xmlChar*)tag_mother)) ) {
-          key  = xmlNodeListGetString(doc,cur_node->xmlChildrenNode,1);
-          if(key!=NULL) {
-            //printf("key %s\n",key);
-            temp_str = (char*) malloc(strlen((char*)key)*sizeof(char));
-            strcpy(temp_str,(char*)key);
-            xmlFree(key);
-            //printf("temp_str %s at %p\n",temp_str,temp_str);
-          }
-        }
-      }
-
-      if(temp_str!=NULL) {
-        //printf("found it at name %s \n",cur_node->name);
-        return temp_str;
-      }
-
-    }
-    temp_str = retrieveTagTagValue(doc,cur_node->children,tag,tag_mother);
-  }
-  return temp_str;
- }
-
-
-
-char* retrieveTemp(xmlDoc* doc, xmlNode* node, int rce, int hyb) {
-  xmlNode* cur_node = NULL;
-  char* temp_str = NULL;
-
-  for(cur_node = node; cur_node; cur_node = cur_node->next) {
-    if(temp_str!=NULL) {
-      //printf("stop before processing name %s \n",node->name);
-      break;
-    }
-    //printf("looking at %s\n",cur_node->name);
-    if( !xmlStrcmp(cur_node->name,(const xmlChar*)"cntrlFpga") ) {
-      //printf("getting temp_str\n");
-      temp_str = getTemp(doc,cur_node,rce,hyb);
-      //printf("temp_str %s at %p\n",temp_str,temp_str);
-    }
-
-    if(temp_str!=NULL) {
-      //printf("found it at name %s \n",cur_node->name);
-      return temp_str;
-    }
-
-    temp_str = retrieveTemp(doc,cur_node->children,rce,hyb);
-
-  }
-
-  //printf("returning %s at %p from retrieveTemp\n",temp_str,temp_str);
-
-  return temp_str;
- }
-
-
-
-
-
-
-
-char* getTemp(xmlDoc* doc, xmlNode* cur, int rce, int hyb) {
-
-  xmlChar* key;
-  char* key_copy = NULL;
-  xmlChar str[20];
-  sprintf(str,"Temp_%d_%d",rce,hyb);
-  //printf("looking for %s in children to %s \n",str,cur->name);
-  cur = cur->xmlChildrenNode;
-  while( cur != NULL) {
-    if( !xmlStrcmp(cur->name,(const xmlChar*)str) ) {
-      key  = xmlNodeListGetString(doc,cur->xmlChildrenNode,1);
-      //printf("key at %p\n",key);
-      if( key != NULL) {
-        //printf("Hybrid %s: %s\n",str,key);
-        key_copy = (char*) malloc(strlen((char*)key)*sizeof(char));
-        strcpy(key_copy,(char*)key);
-        xmlFree(key);
-        //printf("return key_copy %s at %p\n",key_copy,key_copy);
-        return key_copy;
-      } else {
-        //printf("no valid key\n");
-      }
-      xmlFree(key);
-    }
-    cur = cur->next;
-  }
-  return NULL;
-}
-
-
-char* retrieveTagValue(xmlDoc* doc, xmlNode* node, char* tag) {
-  xmlNode* cur_node = NULL;
-  char* temp_str = NULL;
-  xmlChar* key;
-
-  for(cur_node = node; cur_node; cur_node = cur_node->next) {
-    if(temp_str!=NULL) {
-      //printf("stop at cur_node %s prev %s  \n",cur_node->name,cur_node->prev->name);
-      break;
-    }
-    //printf("comparing %s with tag %s\n",cur_node->name,tag);
-    if( (!xmlStrcmp(cur_node->name,(const xmlChar*)tag)) ) {
-      //printf("getting temp_str\n");
-      key  = xmlNodeListGetString(doc,cur_node->xmlChildrenNode,1);
-      if(key!=NULL) {
-        //printf("key (%d) \"%s\"\n",strlen((char*)key),key);
-        temp_str = (char*) malloc(strlen((char*)key)*sizeof(char));
-        strcpy(temp_str,key);
-        xmlFree(key);
-        //printf("temp_str %s at %p\n",temp_str,temp_str);
-      }
-    }
-
-    if(temp_str!=NULL) {
-      //printf("found it at name %s \n",cur_node->name);
-      return temp_str;
-    }
-
-    temp_str = retrieveTagValue(doc,cur_node->children,tag);
-
-  }
-
-  return temp_str;
-}
-
-
-
-
-char* retrieveTemp(xmlDoc* doc, xmlNode* node, int rce, int hyb) {
-  xmlNode* cur_node = NULL;
-  char* temp_str = NULL;
-
-  for(cur_node = node; cur_node; cur_node = cur_node->next) {
-    if(temp_str!=NULL) {
-      //printf("stop before processing name %s \n",node->name);
-      break;
-    }
-    //printf("looking at %s\n",cur_node->name);
-    if( !xmlStrcmp(cur_node->name,(const xmlChar*)"cntrlFpga") ) {
-      //printf("getting temp_str\n");
-      temp_str = getTemp(doc,cur_node,rce,hyb);
-      //printf("temp_str %s at %p\n",temp_str,temp_str);
-    }
-
-    if(temp_str!=NULL) {
-      //printf("found it at name %s \n",cur_node->name);
-      return temp_str;
-    }
-
-    temp_str = retrieveTemp(doc,cur_node->children,rce,hyb);
-
-  }
-
-  //printf("returning %s at %p from retrieveTemp\n",temp_str,temp_str);
-
-  return temp_str;
- }
-
-
-
-
-
-
-char* copy_substr(char* buf_src, int* nread, const char* str_start, const char* str_end) {
-
-  char* p_start = strstr(buf_src,str_start);
-  
-  if(p_start == NULL) {
-    printf("start string \"%s\" not in string\n",str_start);
-    return 0;
-  }
-  
-  char* p_end = strstr(buf_src,str_end);
-  
-  if(p_end == NULL) {
-    printf("end string \"%s\" not in string\n",str_end);
-    char str_end2[sizeof(str_end)+2];
-    sprintf(str_end2,"%s\n",str_end);
-    p_end = strstr(buf_src,str_end2);
-    if(p_end == NULL) {
-      printf("end string \"%s\" not in string\n",str_end2);
-      return 0;
-    }
-  }
-
-  *nread = p_end-p_start;
-
-  char* buf_dest = (char*) malloc(*nread);
-
-  strncpy(buf_dest, p_start, *nread);
-  return buf_dest;
-}
-
-
-
-char* copy_to_array(int n, int len, char* xml_buffer[]) {
-  const unsigned int BUFFER_SIZE = n*len;
-  char* buffer = (char*) malloc(BUFFER_SIZE);
-  int i;
-  for(i=0;i<n;i++) {
-    int offset = i*len;
-    memcpy(buffer+offset, xml_buffer[i], len);
-  }
-  return buffer;
-}
-
-
-char* read_xml(int *sockfd, int* len) {
-
-  // loop over input until I see a full cycle of the system
-  // then put into a character array
-
-  const unsigned int BUFFER_SIZE = 1024;
-  const unsigned int XML_BUFFERS = 20;
-  xmlChar buffer[BUFFER_SIZE];
-  xmlChar* xml_buffer[XML_BUFFERS];
-  int j;
-  int n;
-  unsigned int i = 0;
-  unsigned int open_system = 0;
-  int buffer_offset= 0;
-  
-
-  for(j=0;j<XML_BUFFERS;++j) {
-    xml_buffer[j] = NULL;
-  }
-  
-  
-  while(i<100000) { // stupid
-    bzero(buffer,BUFFER_SIZE);
-    n = read(*sockfd,buffer,BUFFER_SIZE-1);
-    if (n < 0) {
-      error("ERROR reading from socket");
-    }
-    
-    if(open_system == 0) {
-      if(strstr(buffer,"<system>") != NULL) {
-	open_system = 1;
-	buffer_offset= 0;
-	xml_buffer[buffer_offset] = (char*) malloc(BUFFER_SIZE);
-	memcpy(xml_buffer[buffer_offset],buffer,BUFFER_SIZE);	    
-      }
-      continue;
-    }
-    
-    if(open_system == 1) {	  
-      buffer_offset++;
-      xml_buffer[buffer_offset] = (char*) malloc(BUFFER_SIZE);
-      memcpy(xml_buffer[buffer_offset],buffer,BUFFER_SIZE);
-      if(strstr(buffer,"</system>") != NULL) {
-	open_system = 0;
-	break;
-      }
-    }
-    ++i;
-  }
-
-  // Now take this 2D array and put into a long 1D array
-  // this feels stupid.
-  char* buffer_long = copy_to_array(buffer_offset+1,BUFFER_SIZE,xml_buffer);
-  
-  // I think I should free up some of the buffers used in the 2D array?  
-  for(j=0;j<XML_BUFFERS;++j) {
-    if(xml_buffer[j] != NULL) {
-      free(xml_buffer[j]);
-    }
-  }
-
-  // set the length as a returned parameter
-  *len = (buffer_offset+1)*BUFFER_SIZE;
-
-  // return the long array
-  return buffer_long;
-  
-  
-  
- }
-
-
-
-
- void print_element_names(xmlNode * a_node)
-{
-  //printf("print_element_names starting with name %s\n", a_node->name);
-    xmlNode *cur_node = NULL;
-
-    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
-      if (cur_node->type == XML_ELEMENT_NODE) {
-	printf("node type: Element, name: %s\n, content: \%s\n", cur_node->name, cur_node->content);
-      }
-      
-      print_element_names(cur_node->children);
-    }
-}
-
-
-
-
-char* get_xml_substr(char* xml_buf, int n, int* n_read, const char* p_start_str, const char* p_end_str) {
-  
-  // ok, here I'm not sure why strstr doesn't work?
-  // so this is a stupid way of removing evereyting except within some 
[truncated at 1000 lines; 83 more skipped]

epics/trunk/epics_example/myexampleApp/src
dbSubExample.c 818 -> 819
--- epics/trunk/epics_example/myexampleApp/src/dbSubExample.c	2014-07-22 17:23:29 UTC (rev 818)
+++ epics/trunk/epics_example/myexampleApp/src/dbSubExample.c	2014-07-24 00:09:53 UTC (rev 819)
@@ -21,8 +21,8 @@
 int port;
 int sockfd = 0;
 int counter = 0;
-const double def_hyb_v = -1.0;
-const double def_hyb_i = -1.0;
+const double def_hyb_v = -999.9;
+const double def_hyb_i = -999.9;
 const double def_hyb_t = -999.9;
 const double def_AxiXadcTemp = -999.9;
 
@@ -51,26 +51,26 @@
   if(strlen(inpa_val)>0) {
     dbAddr paddr;
     if(dbNameToAddr(inpa_val,&paddr)!=0) {
-      printf("getIpFromRecord: dbNameToAddr for %s failed (paddr=%p)\n",inpa_val,&paddr);
+      printf("ERROR: getIpFromRecord: dbNameToAddr for %s failed (paddr=%p)\n",inpa_val,&paddr);
     }
     struct stringinRecord* recA = (stringinRecord*)paddr.precord;
-    if (mySubDebug) printf("getIpFromRecord: recA at %p\n",recA);
+    if (mySubDebug>1) printf("getIpFromRecord: recA at %p\n",recA);
     if(recA!=NULL) {
-      if (mySubDebug) printf("getIpFromRecord: recA name %s val %p\n",recA->name,recA->val);
+      if (mySubDebug>1) printf("getIpFromRecord: recA name %s val %p\n",recA->name,recA->val);
       char inpa_val2[40];
       memset(inpa_val2,0,40);
       snprintf( inpa_val2, 40, "%s", recA->val ) ;
-      if (mySubDebug) printf("getIpFromRecord: got IP %s\n",inpa_val2);
+      if (mySubDebug>1) printf("getIpFromRecord: got IP %s\n",inpa_val2);
       if(strlen(inpa_val2)<MAX) {
 	strcpy(value,inpa_val2);
       } else {
-	printf("getIpFromRecord: IP from DB is too long? %s \n",inpa_val2);
+	printf("WARNING: getIpFromRecord: IP from DB is too long? %s \n",inpa_val2);
       }
     } else {
-      printf("getIpFromRecord: cannot get IP record %s \n",inpa_val);
+      printf("WARNING: getIpFromRecord: cannot get IP record from inpa_val %s \n",inpa_val);
     }
   } else {
-      printf("getIpFromRecord: INPA string has zero length \n");    
+      printf("WARNING: getIpFromRecord: INPA string has zero length \n");    
   }
 }
 static int getPortFromRecord(subRecord* precord) {
@@ -83,50 +83,47 @@
       printf("getPortFromRecord: dbNameToAddr for %s failed (paddr=%p)\n",inpb_val,&paddr);
     }
     struct longinRecord* recA = (longinRecord*)paddr.precord;
-    if (mySubDebug) printf("getPortFromRecord: recA at %p\n",recA);
+    if (mySubDebug>1) printf("getPortFromRecord: recA at %p\n",recA);
     if(recA!=NULL) {
-      if (mySubDebug) printf("getPortFromRecord: recA name %s val %d\n",recA->name,recA->val);
+      if (mySubDebug>1) printf("getPortFromRecord: recA name %s val %d\n",recA->name,recA->val);
       return recA->val;
     } else {
-      printf("getPortFromRecord: cannot get port record %s \n",inpb_val);
+      printf("WARNING: getPortFromRecord: cannot get port record from inpb_val %s \n",inpb_val);
     }
   } else {
-      printf("getIpFromRecord: INPB string has zero length \n");    
+      printf("WARNING: getIpFromRecord: INPB string has zero length \n");    
   }
   return -1;
 }
   
 static void setupSocket(subRecord *precord) {
   process_order++;
-  if (mySubDebug) {
+  if (mySubDebug>1) {
     printf("%d: Record %s called setupSocket(%p)\n", process_order, precord->name, (void*) precord);
   }
 
   // only setup socket from DB if it's the poll record?
   if(strcmp(precord->name,"SVT:poll_xml")==0) {    
     // look for host name and port in DB
+    if (mySubDebug>1) printf("setupSocket: look for host name and port in DB\n");
     char host[40];
     int p;
     getIpFromRecord(precord,host,40);
     p = getPortFromRecord(precord);
-    if (mySubDebug) printf("Got host \"%s\" port %d\n",host,p);
-    strcpy(hostName,host);
-    port = p;
+    if (mySubDebug>1) printf("Got host \"%s\" port %d\n",host,p);
+    if(strlen(host)==0 || p<=0) {
+      printf("ERROR: setupSocket: No hostname or port found in DB.\n");
+      exit(1);
+    } else {
+      strcpy(hostName,host);
+      port = p;
+    }      
   }
-  
-  if(strlen(hostName)==0 || port<=0) {
-    printf("No hostname or port found in DB. Use default\n");
-    strcpy(hostName,hostNameDef);
-    port = portDef;
-  }
-  
+
   if(strcmp(hostName,"")==0) {
-    printf("ERROR no valid hostname found\n");
-    return;
-  }  
-  if (mySubDebug) {
-    printf("hostName %s port %d\n",hostName,port);  
-  }
+    printf("ERROR: setupSocket: no valid hostname found. Fix this.\n");
+    exit(1);
+  } 
   
 }
 
@@ -136,8 +133,6 @@
   if (mySubDebug) {
     printf("%d: Record %s called subPollInit(%p)\n", process_order, precord->name, (void*) precord);
   }
-  //setupSocket(precord);
-  
 
   return 0;
 }
@@ -149,55 +144,92 @@
   if(mySubDebug) {
     printf("Record %s called writeHybrid %s with val %f for feb_id= %d  id=%d ch_name=%s\n", precord->name,action,precord->val,feb_id,id,ch_name);
   }
+  time_t cur_time;
+  time_t timer;
+  time(&timer);
+  int dt;
+  // get a valid socket
+  dt = 0;
+  if(sockfd>0) {
+    if (mySubDebug) printf("writeHybrid: socket %d is already open, wait for it to close\n", sockfd);    
+    while(sockfd>0 && dt<6) {
+      time(&cur_time);
+      dt = difftime(cur_time, timer);
+      if (mySubDebug) printf("writeHybrid: socket %d is still open after %ds, sleep 1s\n", sockfd, dt);    
+      sleep(1);
+    }    
+  }
   
-  if(sockfd<=0) {
+  if(sockfd>0) {    
+    printf("WARNING: writeHybrid: socket %d was still open after %ds, don't write anything\n", sockfd, dt); 
+    return;
+  } 
+  else {    
+    if (mySubDebug) printf("writeHybrid: Opening socket: host: %s:%d\n",hostName, port);    
     setupSocket(precord);
-    if (mySubDebug) printf("Opening socket: host: %s:%d\n",hostName, port);    
     sockfd = open_socket(hostName,port);
-    if (mySubDebug) printf("Opened socket : %d\n",sockfd);        
-    
+    if (mySubDebug) printf("writeHybrid: Opened socket : %d\n",sockfd);            
   }
-    
+  
   if(sockfd<=0) {
-    //if (mySubDebug) 
-    printf("Failed to open socket in writeHybrid (host %s:%d) \n",hostName,sockfd);        
+    printf("ERROR: writeHybrid: Failed to open socket in writeHybrid (host %s:%d) \n",hostName,sockfd);        
     return;
   }
-
+  
   if(strcmp(action,"v_set_sub")==0) {    
     if(precord->val<255 && precord->val>0) {
+      
       writeHybridVTrim(sockfd,(int)precord->val, id, ch_name);    
-
-
-      if (mySubDebug) {
-	printf("Poll xml string after write\n");
-      }
       
-      pollXmlStr(sockfd);
+      if (mySubDebug) printf("writeHybrid: Poll xml string after write\n");
       
-      if (mySubDebug) {
-	printf(" Poll XML done after write.\n");
-      }
+      getXmlDoc(sockfd,0,0);
+      
+      if (mySubDebug) printf("writeHybrid:  Poll XML done after write.\n");
+      
+    } else {
+      printf("ERROR: writeHybrid: voltage trim %f is not allowed!\n",precord->val);
+      exit(1);
+    }
+    
+  } 
+  else if(strcmp(action,"switch_sub")==0) {    
 
+    if(strcmp(ch_name,"all")==0) {
+      
+      int val = (int)precord->val;
+      if(val==0 || val==1) {
+	
+	writeHybridVSwitch(sockfd, val, id);    
+	
+	if (mySubDebug) printf("writeHybrid: Poll xml string after write\n");
 
+	getXmlDoc(sockfd,0,0);
 
+	if (mySubDebug) printf("writeHybrid:  Poll XML done after write.\n");
+	
+      } else {
+	printf("ERROR: writeHybrid: voltage switch %d is not allowed!\n",val);
+	exit(1);
+      }
+
     } else {
-      printf("ERROR: voltage trim %f is not allowed!\n",precord->val);
-    }
+      printf("WARNING: writeHybrid: this ch_name %s for action %s is not defined yet\n",ch_name,action);
+    }    
   }
   else {
-    printf("ERROR: this action \"%s\" for writeHybrid is not defined!\n",action);
+    printf("ERROR: writeHybrid: this action \"%s\" for writeHybrid is not defined!\n",action);
+    exit(1);
   }
   
   if (mySubDebug) {
-    printf(" Close socket\n");
+    printf("writeHybrid: Closing socket\n");
   }
   
   sockfd = close_socket(sockfd);
   
   if (mySubDebug) {
-    printf(" sleep after closing (%d)\n",sockfd);
-    sleep(1);
+    printf("writeHybrid:  after closing socket is %d\n",sockfd);
   }
 
 }
@@ -343,14 +375,24 @@
   } 
   
   if(strcmp(board_type,"hyb")==0) {
-    if(strcmp(ch_name,"dvdd")!=0 && strcmp(ch_name,"avdd")!=0 && strcmp(ch_name,"v125")!=0) {
-      printf("ERROR wrong option for hybrid ch: %s\n",ch_name);
-      return 0;
-    }
+   
     if(strcmp(action,"vn_sub")==0 || strcmp(action,"vf_sub")==0 || strcmp(action,"i_rd_sub")==0 || strcmp(action,"v_set_rd_sub")==0) {
+
+      if(strcmp(ch_name,"dvdd")!=0 && strcmp(ch_name,"avdd")!=0 && strcmp(ch_name,"v125")!=0) {
+	printf("ERROR wrong option for hybrid ch: %s\n",ch_name);
+	return 0;
+      }
       readHybrid(precord,action,(int)li_id,feb_id,ch_name);  
-    } else if(strcmp(action,"v_set_sub")==0) { 
+      
+    } else if(strcmp(action,"v_set_sub")==0 || strcmp(action,"switch_sub")==0) { 
+
+      if(strcmp(ch_name,"dvdd")!=0 && strcmp(ch_name,"avdd")!=0 && strcmp(ch_name,"v125")!=0 && strcmp(ch_name,"all")!=0) {
+	printf("ERROR wrong option for hybrid ch: %s\n",ch_name);
+	return 0;
+      }
+
       writeHybrid(precord,action,(int)li_id,feb_id,ch_name);  
+
     } else {
       printf("ERROR this hybrid action type is not valid \"%s\"\n",action);
       return 0;
@@ -469,78 +511,77 @@
 
 
 static long subPollProcess(subRecord *precord) {
+  time_t cur_time;
+  int dt;
+  time_t timer;
+
   process_order++;
-  if (mySubDebug) {
+  if (mySubDebug>0) {
     printf("%d: Record %s called subPollProcess(%p)\n",process_order, precord->name, (void*) precord);
-    printf("sockfd %d\n",sockfd);
   }
-
-  if(sockfd<=0) {
-    setupSocket(precord);
-    printf("Re-opening socket: host: %s:%d\n",hostName, port);    
-    sockfd = open_socket(hostName,port);
-    printf("Opened socket : %d\n",sockfd);    
+  
+  // check that the socket is available
+  dt=0;
+  time(&timer);
+  if(sockfd>0) {
+    if (mySubDebug>0) printf("subPollProcess: socket %d is already open, wait for it to close\n", sockfd);    
+    while(sockfd>0 && dt<6) {
+      time(&cur_time);
+      dt = difftime(cur_time, timer);
+      if (mySubDebug>0) printf("subPollProcess: socket %d is still open (%ds)\n", sockfd, dt);    
+      sleep(1);
+    }    
   }
   
-  if (mySubDebug) {
-    printf("Poll xml string\n");
+  if(sockfd>0) {    
+    printf("WARNING: subPollProcess: socket %d was still open after %ds, don't write anything\n", sockfd, dt); 
+    return 0;
+  } 
+  else {
+
+    if (mySubDebug>0) printf("subPollProcess: socket is available now (socket %d after %ds)\n", sockfd, dt); 
+    // get a valid socket
+    while(sockfd<=0 && dt<6) {
+      time(&cur_time);
+      dt = difftime(cur_time, timer);
+      if (mySubDebug>0) printf("subPollProcess: try to setup socket (%ds)\n",dt);
+
+      setupSocket(precord);
+
+      sockfd = open_socket(hostName,port);
+      
+      if(sockfd<=0) {
+	if (mySubDebug>0) printf("subPollProcess: couln't get socket, sleep before retrying\n");	
+	sleep(1);
+      }
+    }
+
   }
   
-  pollXmlStr(sockfd);
+  // only poll the xml string if the socket is open 
+  
+  if(sockfd>0) {
     
-  if (mySubDebug) {
-    printf(" Poll XML done. Close socket\n");
+    if (mySubDebug) printf("subPollProcess: Poll xml string\n");
+    
+    getXmlDoc(sockfd,1,0);
+    
+    if (mySubDebug) printf("subPollProcess: Poll XML done. Close socket\n");
+    
+    sockfd = close_socket(sockfd);
+  } 
+  else {
+    printf("subPollProcess: couldn't open a socket (tried over %ds period). Check host and port?\n",dt);
   }
   
-  sockfd = close_socket(sockfd);
-  
   return 0;
 }
 
 
 
-static long mySubInit(subRecord *precord)
-{
-  process_order++;
-    if (mySubDebug) {
-        printf("%d: Record %s called mySubInit(%p)\n", process_order, precord->name, (void*) precord);
-	}
-    return 0;
-}
 
-static long mySubProcess(subRecord *precord)
-{
- process_order++;
-    if (mySubDebug) {
-        printf("%d: Record %s called mySubProcess(%p)\n",
-	       process_order, precord->name, (void*) precord);
-    }
-    precord->val++;
-    counter++;
-    return 0;
-}
 
 
-
-
-static long myAsubInit(aSubRecord *precord)
-{
-process_order++;
-    if (mySubDebug)
-        printf("%dRecord %s called myAsubInit(%p)\n",
-               process_order, precord->name, (void*) precord);
-    return 0;
-}
-
-static long myAsubProcess(aSubRecord *precord)
-{
-process_order++;
-    if (mySubDebug)
-        printf("%d: Record %s called myAsubProcess(%p)\n",
-               process_order, precord->name, (void*) precord);
-    return 0;
-}
-
 /* Register these symbols for use by IOC code: */
 
 epicsExportAddress(int, mySubDebug);
@@ -550,7 +591,3 @@
 epicsRegisterFunction(subTempProcess);
 epicsRegisterFunction(subPollInit);
 epicsRegisterFunction(subPollProcess);
-epicsRegisterFunction(mySubInit);
-epicsRegisterFunction(mySubProcess);
-epicsRegisterFunction(myAsubInit);
-epicsRegisterFunction(myAsubProcess);

epics/trunk/epics_example/myexampleApp/src
dbSubExample.dbd 818 -> 819
--- epics/trunk/epics_example/myexampleApp/src/dbSubExample.dbd	2014-07-22 17:23:29 UTC (rev 818)
+++ epics/trunk/epics_example/myexampleApp/src/dbSubExample.dbd	2014-07-24 00:09:53 UTC (rev 819)
@@ -1,11 +1,7 @@
 variable(mySubDebug)
-function(mySubInit)
-function(mySubProcess)
 function(subLVInit)
 function(subLVProcess)
 function(subTempInit)
 function(subTempProcess)
 function(subPollInit)
 function(subPollProcess)
-function(myAsubInit)
-function(myAsubProcess)
SVNspam 0.1