Instrument Neutral Distributed Interface INDI 2.2.5
MsgQueue.hpp
Go to the documentation of this file.
1/* INDI Server for protocol version 1.7.
2 * Copyright (C) 2007 Elwood C. Downey <[email protected]>
3 2013 Jasem Mutlaq <[email protected]>
4 2022 Ludovic Pollet
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include "lilxml.h"
22#include "Collectable.hpp"
23#include "MsgChunckIterator.hpp"
24#include "indicore/indidevapi.h"
25
26#include <ev++.h>
27#include <list>
28#include <set>
29
30class SerializedMsg;
31class Msg;
32
33class MsgQueue: public Collectable
34{
35 static constexpr unsigned maxFDPerMessage {16}; /* No more than 16 buffer attached to a message */
36 static constexpr unsigned maxReadBufferLength {49152};
37 static constexpr unsigned maxWriteBufferLength {49152};
38
39 int rFd, wFd;
40 LilXML * lp; /* XML parsing context */
41 ev::io rio, wio; /* Event loop io events */
42 void ioCb(ev::io &watcher, int revents);
43
44 // Update the status of FD read/write ability
45 void updateIos();
46
47 std::set<SerializedMsg*> readBlocker; /* The message that block this queue */
48
49 std::list<SerializedMsg*> msgq; /* To send msg queue */
50 std::list<int> incomingSharedBuffers; /* During reception, fds accumulate here */
51
52 // Position in the head message
54
55 // Handle fifo or socket case
56 size_t doRead(char * buff, size_t len);
57 void readFromFd();
58
59 /* write the next chunk of the current message in the queue to the given
60 * client. pop message from queue when complete and free the message if we are
61 * the last one to use it. shut down this client if trouble.
62 */
63 void writeToFd();
64
65 protected:
67 int getRFd() const
68 {
69 return rFd;
70 }
71 int getWFd() const
72 {
73 return wFd;
74 }
75
76 /* print key attributes and values of the given xml to stderr. */
77 void traceMsg(const std::string &log, XMLEle *root);
78
79 /* Close the connection. (May be restarted later depending on driver logic) */
80 virtual void close() = 0;
81
82 /* Close the writing part of the connection. By default, shutdown the write part, but keep on reading. May delete this */
83 virtual void closeWritePart();
84
85 /* Handle a message. root will be freed by caller. fds of buffers will be closed, unless set to -1 */
86 virtual void onMessage(XMLEle *root, std::list<int> &sharedBuffers) = 0;
87
88 /* convert the string value of enableBLOB to our B_ state value.
89 * no change if unrecognized
90 */
91 static void crackBLOB(const char *enableBLOB, BLOBHandling *bp);
92
94 public:
95 virtual ~MsgQueue();
96
97 void pushMsg(Msg * msg);
98
99 /* return storage size of all Msqs on the given q */
100 unsigned long msgQSize() const;
101
102 SerializedMsg * headMsg() const;
103 void consumeHeadMsg();
104
105 /* Remove all messages from queue */
106 void clearMsgQueue();
107
108 void messageMayHaveProgressed(const SerializedMsg * msg);
109
110 void setFds(int rFd, int wFd);
111
112 virtual bool acceptSharedBuffers() const
113 {
114 return useSharedBuffer;
115 }
116
117 virtual void log(const std::string &log) const;
118};
virtual ~MsgQueue()
Definition MsgQueue.cpp:229
static void crackBLOB(const char *enableBLOB, BLOBHandling *bp)
Definition MsgQueue.cpp:174
void consumeHeadMsg()
Definition MsgQueue.cpp:326
void messageMayHaveProgressed(const SerializedMsg *msg)
Definition MsgQueue.cpp:372
virtual void onMessage(XMLEle *root, std::list< int > &sharedBuffers)=0
void clearMsgQueue()
Definition MsgQueue.cpp:380
virtual bool acceptSharedBuffers() const
Definition MsgQueue.hpp:112
void traceMsg(const std::string &log, XMLEle *root)
Definition MsgQueue.cpp:186
virtual void closeWritePart()
Definition MsgQueue.cpp:249
void pushMsg(Msg *msg)
Definition MsgQueue.cpp:336
virtual void close()=0
unsigned long msgQSize() const
Definition MsgQueue.cpp:396
virtual void log(const std::string &log) const
Definition MsgQueue.cpp:163
bool useSharedBuffer
Definition MsgQueue.hpp:66
int getWFd() const
Definition MsgQueue.hpp:71
SerializedMsg * headMsg() const
Definition MsgQueue.cpp:320
void setFds(int rFd, int wFd)
Definition MsgQueue.cpp:284
int getRFd() const
Definition MsgQueue.hpp:67
Definition Msg.hpp:33
Interface to the reference INDI C API device implementation on the Device Driver side.
BLOBHandling
How drivers handle BLOBs incoming from snooping drivers.
Definition indidevapi.h:267
A little DOM-style library to handle parsing and processing an XML file.