CMSDK  2.0.1
PsySpace.h
1 #if !defined(_PSYSPACE_H_)
2 #define _PSYSPACE_H_
3 
4 #include "MemoryManager.h"
5 #include "PsyTime.h"
6 #include "xml_parser.h"
7 #include "DataMessage.h"
8 #include "Subscriptions.h"
9 
10 namespace cmlabs {
11 
12 class PsyAPI;
13 typedef int8 (* CrankFunction)(PsyAPI* api);
14 
15 #define LOG_SPACE 7
16 
18 public:
19  TimeQueueSchedule(uint32 interval, uint64 start = 0, uint64 end = 0);
21 
22  uint32 id;
23  uint64 nextTrigger;
24 
25  uint32 interval;
26  uint64 start;
27  uint64 end;
28 
29  PsyType msgType;
30  uint32 msgTag;
31 };
32 
33 // TimeQueue
34 // with time bins for module activation
35 class TimeQueue {
36 public:
37  TimeQueue();
38  ~TimeQueue();
39 
40  bool addSchedule(TimeQueueSchedule* schedule);
41  bool removeSchedule(uint32 id);
42 
43  DataMessage* waitForNextEvent(uint32 ms);
44 
45 private:
46  utils::Timer* timer;
47  std::map<uint32, TimeQueueSchedule*> schedules;
48 
49  utils::Mutex mutex;
50  utils::Semaphore semaphore;
51 };
52 
53 struct SignalStruct {
54  DataMessage* lastSignalMsg;
55  utils::Event event;
56  utils::Mutex mutex;
57 };
58 
59 #define PROCMESSAGE 0
60 #define PROCSIGNAL 1
61 #define PROCREQUEST 2
62 
63 class PsySpace : public Runnable, public LogReceiver {
64  friend class PsyAPI;
65 public:
66  friend THREAD_RET THREAD_FUNCTION_CALL PsySpaceRun(THREAD_ARG arg);
67  friend THREAD_RET THREAD_FUNCTION_CALL PsySpacePoolRun(THREAD_ARG arg);
68  friend THREAD_RET THREAD_FUNCTION_CALL PsySpaceContinuousRun(THREAD_ARG arg);
69 
70  // ***************** Constructors *****************
71  // Create and destroy resources
72 
73  PsySpace(const char* name = NULL, bool isAdHoc = true, uint16 procID = 0, bool isLocal = false);
74  ~PsySpace();
75 
76  bool connect(uint16 systemID, bool isMaster = false, const char* cmdline = NULL);
77  bool isConnected(uint32 timeoutMS = 5000);
78  bool reset();
79  bool start(uint16 threadCount = 5);
80  bool shutdown();
81  bool hasShutdown();
82  uint16 getID();
83 
84  bool postMessage(DataMessage* msg);
85 
86  // ***************** Signals *****************
87  bool emitSignal(const PsyType& type, DataMessage* msg);
88  DataMessage* waitForSignal(const PsyType& type, uint32 timeout, uint64 lastReceivedTime = 0);
89 
90  uint8 query(DataMessage* msg, DataMessage** result, uint32 timeout);
91  bool queryReply(uint32 id, uint8 status, DataMessage* result);
92 
93  uint32 getComponentID(const char* name);
94 // PsyAPI* getComponentAPI(const char* name);
95 // PsyAPI* getComponentAPI(uint32 compID);
96 // bool registerComponentCallback(const char* name, CrankFunction func);
97 // bool registerComponentCallback(uint32 compID, CrankFunction func);
98 
99  PsyAPI* getCrankAPI(const char* name);
100  bool registerCrankCallback(const char* name, CrankFunction func);
101 
102  bool addPsyProbeCustomView(uint32 compID, const char* name, const char* templateURL);
103 
104  bool logEntry(LogEntry* entry);
105 
106  MemoryManager* manager;
107 private:
108 
109  std::map<std::string, CrankFunction> internalCranks;
110 
111  bool threadPoolDispatch();
112  bool runContinuousComponent();
113  bool startContinuousComponent(uint32 compID);
114  bool setThreadPoolSize(uint16 threadCount);
115  CrankFunction loadCrankFromLibrary(const char* crankName, const char* libraryFilename);
116 
117  bool pullRemoteComponentData(uint32 compID, uint16 fromNodeID);
118 
119  bool run();
120 
121  std::string name;
122  bool isLocal;
123  bool isMaster;
124  bool isAdHoc;
125  bool finishedShuttingDown;
126  uint64 currentInstID;
127 
128  uint16 procID;
129  uint64 masterCreatedTime;
130 
131  uint32 waitCounters[3];
132  uint32 procCounters[3];
133 
134  uint32 threadPoolWaitLowerThreshold;
135  uint32 threadPoolWaitUpperThreshold;
136  uint32 threadPoolWaitIncrement;
137  uint64 lastThreadPoolCheck;
138  uint64 threadPoolCheckInterval;
139  utils::Mutex threadPoolMutex;
140 
141  TimeQueue* timeQ;
142 
143  uint16 threadTarget;
144 
145  std::map<uint16, CrankFunction> cranks;
146  std::map<uint32, uint16> compCrankIDs;
147  std::map<std::string, utils::Library*> libraries;
148  std::map<uint32, PsyAPI*> psyAPIs;
149  std::map<PsyType, SignalStruct*> signalList;
150  utils::Mutex signalsMutex;
151 
152  std::map<uint32, uint8> threadPool;
153  std::map<uint32, uint32> continuousComponentThreads;
154 };
155 
156 THREAD_RET THREAD_FUNCTION_CALL PsySpaceRun(THREAD_ARG arg);
157 THREAD_RET THREAD_FUNCTION_CALL PsySpacePoolRun(THREAD_ARG arg);
158 THREAD_RET THREAD_FUNCTION_CALL PsySpaceContinuousRun(THREAD_ARG arg);
159 
160 } // namespace cmlabs
161 
162 #include "PsyAPI.h"
163 #include "PsyInternal.h"
164 
165 #endif //_PSYSPACE_H_
166 
Definition: Utils.h:175
Definition: Bitmap.h:7
Definition: PsySpace.h:63
Definition: ThreadManager.h:33
Definition: PsySpace.h:53
Definition: MemoryManager.h:214
Definition: PsySpace.h:17
Definition: Utils.h:317
Definition: Utils.h:387
Definition: Types.h:80
Definition: Utils.h:299
Definition: Utils.h:276
Definition: Utils.h:189
Definition: PsySpace.h:35
Definition: PsyAPI.h:35
Definition: DataMessage.h:95