]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.h
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / bget_msg.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19
20 #ifndef __BGET_MSG_H_
21 #define __BGET_MSG_H_
22
23 #include "bacula.h"
24
25 typedef uint64_t blockaddr;
26 typedef int64_t  blockidx;
27
28 #ifdef COMMUNITY
29 #define BLOCK_HEAD_SIZE               4       // only one int
30 #define GETMSG_MAX_BLOCK_SIZE    (65*1024-BLOCK_HEAD_SIZE)
31 #define GETMSG_MAX_HASH_SIZE     64  /* SHA512 */
32 #define GETMSG_MAX_MSG_SIZE      (GETMSG_MAX_BLOCK_SIZE+GETMSG_MAX_HASH_SIZE+sizeof(uint32_t)+OFFSET_FADDR_SIZE+100)
33 #endif
34
35
36 class bmessage: public SMARTALLOC
37 {
38 public:
39    enum { bm_none, bm_ready, bm_busy, bm_ref };
40
41    POOLMEM *msg;    // exchanged with BSOCK
42    int32_t msglen;  // length from BSOCK
43    int32_t origlen; // length before rehydration, to be compared with length in header
44    char *rbuf;      // adjusted to point to data inside *msg
45    int32_t rbuflen; // adjusted from msglen
46    int status;
47    int ret;         // return value from bget_msg()
48    int jobbytes;    // must be added to jcr->JobBytes if block is downloaded
49
50    bmessage(int bufsize);
51    virtual ~bmessage();
52    void swap(BSOCK *sock);
53 };
54
55 class GetMsg: public SMARTALLOC
56 {
57 public:
58    JCR *jcr;
59    BSOCK *bsock;
60    const char *rec_header;        /* Format of a header */
61    int32_t bufsize;               /* "ideal" bufsize from JCR */
62
63    bool m_is_stop;                /* set by the read thread when bsock->is_stop() */
64    bool m_is_done;                /* set when the read thread finish (no more record will be pushed) */
65    bool m_is_error;               /* set when the read thread get an error */
66
67    int32_t m_use_count;
68
69    pthread_mutex_t mutex;
70    pthread_cond_t cond;
71    bmessage *bmsg_aux;
72    bmessage *bmsg;   // local bmsg used by bget_msg(NULL)
73    int32_t msglen;   // used to mimic BSOCK, updated by bget_msg()
74    POOLMEM *msg;     // used to mimic BSOCK, updated by bget_msg()
75
76    void inc_use_count(void) {P(mutex); m_use_count++; V(mutex); };
77    void dec_use_count(void) {P(mutex); m_use_count--; V(mutex); };
78    int32_t use_count() { int32_t v; P(mutex); v = m_use_count; V(mutex); return v;};
79
80
81    GetMsg(JCR *a_jcr, BSOCK *a_bsock, const char *a_rec_header, int32_t a_bufsize);
82    virtual ~GetMsg();
83
84    virtual int bget_msg(bmessage **pbmsg=NULL);
85    inline virtual void *do_read_sock_thread(void) { return NULL; };
86    inline virtual int start_read_sock() { return 0; };
87    inline virtual void *wait_read_sock(int /*emergency_quit*/) { return NULL;};
88
89    virtual bool is_stop() { return (m_is_stop!=false); };
90    virtual bool is_done() { return (m_is_done!=false); };
91    virtual bool is_error(){ return (m_is_error!=false); };
92
93    bmessage *new_msg() { return New(bmessage(bufsize)); };
94
95 };
96
97 /* Call this function to release the memory associated with the message queue
98  * The reading thread is using the BufferedMsgBase to work, so we need to free
99  * the memory only when the main thread and the reading thread agree
100  */
101 inline void free_GetMsg(GetMsg *b)
102 {
103    b->dec_use_count();
104    ASSERT2(b->use_count() >= 0, "GetMsg use_count too low");
105    if (b->use_count() == 0) {
106       delete b;
107    }
108 }
109
110 #endif