NAP
apiservice.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "apievent.h"
9 
10 // External Includes
11 #include <nap/service.h>
12 #include <queue>
13 #include <mutex>
14 #include <nap/signalslot.h>
15 
16 namespace nap
17 {
18  // Forward Declares
19  class APIComponentInstance;
20 
47  class NAPAPI APIService : public Service
48  {
49  friend class APIComponentInstance;
50  RTTI_ENABLE(Service)
51  public:
55  APIService(ServiceConfiguration* configuration);
56 
60  ~APIService() override;
61 
70  bool sendFloat(const char* id, float value, utility::ErrorState* error);
71 
80  bool sendString(const char* id, const char* value, utility::ErrorState* error);
81 
90  bool sendInt(const char* id, int value, utility::ErrorState* error);
91 
100  bool sendByte(const char* id, nap::uint8 value, utility::ErrorState* error);
101 
110  bool sendBool(const char* id, bool value, utility::ErrorState* error);
111 
120  bool sendLong(const char* id, int64_t value, utility::ErrorState* error);
121 
130  bool sendChar(const char* id, char value, utility::ErrorState* error);
131 
139  bool send(const char* id, utility::ErrorState* error);
140 
148  bool sendEvent(APIEventPtr apiEvent, utility::ErrorState* error);
149 
201  bool sendMessage(const char* json, utility::ErrorState* error);
202 
212  bool sendIntArray(const char* id, int* array, int length, utility::ErrorState* error);
213 
223  bool sendFloatArray(const char* id, float* array, int length, utility::ErrorState* error);
224 
234  bool sendByteArray(const char* id, uint8_t* array, int length, utility::ErrorState* error);
235 
245  bool sendStringArray(const char* id, const char** array, int length, utility::ErrorState* error);
246 
253  void processEvents();
254 
264  void dispatchEvent(nap::APIEventPtr apiEvent);
265 
271 
272  protected:
278  virtual bool init(utility::ErrorState& error) override;
279 
283  virtual void shutdown() override;
284 
288  virtual void update(double deltaTime) override;
289 
290  private:
295  void registerAPIComponent(APIComponentInstance& apicomponent);
296 
301  void removeAPIComponent(APIComponentInstance& apicomponent);
302 
308  bool forward(APIEventPtr apiEvent, utility::ErrorState& error);
309 
314  void consumeEvents(std::queue<APIEventPtr>& outEvents);
315 
316  // All the api components currently available to the system
317  std::vector<APIComponentInstance*> mAPIComponents;
318 
319  // All the api events to process
320  std::queue<APIEventPtr> mAPIEvents;
321 
322  // Mutex associated with setting / getting api events
323  std::mutex mEventMutex;
324 
325  // Mutex associated with component registration and iteration
326  std::mutex mComponentMutex;
327  };
328 }
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ServiceConfiguration
Definition: service.h:28
nap::APIService
Definition: apiservice.h:47
nap::Signal
Definition: signalslot.h:28
nap::APIService::eventDispatched
nap::Signal< const APIEvent & > eventDispatched
Definition: apiservice.h:270
nap::APIEventPtr
std::unique_ptr< nap::APIEvent > APIEventPtr
Definition: apievent.h:179
nap::Service
Definition: templateservice.h:8
nap
Definition: templateapp.h:17
nap::APIComponentInstance
Definition: apicomponent.h:47