• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ANDROID_PDX_SERVICE_DISPATCHER_H_
2 #define ANDROID_PDX_SERVICE_DISPATCHER_H_
3 
4 #include <memory>
5 
6 namespace android {
7 namespace pdx {
8 
9 class Service;
10 
11 /*
12  * ServiceDispatcher manages a list of Service instances and handles message
13  * reception and dispatch to the services. This makes repetitive dispatch tasks
14  * easier to implement.
15  */
16 class ServiceDispatcher {
17  public:
18   virtual ~ServiceDispatcher() = default;
19 
20   /*
21    * Adds a service to the list of services handled by this dispatcher. This
22    * will fail if any threads are blocked waiting for messages in this
23    * dispatcher.
24    *
25    * Returns 0 on success; -EEXIST if the service was already added.
26    */
27   virtual int AddService(const std::shared_ptr<Service>& service) = 0;
28 
29   /*
30    * Removes a service from this dispatcher. This will fail if any threads are
31    * blocked waiting for messages in this dispatcher.
32    *
33    * Returns 0 on success; -ENOENT if the service was not previously added;
34    * -EBUSY if there are threads in the dispatcher.
35    */
36   virtual int RemoveService(const std::shared_ptr<Service>& service) = 0;
37 
38   /*
39    * Receive and dispatch one set of messages. Multiple threads may enter this
40    * method to create an implicit thread pool, as described for
41    * enterDispatchLoop() below, however this method exits after one dispatch
42    * cycle, requiring an external loop. This is useful when other work needs
43    * to be done in the service dispatch loop.
44    */
45   virtual int ReceiveAndDispatch() = 0;
46 
47   /*
48    * Same as above with timeout in milliseconds. A negative value means
49    * infinite timeout, while a value of 0 means return immediately if no
50    * messages are available to receive.
51    */
52   virtual int ReceiveAndDispatch(int timeout) = 0;
53 
54   /*
55    * Receive and dispatch messages until canceled. When more than one thread
56    * enters this method it creates an implicit thread pool to dispatch messages.
57    * Explicit thread pools may be created by using a single dispatch thread that
58    * hands Message instances (via move assignment) over to a queue of threads
59    * (or perhaps one of several) to handle.
60    */
61   virtual int EnterDispatchLoop() = 0;
62 
63   /*
64    * Sets the canceled state of the dispatcher. When canceled is true, any
65    * threads blocked waiting for messages will return. This method waits until
66    * all dispatch threads have exited the dispatcher.
67    */
68   virtual void SetCanceled(bool cancel) = 0;
69 
70   /*
71    * Gets the canceled state of the dispatcher.
72    */
73   virtual bool IsCanceled() const = 0;
74 };
75 
76 }  // namespace pdx
77 }  // namespace android
78 
79 #endif  // ANDROID_PDX_SERVICE_DISPATCHER_H_
80