some more work on plugin manager
[enigma2.git] / lib / base / message.h
1 #ifndef __lib_base_message_h
2 #define __lib_base_message_h
3
4 #include <lib/base/ebase.h>
5 #include <lib/python/connections.h>
6 #include <lib/python/swig.h>
7 #include <unistd.h>
8 #include <lib/base/elock.h>
9
10
11 /**
12  * \brief A generic messagepump.
13  *
14  * You can send and receive messages with this class. Internally a fifo is used,
15  * so you can use them together with a \c eMainloop.
16  */
17 #ifndef SWIG
18 class eMessagePump
19 {
20         int fd[2];
21         eLock content;
22         int ismt;
23 public:
24         eMessagePump(int mt=0);
25         virtual ~eMessagePump();
26 protected:
27         int send(const void *data, int len);
28         int recv(void *data, int len); // blockierend
29         int getInputFD() const;
30         int getOutputFD() const;
31 };
32
33 /**
34  * \brief A messagepump with fixed-length packets.
35  *
36  * Based on \ref eMessagePump, with this class you can send and receive fixed size messages.
37  * Automatically creates a eSocketNotifier and gives you a callback.
38  */
39 template<class T>
40 class eFixedMessagePump: private eMessagePump, public Object
41 {
42         ePtr<eSocketNotifier> sn;
43         void do_recv(int)
44         {
45                 T msg;
46                 recv(&msg, sizeof(msg));
47                 /*emit*/ recv_msg(msg);
48         }
49 public:
50         Signal1<void,const T&> recv_msg;
51         void send(const T &msg)
52         {
53                 eMessagePump::send(&msg, sizeof(msg));
54         }
55         eFixedMessagePump(eMainloop *context, int mt): eMessagePump(mt)
56         {
57                 sn=eSocketNotifier::create(context, getOutputFD(), eSocketNotifier::Read);
58                 CONNECT(sn->activated, eFixedMessagePump<T>::do_recv);
59                 sn->start();
60         }
61         void start() { if (sn) sn->start(); }
62         void stop() { if (sn) sn->stop(); }
63 };
64 #endif
65
66 class ePythonMessagePump: public eMessagePump, public Object
67 {
68         ePtr<eSocketNotifier> sn;
69         void do_recv(int)
70         {
71                 int msg;
72                 recv(&msg, sizeof(msg));
73                 /*emit*/ recv_msg(msg);
74         }
75 public:
76         PSignal1<void,int> recv_msg;
77         void send(int msg)
78         {
79                 eMessagePump::send(&msg, sizeof(msg));
80         }
81         ePythonMessagePump()
82                 :eMessagePump(1)
83         {
84                 sn=eSocketNotifier::create(eApp, getOutputFD(), eSocketNotifier::Read);
85                 CONNECT(sn->activated, ePythonMessagePump::do_recv);
86                 sn->start();
87         }
88         void start() { if (sn) sn->start(); }
89         void stop() { if (sn) sn->stop(); }
90 };
91
92 #endif