1
/* INDI Server for protocol version 1.7.
2
* Copyright (C) 2007 Elwood C. Downey ecdowney@clearskyinstitute.com
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
* argv lists names of Driver programs to run or sockets to connect for Devices.
19
* Drivers are restarted if they exit or connection closes.
20
* Each local Driver's stdin/out are assumed to provide INDI traffic and are
21
* connected here via pipes. Local Drivers' stderr are connected to our
22
* stderr with date stamp and driver name prepended.
23
* We only support Drivers that advertise support for one Device. The problem
24
* with multiple Devices in one Driver is without a way to know what they
25
* _all_ are there is no way to avoid sending all messages to all Drivers.
26
* Outbound messages are limited to Devices and Properties seen inbound.
27
* Messages to Devices on sockets always include Device so the chained
28
* indiserver will only pass back info from that Device.
29
* All newXXX() received from one Client are echoed to all other Clients who
30
* have shown an interest in the same Device and property.
32
* Implementation notes:
34
* We fork each driver and open a server socket listening for INDI clients.
35
* Then forever we listen for new clients and pass traffic between clients and
36
* drivers, subject to optimizations based on sniffing messages for matching
37
* Devices and Properties. Since one message might be destined to more than
38
* one client or device, they are queued and only removed after the last
39
* consumer is finished. XMLEle are converted to linear strings before being
40
* sent to optimize write system calls and avoid blocking to slow clients.
41
* Clients that get more than maxqsiz bytes behind are shut down.
54
#include <sys/types.h>
56
#include <sys/socket.h>
57
#include <netinet/in.h>
59
#include <arpa/inet.h>
65
#define INDIPORT 7624 /* default TCP/IP port to listen */
66
#define REMOTEDVR (-1234) /* invalid PID to flag remote drivers */
67
#define MAXRBUF 4096 /* max read buffering here */
68
#define MAXWSIZ 4096 /* max bytes/write */
69
#define DEFMAXQSIZ 10 /* default max q behind, MB */
71
/* associate a usage count with queuded client or device message */
73
int count; /* number of consumers left */
74
unsigned int cl; /* content length */
75
char *cp; /* content: buf or malloced */
76
char buf[MAXWSIZ]; /* local buf for most messages */
79
/* BLOB handling, NEVER is the default */
80
typedef enum {B_NEVER=0, B_ALSO, B_ONLY} BLOBHandling;
82
/* device + property name */
84
char dev[MAXINDIDEVICE];
85
char name[MAXINDINAME];
88
/* record of each snooped property */
91
BLOBHandling blob; /* when to snoop BLOBs */
94
/* info for each connected client */
96
int active; /* 1 when this record is in use */
97
Property *props; /* malloced array of props we want */
98
int nprops; /* n entries in props[] */
99
int allprops; /* saw getProperties w/o device */
100
BLOBHandling blob; /* when to send setBLOBs */
101
int s; /* socket for this client */
102
LilXML *lp; /* XML parsing context */
103
FQ *msgq; /* Msg queue */
104
unsigned int nsent; /* bytes of current Msg sent so far */
106
static ClInfo *clinfo; /* malloced pool of clients */
107
static int nclinfo; /* n total (not active) */
109
/* info for each connected driver */
111
const char *name; /* persistent malloced name */
112
char dev[MAXINDIDEVICE]; /* device served by this driver */
113
Snoopee *sprops; /* malloced array of props we snoop */
114
int nsprops; /* n entries in sprops[] */
115
int pid; /* process id or REMOTEDVR if remote */
116
int rfd; /* read pipe fd */
117
int wfd; /* write pipe fd */
118
int efd; /* stderr from driver, if local */
119
int restarts; /* times process has been restarted */
120
LilXML *lp; /* XML parsing context */
121
FQ *msgq; /* Msg queue */
122
unsigned int nsent; /* bytes of current Msg sent so far */
124
static DvrInfo *dvrinfo; /* malloced array of drivers */
125
static int ndvrinfo; /* n total */
127
static char *me; /* our name */
128
static int port = INDIPORT; /* public INDI port */
129
static int verbose; /* chattiness */
130
static int lsocket; /* listen socket */
131
static char *ldir; /* where to log driver messages */
132
static int maxqsiz = (DEFMAXQSIZ*1024*1024); /* kill if these bytes behind */
134
static void logStartup(int ac, char *av[]);
135
static void usage (void);
136
static void noZombies (void);
137
static void noSIGPIPE (void);
138
static void indiRun (void);
139
static void indiListen (void);
140
static void newClient (void);
141
static int newClSocket (void);
142
static void shutdownClient (ClInfo *cp);
143
static int readFromClient (ClInfo *cp);
144
static void startDvr (DvrInfo *dp);
145
static void startLocalDvr (DvrInfo *dp);
146
static void startRemoteDvr (DvrInfo *dp);
147
static int openINDIServer (char host[], int indi_port);
148
static void restartDvr (DvrInfo *dp);
149
static void q2RDrivers (const char *dev, Msg *mp, XMLEle *root);
150
static void q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp,
152
static int q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name,
153
Msg *mp, XMLEle *root);
154
static void q2NDrivers (int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root);
155
static void addSDevice (DvrInfo *dp, const char *dev, const char *name);
156
static Snoopee *findSDevice (DvrInfo *dp, const char *dev, const char *name);
157
static void addClDevice (ClInfo *cp, const char *dev, const char *name);
158
static int findClDevice (ClInfo *cp, const char *dev, const char *name);
159
static int readFromDriver (DvrInfo *dp);
160
static int stderrFromDriver (DvrInfo *dp);
161
static int msgQSize (FQ *q);
162
static void setMsgXMLEle (Msg *mp, XMLEle *root);
163
static void setMsgStr (Msg *mp, char *str);
164
static void freeMsg (Msg *mp);
165
static Msg *newMsg (void);
166
static int sendClientMsg (ClInfo *cp);
167
static int sendDriverMsg (DvrInfo *cp);
168
static void crackBLOB (char *enableBLOB, BLOBHandling *bp);
169
static void traceMsg (XMLEle *root);
170
static char *indi_tstamp (char *s);
171
static void logDMsg (XMLEle *root, const char *dev);
172
static void Bye(void);
175
main (int ac, char *av[])
184
while ((--ac > 0) && ((*++av)[0] == '-')) {
186
for (s = av[0]+1; *s != '\0'; s++)
190
fprintf (stderr, "-l requires log directory\n");
198
fprintf (stderr, "-m requires max MB behind\n");
201
maxqsiz = 1024*1024*atoi(*++av);
206
fprintf (stderr, "-p requires port value\n");
220
/* at this point there are ac args in av[] to name our drivers */
224
/* take care of some unixisms */
228
/* realloc seed for client pool */
229
clinfo = (ClInfo *) malloc (1);
232
/* create driver info array all at once since size never changes */
234
dvrinfo = (DvrInfo *) calloc (ndvrinfo, sizeof(DvrInfo));
236
/* start each driver */
238
dvrinfo[ac].name = *av++;
239
startDvr (&dvrinfo[ac]);
242
/* announce we are online */
245
/* handle new clients and all io */
250
fprintf (stderr, "unexpected return from main\n");
254
/* record we have started and our args */
256
logStartup(int ac, char *av[])
260
fprintf (stderr, "%s: startup: ", indi_tstamp(NULL));
261
for (i = 0; i < ac; i++)
262
fprintf (stderr, "%s ", av[i]);
263
fprintf (stderr, "\n");
266
/* print usage message and exit (2) */
270
fprintf (stderr, "Usage: %s [options] driver [driver ...]\n", me);
271
fprintf (stderr, "Purpose: server for local and remote INDI drivers\n");
272
fprintf (stderr, "Code %s. Protocol %g.\n", "$Revision: 726523 $", INDIV);
273
fprintf (stderr, "Options:\n");
274
fprintf (stderr, " -l d : log driver messages to <d>/YYYY-MM-DD.islog\n");
275
fprintf (stderr, " -m m : kill client if gets more than this many MB behind, default %d\n", DEFMAXQSIZ);
276
fprintf (stderr, " -p p : alternate IP port, default %d\n", INDIPORT);
277
fprintf (stderr, " -v : show key events, no traffic\n");
278
fprintf (stderr, " -vv : -v + key message content\n");
279
fprintf (stderr, " -vvv : -vv + complete xml\n");
280
fprintf (stderr, "driver : executable or device@host[:port]\n");
285
/* arrange for no zombies if drivers die */
290
sa.sa_handler = SIG_IGN;
291
sigemptyset(&sa.sa_mask);
293
sa.sa_flags = SA_NOCLDWAIT;
297
(void)sigaction(SIGCHLD, &sa, NULL);
300
/* turn off SIGPIPE on bad write so we can handle it inline */
305
sa.sa_handler = SIG_IGN;
306
sigemptyset(&sa.sa_mask);
307
(void)sigaction(SIGPIPE, &sa, NULL);
310
/* start the given INDI driver process or connection.
314
startDvr (DvrInfo *dp)
316
if (strchr (dp->name, '@'))
322
/* start the given local INDI driver process.
326
startLocalDvr (DvrInfo *dp)
330
int rp[2], wp[2], ep[2];
333
/* build three pipes: r, w and error*/
335
fprintf (stderr, "%s: read pipe: %s\n", indi_tstamp(NULL),
340
fprintf (stderr, "%s: write pipe: %s\n", indi_tstamp(NULL),
345
fprintf (stderr, "%s: stderr pipe: %s\n", indi_tstamp(NULL),
350
/* fork&exec new process */
353
fprintf (stderr, "%s: fork: %s\n", indi_tstamp(NULL), strerror(errno));
357
/* child: exec name */
361
dup2 (wp[0], 0); /* driver stdin reads from wp[0] */
362
dup2 (rp[1], 1); /* driver stdout writes to rp[1] */
363
dup2 (ep[1], 2); /* driver stderr writes to e[]1] */
364
for (fd = 3; fd < 100; fd++)
367
/* go -- should never return */
368
execlp (dp->name, dp->name, NULL);
369
fprintf (stderr, "%s: Driver %s: execlp: %s\n", indi_tstamp(NULL),
370
dp->name, strerror(errno));
371
_exit (1); /* parent will notice EOF shortly */
374
/* don't need child's side of pipes */
379
/* record pid, io channels, init lp and snoop list */
384
dp->lp = newLilXML();
386
dp->sprops = (Snoopee*) malloc (1); /* seed for realloc */
390
/* first message primes driver to report its properties -- dev known
394
pushFQ (dp->msgq, mp);
396
sprintf (buf, "<getProperties device='%s' version='%g'/>\n",
399
sprintf (buf, "<getProperties version='%g'/>\n", INDIV);
404
fprintf (stderr, "%s: Driver %s: pid=%d rfd=%d wfd=%d efd=%d\n",
405
indi_tstamp(NULL), dp->name, dp->pid, dp->rfd, dp->wfd, dp->efd);
408
/* start the given remote INDI driver connection.
412
startRemoteDvr (DvrInfo *dp)
418
int indi_port, sockfd;
420
/* extract host and port */
421
indi_port = INDIPORT;
422
if (sscanf (dp->name, "%[^@]@%[^:]:%d", dev, host, &indi_port) < 2) {
423
fprintf (stderr, "Bad remote device syntax: %s\n", dp->name);
428
sockfd = openINDIServer (host, indi_port);
430
/* record flag pid, io channels, init lp and snoop list */
434
dp->lp = newLilXML();
436
dp->sprops = (Snoopee*) malloc (1); /* seed for realloc */
440
/* N.B. storing name now is key to limiting outbound traffic to this
443
strncpy (dp->dev, dev, MAXINDIDEVICE-1);
444
dp->dev[MAXINDIDEVICE-1] = '\0';
446
/* Sending getProperties with device lets remote server limit its
447
* outbound (and our inbound) traffic on this socket to this device.
450
pushFQ (dp->msgq, mp);
451
sprintf (buf, "<getProperties device='%s' version='%g'/>\n",
457
fprintf (stderr, "%s: Driver %s: socket=%d\n", indi_tstamp(NULL),
461
/* open a connection to the given host and port or die.
465
openINDIServer (char host[], int indi_port)
467
struct sockaddr_in serv_addr;
471
/* lookup host address */
472
hp = gethostbyname (host);
474
fprintf (stderr, "gethostbyname(%s): %s\n", host, strerror(errno));
478
/* create a socket to the INDI server */
479
(void) memset ((char *)&serv_addr, 0, sizeof(serv_addr));
480
serv_addr.sin_family = AF_INET;
481
serv_addr.sin_addr.s_addr =
482
((struct in_addr *)(hp->h_addr_list[0]))->s_addr;
483
serv_addr.sin_port = htons(indi_port);
484
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
485
fprintf (stderr, "socket(%s,%d): %s\n", host, indi_port,strerror(errno));
490
if (connect (sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){
491
fprintf (stderr, "connect(%s,%d): %s\n", host,indi_port,strerror(errno));
499
/* create the public INDI Driver endpoint lsocket on port.
500
* return server socket else exit.
505
struct sockaddr_in serv_socket;
509
/* make socket endpoint */
510
if ((sfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
511
fprintf (stderr, "%s: socket: %s\n", indi_tstamp(NULL), strerror(errno));
515
/* bind to given port for any IP address */
516
memset (&serv_socket, 0, sizeof(serv_socket));
517
serv_socket.sin_family = AF_INET;
519
serv_socket.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
521
serv_socket.sin_addr.s_addr = htonl (INADDR_ANY);
523
serv_socket.sin_port = htons ((unsigned short)port);
524
if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
525
fprintf (stderr, "%s: setsockopt: %s\n", indi_tstamp(NULL),
529
if (bind(sfd,(struct sockaddr*)&serv_socket,sizeof(serv_socket)) < 0){
530
fprintf (stderr, "%s: bind: %s\n", indi_tstamp(NULL), strerror(errno));
534
/* willing to accept connections with a backlog of 5 pending */
535
if (listen (sfd, 5) < 0) {
536
fprintf (stderr, "%s: listen: %s\n", indi_tstamp(NULL), strerror(errno));
543
fprintf (stderr, "%s: listening to port %d on fd %d\n",
544
indi_tstamp(NULL), port, sfd);
547
/* service traffic from clients and drivers */
555
/* init with no writers or readers */
559
/* always listen for new clients */
560
FD_SET(lsocket, &rs);
563
/* add all client readers and client writers with work to send */
564
for (i = 0; i < nclinfo; i++) {
565
ClInfo *cp = &clinfo[i];
568
if (nFQ(cp->msgq) > 0)
575
/* add all driver readers and driver writers with work to send */
576
for (i = 0; i < ndvrinfo; i++) {
577
DvrInfo *dp = &dvrinfo[i];
578
FD_SET(dp->rfd, &rs);
581
if (dp->pid != REMOTEDVR) {
582
FD_SET(dp->efd, &rs);
586
if (nFQ(dp->msgq) > 0) {
587
FD_SET(dp->wfd, &ws);
593
/* wait for action */
594
s = select (maxfd+1, &rs, &ws, NULL, NULL);
596
fprintf (stderr, "%s: select(%d): %s\n", indi_tstamp(NULL), maxfd+1,
602
if (s > 0 && FD_ISSET(lsocket, &rs)) {
607
/* message to/from client? */
608
for (i = 0; s > 0 && i < nclinfo; i++) {
609
ClInfo *cp = &clinfo[i];
611
if (FD_ISSET(cp->s, &rs)) {
612
if (readFromClient(cp) < 0)
613
return; /* fds effected */
616
if (s > 0 && FD_ISSET(cp->s, &ws)) {
617
if (sendClientMsg(cp) < 0)
618
return; /* fds effected */
624
/* message to/from driver? */
625
for (i = 0; s > 0 && i < ndvrinfo; i++) {
626
DvrInfo *dp = &dvrinfo[i];
627
if (dp->pid != REMOTEDVR && FD_ISSET(dp->efd, &rs)) {
628
if (stderrFromDriver(dp) < 0)
629
return; /* fds effected */
632
if (s > 0 && FD_ISSET(dp->rfd, &rs)) {
633
if (readFromDriver(dp) < 0)
634
return; /* fds effected */
637
if (s > 0 && FD_ISSET(dp->wfd, &ws) && nFQ(dp->msgq) > 0) {
638
if (sendDriverMsg(dp) < 0)
639
return; /* fds effected */
645
/* prepare for new client arriving on lsocket.
654
/* assign new socket */
657
/* try to reuse a clinfo slot, else add one */
658
for (cli = 0; cli < nclinfo; cli++)
659
if (!(cp = &clinfo[cli])->active)
661
if (cli == nclinfo) {
663
clinfo = (ClInfo *) realloc (clinfo, (nclinfo+1)*sizeof(ClInfo));
665
fprintf (stderr, "no memory for new client\n");
668
cp = &clinfo[nclinfo++];
671
/* rig up new clinfo entry */
672
memset (cp, 0, sizeof(*cp));
675
cp->lp = newLilXML();
677
cp->props = malloc (1);
681
struct sockaddr_in addr;
682
socklen_t len = sizeof(addr);
683
getpeername(s, (struct sockaddr*)&addr, &len);
684
fprintf(stderr,"%s: Client %d: new arrival from %s:%d - welcome!\n",
685
indi_tstamp(NULL), cp->s, inet_ntoa(addr.sin_addr),
686
ntohs(addr.sin_port));
690
/* read more from the given client, send to each appropriate driver when see
691
* xml closure. also send all newXXX() to all other interested clients.
692
* return -1 if had to shut down anything, else 0.
695
readFromClient (ClInfo *cp)
702
nr = read (cp->s, buf, sizeof(buf));
705
fprintf (stderr, "%s: Client %d: read: %s\n", indi_tstamp(NULL),
706
cp->s, strerror(errno));
707
else if (verbose > 0)
708
fprintf (stderr, "%s: Client %d: read EOF\n", indi_tstamp(NULL),
714
/* process XML, sending when find closure */
715
for (i = 0; i < nr; i++) {
717
XMLEle *root = readXMLEle (cp->lp, buf[i], err);
719
char *roottag = tagXMLEle(root);
720
const char *dev = findXMLAttValu (root, "device");
721
const char *name = findXMLAttValu (root, "name");
722
int isblob = !strcmp (tagXMLEle(root), "setBLOBVector");
726
fprintf (stderr, "%s: Client %d: read ",indi_tstamp(NULL),cp->s);
728
} else if (verbose > 1) {
729
fprintf (stderr, "%s: Client %d: read <%s device='%s' name='%s'>\n",
730
indi_tstamp(NULL), cp->s, tagXMLEle(root),
731
findXMLAttValu (root, "device"),
732
findXMLAttValu (root, "name"));
735
/* snag interested properties.
736
* N.B. don't open to alldevs if seen specific dev already, else
737
* remote client connections start returning too much.
740
addClDevice (cp, dev, name);
741
else if (!strcmp (roottag, "getProperties") && !cp->nprops)
744
/* snag enableBLOB -- send to remote drivers too */
745
if (!strcmp (roottag, "enableBLOB"))
746
crackBLOB (pcdataXMLEle(root), &cp->blob);
748
/* build a new message -- set content iff anyone cares */
751
/* send message to driver(s) responsible for dev */
752
q2RDrivers (dev, mp, root);
754
/* echo new* commands back to other clients */
755
if (!strncmp (roottag, "new", 3)) {
756
if (q2Clients (cp, isblob, dev, name, mp, root) < 0)
760
/* set message content if anyone cares else forget it */
762
setMsgXMLEle (mp, root);
768
char *ts = indi_tstamp(NULL);
769
fprintf (stderr, "%s: Client %d: XML error: %s\n", ts,
771
fprintf (stderr, "%s: Client %d: XML read: %.*s\n", ts,
778
return (shutany ? -1 : 0);
781
/* read more from the given driver, send to each interested client when see
782
* xml closure. if driver dies, try restarting.
783
* return 0 if ok else -1 if had to shut down anything.
786
readFromDriver (DvrInfo *dp)
793
nr = read (dp->rfd, buf, sizeof(buf));
796
fprintf (stderr, "%s: Driver %s: stdin %s\n", indi_tstamp(NULL),
797
dp->name, strerror(errno));
799
fprintf (stderr, "%s: Driver %s: stdin EOF\n",
800
indi_tstamp(NULL), dp->name);
805
/* process XML, sending when find closure */
806
for (i = 0; i < nr; i++) {
808
XMLEle *root = readXMLEle (dp->lp, buf[i], err);
810
char *roottag = tagXMLEle(root);
811
const char *dev = findXMLAttValu (root, "device");
812
const char *name = findXMLAttValu (root, "name");
813
int isblob = !strcmp (tagXMLEle(root), "setBLOBVector");
817
fprintf(stderr, "%s: Driver %s: read ", indi_tstamp(0),dp->name);
819
} else if (verbose > 1) {
820
fprintf (stderr, "%s: Driver %s: read <%s device='%s' name='%s'>\n",
821
indi_tstamp(NULL), dp->name, tagXMLEle(root),
822
findXMLAttValu (root, "device"),
823
findXMLAttValu (root, "name"));
826
/* that's all if driver is just registering a snoop */
827
if (!strcmp (roottag, "getProperties")) {
828
addSDevice (dp, dev, name);
833
/* that's all if driver is just registering a BLOB mode */
834
if (!strcmp (roottag, "enableBLOB")) {
835
Snoopee *sp = findSDevice (dp, dev, name);
837
crackBLOB (pcdataXMLEle (root), &sp->blob);
842
/* snag device name if not known yet */
843
if (!dp->dev[0] && dev[0]) {
844
strncpy (dp->dev, dev, MAXINDIDEVICE-1);
845
dp->dev[MAXINDIDEVICE-1] = '\0';
848
/* log messages if any and wanted */
852
/* build a new message -- set content iff anyone cares */
855
/* send to interested clients */
856
if (q2Clients (NULL, isblob, dev, name, mp, root) < 0)
859
/* send to snooping drivers */
860
q2SDrivers (isblob, dev, name, mp, root);
862
/* set message content if anyone cares else forget it */
864
setMsgXMLEle (mp, root);
870
char *ts = indi_tstamp(NULL);
871
fprintf (stderr, "%s: Driver %s: XML error: %s\n", ts,
873
fprintf (stderr, "%s: Driver %s: XML read: %.*s\n", ts,
880
return (shutany ? -1 : 0);
883
/* read more from the given driver stderr, add prefix and send to our stderr.
884
* return 0 if ok else -1 if had to restart.
887
stderrFromDriver (DvrInfo *dp)
889
static char exbuf[MAXRBUF];
894
nr = read (dp->efd, exbuf+nexbuf, sizeof(exbuf)-nexbuf);
897
fprintf (stderr, "%s: Driver %s: stderr %s\n", indi_tstamp(NULL),
898
dp->name, strerror(errno));
900
fprintf (stderr, "%s: Driver %s: stderr EOF\n",
901
indi_tstamp(NULL), dp->name);
907
/* prefix each whole line to our stderr, save extra for next time */
908
for (i = 0; i < nexbuf; i++) {
909
if (exbuf[i] == '\n') {
910
fprintf (stderr, "%s: Driver %s: %.*s\n", indi_tstamp(NULL),
912
i++; /* count including nl */
913
nexbuf -= i; /* remove from nexbuf */
914
memmove (exbuf, exbuf+i, nexbuf); /* slide remaining to front */
915
i = -1; /* restart for loop scan */
922
/* close down the given client */
924
shutdownClient (ClInfo *cp)
928
/* close connection */
929
shutdown (cp->s, SHUT_RDWR);
936
/* decrement and possibly free any unsent messages for this client */
937
while ((mp = (Msg*) popFQ(cp->msgq)) != NULL)
938
if (--mp->count == 0)
942
/* ok now to recycle */
946
fprintf (stderr, "%s: Client %d: shut down complete - bye!\n",
947
indi_tstamp(NULL), cp->s);
950
/* close down the given driver and restart */
952
restartDvr (DvrInfo *dp)
956
/* make sure it's dead, reclaim resources */
957
if (dp->pid == REMOTEDVR) {
958
/* socket connection */
959
shutdown (dp->wfd, SHUT_RDWR);
960
close (dp->wfd); /* same as rfd */
962
/* local pipe connection */
963
kill (dp->pid, SIGKILL); /* we've insured there are no zombies */
973
/* decrement and possibly free any unsent messages for this client */
974
while ((mp = (Msg*) popFQ(dp->msgq)) != NULL)
975
if (--mp->count == 0)
979
fprintf (stderr, "%s: Driver %s: restart #%d\n", indi_tstamp(NULL),
980
dp->name, ++dp->restarts);
984
/* put Msg mp on queue of each driver responsible for dev, or all drivers
985
* if dev not specified.
988
q2RDrivers (const char *dev, Msg *mp, XMLEle *root)
993
/* queue message to each interested driver.
994
* N.B. don't send generic getProps to more than one remote driver,
995
* otherwise they all fan out and we get multiple responses back.
997
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) {
998
int isremote = (dp->pid == REMOTEDVR);
999
if (dev[0] && dp->dev[0] && strcmp (dev, dp->dev))
1000
continue; /* driver known to not support this dev */
1001
if (!dev[0] && isremote && sawremote)
1002
continue; /* already sent generic to another remote */
1006
/* ok: queue message to this driver */
1008
pushFQ (dp->msgq, mp);
1010
fprintf (stderr, "%s: Driver %s: queuing responsible for <%s device='%s' name='%s'>\n",
1011
indi_tstamp(NULL), dp->name, tagXMLEle(root),
1012
findXMLAttValu (root, "device"),
1013
findXMLAttValu (root, "name"));
1017
/* put Msg mp on queue of driver target in newXXX
1018
* if BLOB always honor current mode.
1020
/* TODO We need to echo messages back to other drivers. If no drivers found with this name, then echo it back to other clients.
1022
q2NDrivers (int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root)
1028
if (strstr(tagXMLEle(root), "new") == NULL)
1034
>>>>>>> .merge-right.r236
1036
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++)
1038
fprintf(stderr, "Checking if we can send a newXXX command to driver %s for newXXX was issued to %s, snoop_redirect is %d\n", dp->dev, dev, snoop_redirect);
1039
if (!strcmp(dp->dev, dev))
1041
if ((isblob && dp->blob==B_NEVER) || (!isblob && dp->blob==B_ONLY))
1044
fprintf(stderr, "sending newXXX to driver %s\n", dp->dev);
1045
/* ok: queue message to this device */
1047
pushFQ (dp->msgq, mp);
1051
fprintf (stderr, "%s: Driver %s: queuing snooped <%s device='%s' name='%s'>\n",
1052
indi_tstamp(NULL), dp->name, tagXMLEle(root),
1053
findXMLAttValu (root, "device"),
1054
findXMLAttValu (root, "name"));
1062
/* put Msg mp on queue of each driver snooping dev/name.
1063
* if BLOB always honor current mode.
1066
q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root)
1069
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++)
1072
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) {
1073
>>>>>>> .merge-right.r236
1074
Snoopee *sp = findSDevice (dp, dev, name);
1076
/* nothing for dp if not snooping for dev/name or wrong BLOB mode */
1079
if ((isblob && sp->blob==B_NEVER) || (!isblob && sp->blob==B_ONLY))
1082
/* ok: queue message to this device */
1084
pushFQ (dp->msgq, mp);
1086
fprintf (stderr, "%s: Driver %s: queuing snooped <%s device='%s' name='%s'>\n",
1087
indi_tstamp(NULL), dp->name, tagXMLEle(root),
1088
findXMLAttValu (root, "device"),
1089
findXMLAttValu (root, "name"));
1094
/* add dev/name to dp's snooping list.
1095
* init with blob mode set to B_NEVER.
1098
addSDevice (DvrInfo *dp, const char *dev, const char *name)
1104
sp = findSDevice (dp, dev, name);
1108
/* add dev to sdevs list */
1109
dp->sprops = (Snoopee*) realloc (dp->sprops,
1110
(dp->nsprops+1)*sizeof(Snoopee));
1111
sp = &dp->sprops[dp->nsprops++];
1114
strncpy (ip, dev, MAXINDIDEVICE-1);
1115
ip[MAXINDIDEVICE-1] = '\0';
1118
strncpy (ip, name, MAXINDINAME-1);
1119
ip[MAXINDINAME-1] = '\0';
1124
fprintf (stderr, "%s: Driver %s: snooping on %s.%s\n", indi_tstamp(NULL),
1125
dp->name, dev, name);
1128
/* return Snoopee if dp is snooping dev/name, else NULL.
1131
findSDevice (DvrInfo *dp, const char *dev, const char *name)
1135
for (i = 0; i < dp->nsprops; i++) {
1136
Snoopee *sp = &dp->sprops[i];
1137
Property *pp = &sp->prop;
1138
if (!strcmp (pp->dev, dev) &&
1139
(!pp->name[0] || !strcmp(pp->name, name)))
1146
/* put Msg mp on queue of each client interested in dev/name, except notme.
1147
* if BLOB always honor current mode.
1148
* return -1 if had to shut down any clients, else 0.
1151
q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root)
1158
/* Don't send newXXX messages to clients, for now
1159
if (strstr(tagXMLEle(root), "new"))
1162
>>>>>>> .merge-right.r236
1165
/* queue message to each interested client */
1166
for (cp = clinfo; cp < &clinfo[nclinfo]; cp++) {
1167
/* cp in use? notme? want this dev/name? blob? */
1168
if (!cp->active || cp == notme)
1170
if (findClDevice (cp, dev, name) < 0)
1172
if ((isblob && cp->blob==B_NEVER) || (!isblob && cp->blob==B_ONLY))
1175
/* shut down this client if its q is already too large */
1176
ql = msgQSize(cp->msgq);
1179
fprintf (stderr, "%s: Client %d: %d bytes behind, shutting down\n",
1180
indi_tstamp(NULL), cp->s, ql);
1181
shutdownClient (cp);
1186
/* ok: queue message to this client */
1188
pushFQ (cp->msgq, mp);
1190
fprintf (stderr, "%s: Client %d: queuing <%s device='%s' name='%s'>\n",
1191
indi_tstamp(NULL), cp->s, tagXMLEle(root),
1192
findXMLAttValu (root, "device"),
1193
findXMLAttValu (root, "name"));
1196
return (shutany ? -1 : 0);
1199
/* return size of all Msqs on the given q */
1205
for (i = 0; i < nFQ(q); i++) {
1206
Msg *mp = (Msg *) peekiFQ(q,i);
1213
/* print root as content in Msg mp.
1216
setMsgXMLEle (Msg *mp, XMLEle *root)
1218
/* want cl to only count content, but need room for final \0 */
1219
mp->cl = sprlXMLEle (root, 0);
1220
if (mp->cl < sizeof(mp->buf))
1223
mp->cp = malloc (mp->cl+1);
1224
sprXMLEle (mp->cp, root, 0);
1227
/* save str as content in Msg mp.
1230
setMsgStr (Msg *mp, char *str)
1232
/* want cl to only count content, but need room for final \0 */
1233
mp->cl = strlen (str);
1234
if (mp->cl < sizeof(mp->buf))
1237
mp->cp = malloc (mp->cl+1);
1238
strcpy (mp->cp, str);
1241
/* return pointer to one new nulled Msg
1246
return ((Msg *) calloc (1, sizeof(Msg)));
1249
/* free Msg mp and everything it contains */
1253
if (mp->cp && mp->cp != mp->buf)
1258
/* write the next chunk of the current message in the queue to the given
1259
* client. pop message from queue when complete and free the message if we are
1260
* the last one to use it. shut down this client if trouble.
1261
* N.B. we assume we will never be called with cp->msgq empty.
1262
* return 0 if ok else -1 if had to shut down.
1265
sendClientMsg (ClInfo *cp)
1270
/* get current message */
1271
mp = (Msg *) peekFQ (cp->msgq);
1273
/* send next chunk, never more than MAXWSIZ to reduce blocking */
1274
nsend = mp->cl - cp->nsent;
1275
if (nsend > MAXWSIZ)
1277
nw = write (cp->s, &mp->cp[cp->nsent], nsend);
1279
/* shut down if trouble */
1282
fprintf (stderr, "%s: Client %d: write returned 0\n",
1283
indi_tstamp(NULL), cp->s);
1285
fprintf (stderr, "%s: Client %d: write: %s\n", indi_tstamp(NULL),
1286
cp->s, strerror(errno));
1287
shutdownClient (cp);
1293
fprintf(stderr, "%s: Client %d: sending msg copy %d nq %d:\n%.*s\n",
1294
indi_tstamp(NULL), cp->s, mp->count, nFQ(cp->msgq),
1295
nw, &mp->cp[cp->nsent]);
1296
} else if (verbose > 1) {
1297
fprintf(stderr, "%s: Client %d: sending %.50s\n", indi_tstamp(NULL),
1298
cp->s, &mp->cp[cp->nsent]);
1301
/* update amount sent. when complete: free message if we are the last
1302
* to use it and pop from our queue.
1305
if (cp->nsent == mp->cl) {
1306
if (--mp->count == 0)
1315
/* write the next chunk of the current message in the queue to the given
1316
* driver. pop message from queue when complete and free the message if we are
1317
* the last one to use it. restart this driver if touble.
1318
* N.B. we assume we will never be called with dp->msgq empty.
1319
* return 0 if ok else -1 if had to shut down.
1322
sendDriverMsg (DvrInfo *dp)
1327
/* get current message */
1328
mp = (Msg *) peekFQ (dp->msgq);
1330
/* send next chunk, never more than MAXWSIZ to reduce blocking */
1331
nsend = mp->cl - dp->nsent;
1332
if (nsend > MAXWSIZ)
1334
nw = write (dp->wfd, &mp->cp[dp->nsent], nsend);
1336
/* restart if trouble */
1339
fprintf (stderr, "%s: Driver %s: write returned 0\n",
1340
indi_tstamp(NULL), dp->name);
1342
fprintf (stderr, "%s: Driver %s: write: %s\n", indi_tstamp(NULL),
1343
dp->name, strerror(errno));
1350
fprintf(stderr, "%s: Driver %s: sending msg copy %d nq %d:\n%.*s\n",
1351
indi_tstamp(NULL), dp->name, mp->count, nFQ(dp->msgq),
1352
nw, &mp->cp[dp->nsent]);
1353
} else if (verbose > 1) {
1354
fprintf(stderr, "%s: Driver %s: sending %.50s\n", indi_tstamp(NULL),
1355
dp->name, &mp->cp[dp->nsent]);
1358
/* update amount sent. when complete: free message if we are the last
1359
* to use it and pop from our queue.
1362
if (dp->nsent == mp->cl) {
1363
if (--mp->count == 0)
1372
/* return 0 if cp may be interested in dev/name else -1
1375
findClDevice (ClInfo *cp, const char *dev, const char *name)
1379
if (cp->allprops || !dev[0])
1381
for (i = 0; i < cp->nprops; i++) {
1382
Property *pp = &cp->props[i];
1383
if (!strcmp (pp->dev, dev) &&
1384
(!pp->name[0] || !strcmp(pp->name, name)))
1390
/* add the given device and property to the devs[] list of client if new.
1393
addClDevice (ClInfo *cp, const char *dev, const char *name)
1399
if (!findClDevice (cp, dev, name))
1403
cp->props = (Property *) realloc (cp->props,
1404
(cp->nprops+1)*sizeof(Property));
1405
pp = &cp->props[cp->nprops++];
1408
strncpy (ip, dev, MAXINDIDEVICE-1);
1409
ip[MAXINDIDEVICE-1] = '\0';
1412
strncpy (ip, name, MAXINDINAME-1);
1413
ip[MAXINDINAME-1] = '\0';
1417
/* block to accept a new client arriving on lsocket.
1418
* return private nonblocking socket or exit.
1423
struct sockaddr_in cli_socket;
1427
/* get a private connection to new client */
1428
cli_len = sizeof(cli_socket);
1429
cli_fd = accept (lsocket, (struct sockaddr *)&cli_socket, &cli_len);
1431
fprintf (stderr, "accept: %s\n", strerror(errno));
1439
/* convert the string value of enableBLOB to our B_ state value.
1440
* no change if unrecognized
1443
crackBLOB (char *enableBLOB, BLOBHandling *bp)
1445
if (!strcmp (enableBLOB, "Also"))
1447
else if (!strcmp (enableBLOB, "Only"))
1449
else if (!strcmp (enableBLOB, "Never"))
1453
/* print key attributes and values of the given xml to stderr.
1456
traceMsg (XMLEle *root)
1458
static const char *prtags[] = {
1459
"defNumber", "oneNumber",
1460
"defText", "oneText",
1461
"defSwitch", "oneSwitch",
1462
"defLight", "oneLight",
1465
const char *msg, *perm, *pcd;
1468
/* print tag header */
1469
fprintf (stderr, "%s %s %s %s", tagXMLEle(root),
1470
findXMLAttValu(root,"device"),
1471
findXMLAttValu(root,"name"),
1472
findXMLAttValu(root,"state"));
1473
pcd = pcdataXMLEle (root);
1475
fprintf (stderr, " %s", pcd);
1476
perm = findXMLAttValu(root,"perm");
1478
fprintf (stderr, " %s", perm);
1479
msg = findXMLAttValu(root,"message");
1481
fprintf (stderr, " '%s'", msg);
1483
/* print each array value */
1484
for (e = nextXMLEle(root,1); e; e = nextXMLEle(root,0))
1485
for (i = 0; i < sizeof(prtags)/sizeof(prtags[0]); i++)
1486
if (strcmp (prtags[i], tagXMLEle(e)) == 0)
1487
fprintf (stderr, "\n %10s='%s'", findXMLAttValu(e,"name"),
1490
fprintf (stderr, "\n");
1493
/* fill s with current UT string.
1494
* if no s, use a static buffer
1495
* return s or buffer.
1496
* N.B. if use our buffer, be sure to use before calling again
1499
indi_tstamp (char *s)
1501
static char sbuf[64];
1509
strftime (s, sizeof(sbuf), "%Y-%m-%dT%H:%M:%S", tp);
1513
/* log message in root known to be from device dev to ldir, if any.
1516
logDMsg (XMLEle *root, const char *dev)
1520
const char *ts, *ms;
1523
/* get message, if any */
1524
ms = findXMLAttValu (root, "message");
1528
/* get timestamp now if not provided */
1529
ts = findXMLAttValu (root, "timestamp");
1532
indi_tstamp (stamp);
1536
/* append to log file, name is date portion of time stamp */
1537
sprintf (logfn, "%s/%.10s.islog", ldir, ts);
1538
fp = fopen (logfn, "a");
1540
return; /* oh well */
1541
fprintf (fp, "%s: %s: %s\n", ts, dev, ms);
1545
/* log when then exit */
1549
fprintf (stderr, "%s: good bye\n", indi_tstamp(NULL));