WvStreams
debian/libwvstreams-dev/usr/include/wvstreams/wvdbusconn.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 2004-2006 Net Integration Technologies, Inc.
4 *
5 * Pathfinder Software:
6 * Copyright (C) 2007, Carillon Information Security Inc.
7 *
8 * This library is licensed under the LGPL, please read LICENSE for details.
9 *
10 * A WvDBusConn represents a connection to another application. Messages
11 * can be sent and received via this connection. In most cases, the
12 * other application is a message bus.
13 */
14#ifndef __WVDBUSCONN_H
15#define __WVDBUSCONN_H
16
17#include "wvstreamclone.h"
18#include "wvlog.h"
19#include "wvdbusmsg.h"
20#include "wvhashtable.h"
21#include "wvuid.h"
22
23#define WVDBUS_DEFAULT_TIMEOUT (300*1000)
24
25class WvDBusConn;
26
32typedef wv::function<bool(WvDBusMsg&)> WvDBusCallback;
33
35{
36public:
37 virtual ~IWvDBusAuth() { };
38
47 virtual bool authorize(WvDBusConn &c) = 0;
48
49 // Returns the unix UID negotiated during authentication. Boring on the
50 // client side (generally just getuid()), more useful for the server.
51 virtual wvuid_t get_uid() = 0;
52};
53
54
56{
57 bool sent_request;
58public:
60 virtual bool authorize(WvDBusConn &c);
61 virtual wvuid_t get_uid();
62};
63
64
66{
67 bool client, authorized, in_post_select;
68 WvString _uniquename;
69 IWvDBusAuth *auth;
70
71public:
72 WvLog log;
73
85 WvDBusConn(WvStringParm moniker, IWvDBusAuth *_auth = NULL,
86 bool _client = true);
87
92 WvDBusConn(IWvStream *_cloned, IWvDBusAuth *_auth = NULL,
93 bool _client = true);
94
95 void init(IWvDBusAuth *_auth, bool _client);
96
101 virtual ~WvDBusConn();
102
103 void set_uniquename(WvStringParm s);
104 void try_auth();
105 void send_hello();
106 wvuid_t get_uid() { return auth ? auth->get_uid() : WVUID_INVALID; }
107
108 void out(WvStringParm s);
109 void out(WVSTRING_FORMAT_DECL)
110 { return out(WvString(WVSTRING_FORMAT_CALL)); }
111 const char *in();
112
119 void request_name(WvStringParm name, const WvDBusCallback &onreply = 0,
120 time_t msec_timeout = WVDBUS_DEFAULT_TIMEOUT);
121
126 WvString uniquename() const;
127
132 virtual void close();
133
138 uint32_t send(WvDBusMsg msg);
139
144 void send(WvDBusMsg msg, const WvDBusCallback &onreply,
145 time_t msec_timeout = WVDBUS_DEFAULT_TIMEOUT);
146
163 time_t msec_timeout = WVDBUS_DEFAULT_TIMEOUT,
164 wv::function<void(uint32_t)> serial_cb = 0);
165
171 PriSystem = 0, // implemented by DBus or WvDBus. Don't use.
172 PriSpecific = 5000, // strictly limited by interface/method
173 PriNormal = 6000, // a reasonably selective callback
174 PriBridge = 7000, // proxy selectively to other listeners
175 PriBroadcast = 8000, // last-resort proxy to all listeners
176 PriGaveUp = 9900, // run this if nothing else works
177 };
178
198 void add_callback(CallbackPri pri, WvDBusCallback cb, void *cookie = NULL);
199
203 void del_callback(void *cookie);
204
210 virtual bool filter_func(WvDBusMsg &msg);
211
217 bool isidle();
218
219private:
220 time_t mintimeout_msec();
221 virtual bool post_select(SelectInfo &si);
222
223 struct Pending
224 {
225 WvDBusMsg msg; // needed in case we need to generate timeout replies
226 uint32_t serial;
227 WvDBusCallback cb;
228 WvTime valid_until;
229
230 Pending(WvDBusMsg &_msg, const WvDBusCallback &_cb,
231 time_t msec_timeout)
232 : msg(_msg), cb(_cb)
233 {
234 serial = msg.get_serial();
235 if (msec_timeout < 0)
236 msec_timeout = 5*3600*1000; // "forever" is a few hours
237 valid_until = msecadd(wvstime(), msec_timeout);
238 }
239 };
240 DeclareWvDict(Pending, uint32_t, serial);
241
242 PendingDict pending;
243 WvDynBuf in_queue, out_queue;
244
245 void expire_pending(Pending *p);
246 void cancel_pending(uint32_t serial);
247 void add_pending(WvDBusMsg &msg, WvDBusCallback cb,
248 time_t msec_timeout);
249 bool _registered(WvDBusMsg &msg);
250
251 struct CallbackInfo
252 {
253 CallbackPri pri;
254 WvDBusCallback cb;
255 void *cookie;
256
257 CallbackInfo(CallbackPri _pri,
258 const WvDBusCallback &_cb, void *_cookie)
259 : cb(_cb)
260 { pri = _pri; cookie = _cookie; }
261 };
262 static int priority_order(const CallbackInfo *a, const CallbackInfo *b);
263
264 DeclareWvList(CallbackInfo);
265 CallbackInfoList callbacks;
266
267};
268
269#endif // __WVDBUSCONN_H
virtual bool authorize(WvDBusConn &c)=0
Main action callback.
virtual bool authorize(WvDBusConn &c)
Main action callback.
virtual void close()
Close the underlying stream.
virtual bool filter_func(WvDBusMsg &msg)
Called by for each received message.
virtual ~WvDBusConn()
Release this connection.
CallbackPri
The priority level of a callback registration.
WvDBusMsg send_and_wait(WvDBusMsg msg, time_t msec_timeout=WVDBUS_DEFAULT_TIMEOUT, wv::function< void(uint32_t)> serial_cb=0)
Send a message on the bus and wait for a reply to come in, returning the message when it does.
void request_name(WvStringParm name, const WvDBusCallback &onreply=0, time_t msec_timeout=WVDBUS_DEFAULT_TIMEOUT)
Request the given service name on DBus.
bool isidle()
Returns true if there are no outstanding messages that have not received (or timed out) their reply.
uint32_t send(WvDBusMsg msg)
Send a message on the bus, not expecting any reply.
void add_callback(CallbackPri pri, WvDBusCallback cb, void *cookie=NULL)
Adds a callback to the connection: all received messages will be sent to all callbacks to look at and...
void del_callback(void *cookie)
Delete all callbacks that have the given cookie.
WvString uniquename() const
Return this connection's unique name on the bus, assigned by the server at connect time.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
WvStreamClone simply forwards all requests to the "cloned" stream.
WvString is an implementation of a simple and efficient printable-string class.
Based on (and interchangeable with) struct timeval.