diff ad9912.c @ 258:5296f3bcd160

Implement DDS clients reconnect On network send() failures try to reconnect to the server before returning an error. This allows to restart the network servers controlling the DDSes wiothout having to restart the clients.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 14:31:35 +0200
parents 9e0c3541104b
children 6c748ed6a7c5
line wrap: on
line diff
--- a/ad9912.c	Fri Jun 05 21:51:25 2015 +0200
+++ b/ad9912.c	Tue Jun 16 14:31:35 2015 +0200
@@ -1,67 +1,36 @@
 #ifdef _CVI_
-#include <winsock2.h>
 #include <ansi_c.h>
 #include <toolbox.h>
+#define usleep(t) Delay((t) / 1000000.0)
 #else
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <errno.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <ctype.h>
-#include <stdarg.h>
 #endif
 
 #include "dds.h"
+#include "xsocket.h"
 #include "ad9912.h"
 
 
 int ad9912_init(struct ad9912 *d, const char *hostname, double clock)
 {
-	int n, r;
+	int r;
 	char buffer[256];
-	int sock;
-	struct sockaddr_in addr;
-	struct hostent* host;	
 
-#ifdef _CVI_
-	WSADATA wsdata;
-	WSAStartup(MAKEWORD(2,2), &wsdata);	
-#endif
-	
-	d->hostname = strdup(hostname);
-	d->port = 1234;
 	d->clock = clock;
+	d->sock = xsocket(hostname, 1234);
 	
-	host = gethostbyname(hostname);
-	if (host == NULL)
-		return -1;
-  
-	sock = socket(AF_INET, SOCK_STREAM, 0);
-	if (sock < 0)
-		return -2;
-
-	addr.sin_family = host->h_addrtype;
-	memcpy(&addr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
-	addr.sin_port = htons(d->port);
-
-	r = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
-	if (r < 0)
-		return -3;
-	
-	d->fd = sock;
-
-	/* check compatibility */
-	n = snprintf(buffer, sizeof(buffer), "REVISION");
-	r = msend(d->fd, buffer, n);
+ 	r = xconnect(d->sock);
 	if (r < 0)
 		return r;
-	r = mrecv(d->fd, buffer, sizeof(buffer));
+	
+	/* check compatibility */
+	r = msend(d->sock, "REVISION", strlen("REVISION"));
+	if (r < 0)
+		return r;
+	r = mrecv(d->sock, buffer, sizeof(buffer));
 	if (r < 0)
 		return r;
 	if (atoi(buffer) != REVISION)
@@ -79,7 +48,7 @@
 	/* ad9912 ignores ftw last bit */
 	value = value & ~1ULL;
 
-	r = command(d->fd, "SET CH%d:FTW0 0x%llX", channel, value);
+	r = command(d->sock, "SET CH%d:FTW0 0x%llX", channel, value);
 	if (r < 0)
 		return r;
 
@@ -97,11 +66,11 @@
 
 	n = snprintf(buffer, sizeof(buffer), "GET CH%d:FTW0", channel);
 
-	r = msend(d->fd, buffer, n);
+	r = msend(d->sock, buffer, n);
 	if (r < 0)
 		return r;
 
-	r = mrecv(d->fd, buffer, sizeof(buffer));
+	r = mrecv(d->sock, buffer, sizeof(buffer));
 	if (r < 0)
 		return r;
 
@@ -141,6 +110,7 @@
 	
 	/* final adjustment */
 	ad9912_set_frequency(d, channel, f2);
+	
 	return 0;
 }