]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.c
- Correct compiler complaints in wx-console and tray-monitor.
[bacula/bacula] / bacula / src / lib / bget_msg.c
1 /*
2  *  Subroutines to receive network data and handle
3  *   network signals for the FD and the SD.
4  *
5  *   Kern Sibbald, May MMI previously in src/stored/fdmsg.c
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2001-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"                   /* pull in global headers */
31
32 extern char OK_msg[];
33 extern char TERM_msg[];
34
35 #define msglvl 500
36
37 /*
38  * This routine does a bnet_recv(), then if a signal was
39  *   sent, it handles it.  The return codes are the same as
40  *   bne_recv() except the BNET_SIGNAL messages that can
41  *   be handled are done so without returning.
42  *
43  * Returns number of bytes read (may return zero)
44  * Returns -1 on signal (BNET_SIGNAL)
45  * Returns -2 on hard end of file (BNET_HARDEOF)
46  * Returns -3 on error  (BNET_ERROR)
47  */
48 int bget_msg(BSOCK *sock)
49 {
50    int n;
51    for ( ;; ) {
52       n = bnet_recv(sock);
53       if (n >= 0) {                  /* normal return */
54          return n;
55       }
56       if (is_bnet_stop(sock)) {      /* error return */
57          return n;
58       }
59
60       /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
61       switch (sock->msglen) {
62       case BNET_EOD:               /* end of data */
63          Dmsg0(msglvl, "Got BNET_EOD\n");
64          return n;
65       case BNET_EOD_POLL:
66          Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
67          if (sock->terminated) {
68             bnet_fsend(sock, TERM_msg);
69          } else {
70             bnet_fsend(sock, OK_msg); /* send response */
71          }
72          return n;                 /* end of data */
73       case BNET_TERMINATE:
74          Dmsg0(msglvl, "Got BNET_TERMINATE\n");
75          sock->terminated = 1;
76          return n;
77       case BNET_POLL:
78          Dmsg0(msglvl, "Got BNET_POLL\n");
79          if (sock->terminated) {
80             bnet_fsend(sock, TERM_msg);
81          } else {
82             bnet_fsend(sock, OK_msg); /* send response */
83          }
84          break;
85       case BNET_HEARTBEAT:
86       case BNET_HB_RESPONSE:
87          break;
88       case BNET_STATUS:
89          /* *****FIXME***** Implement BNET_STATUS */
90          Dmsg0(msglvl, "Got BNET_STATUS\n");
91          bnet_fsend(sock, "Status OK\n");
92          bnet_sig(sock, BNET_EOD);
93          break;
94       default:
95          Emsg1(M_ERROR, 0, "bget_msg: unknown signal %d\n", sock->msglen);
96          break;
97       }
98    }
99 }