Instrument Neutral Distributed Interface INDI 2.2.5
SerializedMsg.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
22
23#include <mutex>
24#include <vector>
25#include <list>
26#include <ev++.h>
27
28class Msg;
29class MsgChunck;
31class MsgQueue;
32
34
36{
37 friend class Msg;
38 friend class MsgChunckIterator;
39
40 std::recursive_mutex lock;
41 ev::async asyncProgress;
42
43 // Start a thread for execution of asyncRun
44 void async_start();
45 void async_cancel();
46
47 // Called within main loop when async task did some progress
48 void async_progressed();
49
50 // The requirements. Prior to starting, everything is required.
51 SerializationRequirement requirements;
52
53 void produce(bool sync);
54
55 protected:
56 // These methods are to be called from asyncRun
57 bool async_canceled();
59 void async_pushChunck(const MsgChunck &m);
60 void async_done();
61
62 // True if a producing thread is active
63 bool isAsyncRunning();
64
67
69
70 std::set<MsgQueue *> awaiters;
71 private:
72 std::vector<MsgChunck> chuncks;
73
74 protected:
75 // Buffers malloced during asyncRun
76 std::list<void*> ownBuffers;
77
78 // This will notify awaiters and possibly release the owner
80
81 virtual bool generateContentAsync() const = 0;
82 virtual void generateContent() = 0;
83
85
86 // The task will cancel itself if all owner release it
87 void abort();
88
89 // Make sure the given receiver will not be processed until this task complete
90 // TODO : to implement + make sure the task start when it actually block something
91 void blockReceiver(MsgQueue * toblock);
92
93 public:
94 SerializedMsg(Msg * parent);
95 virtual ~SerializedMsg();
96
97 // Calling requestContent will start production
98 // Return true if some content is available
99 bool requestContent(const MsgChunckIterator &position);
100
101 // Return true if some content is available
102 // It is possible to have 0 to send, meaning end was actually reached
103 bool getContent(MsgChunckIterator &position, void * &data, ssize_t &nsend, std::vector<int> &sharedBuffers);
104
105 void advance(MsgChunckIterator &position, ssize_t s);
106
107 // When a queue is done with sending this message
108 void release(MsgQueue * from);
109
110 void addAwaiter(MsgQueue * awaiter);
111
112 ssize_t queueSize();
113};
114
SerializationStatus
Definition Msg.hpp:33
void async_updateRequirement(const SerializationRequirement &n)
SerializationStatus asyncStatus
bool getContent(MsgChunckIterator &position, void *&data, ssize_t &nsend, std::vector< int > &sharedBuffers)
virtual bool generateContentAsync() const =0
MsgQueue * blockedProducer
void collectRequirements(SerializationRequirement &req)
std::list< void * > ownBuffers
void blockReceiver(MsgQueue *toblock)
virtual ~SerializedMsg()
ssize_t queueSize()
bool requestContent(const MsgChunckIterator &position)
void onDataReady()
std::set< MsgQueue * > awaiters
virtual void generateContent()=0
void advance(MsgChunckIterator &position, ssize_t s)
void addAwaiter(MsgQueue *awaiter)
void release(MsgQueue *from)
void async_pushChunck(const MsgChunck &m)