CMSDK  2.0.1
RequestGateway.h
1 #if !defined(_REQUESTGATEWAY_H_)
2 #define _REQUESTGATEWAY_H_
3 
4 #pragma once
5 
6 #include "Stats.h"
7 #include "NetworkManager.h"
8 #include "RequestExecutor.h"
9 #include "RequestClient.h"
10 
11 
12 namespace cmlabs{
13 
15 // RequestGateway
17 
18 class RequestGateway : public Runnable, public NetworkReceiver {
19 public:
20  static bool UnitTest();
21 
22  friend THREAD_RET THREAD_FUNCTION_CALL RequestGatewayExecRun(THREAD_ARG arg);
23  friend THREAD_RET THREAD_FUNCTION_CALL RequestGatewayClientRun(THREAD_ARG arg);
24 
25  RequestGateway(uint32 id, const char* version = NULL);
26  virtual ~RequestGateway();
27 
28  bool init(const char* sslCertPath = NULL, const char* sslKeyPath = NULL);
29 
30  bool setWebServerInfo(const char* name, const char* rootdir, const char* indexfile);
31  bool setExternalAPILabel(const char* label);
32  bool setInternalAPILabel(const char* label);
33  bool setCacheFiles(bool cache);
34  bool setResponseType(const char* type);
35  bool setLongRequestLimit(uint32 limit);
36  bool setExecutorHeartbeatTimeout(uint32 timeout);
37  bool setQueuingParameters(uint32 maxRequestQueueSize, uint32 maxRequestProcessingSize, uint32 priorityThreshold);
38 
39  bool addPort(uint16 port, uint8 encryption, bool enableHTTP = false, uint32 timeout = 3000);
40  bool addGateway(uint32 id, std::string addr, uint16 port);
41  bool addAuthUser(const char* user, const char* password);
42 
43  bool receiveNetworkEvent(NetworkEvent* evt, NetworkChannel* channel, uint64 conid);
44  bool receiveMessage(DataMessage* msg, NetworkChannel* channel, uint64 conid);
45  bool receiveHTTPRequest(HTTPRequest* req, NetworkChannel* channel, uint64 conid);
46 
47 protected:
48  bool runExec();
49  bool runClient();
50 
51  bool callInternalAPI(const char* apiName, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
52  bool callExternalAPI(const char* apiName, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
53 
54  bool sendRestartToExecutor(uint64 id);
55  bool distributeDeadExecutorRequests(std::list<RequestReply*>& shortQueue, std::list<RequestReply*>& longQueue);
56 
57  uint64 getBestExecutorID(std::string requestString, uint32 reqSize, bool& isLongReq);
58  //std::list<RequestConnection> getTopExecutors(std::string requestString, uint32 reqSize, bool& isLongReq);
59  //bool sendToExecutor(RequestReply* reply);
60  bool replyToClient(DataMessage* msg);
61  bool addToRequestQueue(DataMessage* msg, uint64 origin, uint64 conID, uint64 clientRef);
62  bool addRequestReplyToRequestQueue(RequestReply* reply);
63 
64  NetworkManager* manager;
65  NetworkChannel* channel;
66  std::map<uint64,RequestConnection> executors;
67  std::map<uint64,RequestConnection> clients;
68  std::map<uint64,RequestConnection> webClients;
71  std::map<uint64,RequestReply*> requestMap;
72  std::vector<std::string> longReqNames;
73  std::map<std::string, uint64> httpAuth;
74 
75  utils::Mutex mutex;
76  //uint32 threadID;
77  uint64 systemStartTime;
78  uint32 threadIDClient;
79  uint64 lastRefID;
80  uint64 clientSentCount;
81  uint64 clientReceivedCount;
82  uint64 execSentCount;
83  uint64 execReceivedCount;
84 
85  uint32 id;
86  uint16 port;
87  uint32 longReqLimit;
88  uint32 executorHeartbeatTimeout;
89  uint32 maxRequestQueueSize;
90  uint32 maxRequestProcessingSize;
91  uint32 priorityThreshold;
92  bool sslSupport;
93  bool cacheFiles;
94  bool replyXML;
95 
96  std::string webServerName;
97  std::string rootdir;
98  std::string indexFilename;
99  std::string versionString;
100 
101  std::string internalAPITitle;
102  std::string externalAPITitle;
103 
104  MovingAverage shortAvgStats;
105  MovingAverage longAvgStats;
106  //std::map<uint64, MovingAverage> shortAvgStatsExec;
107  //std::map<uint64, MovingAverage> shortAvgStatsClients;
108  //std::map<uint64, MovingAverage> shortAvgStatsWeb;
109  //std::map<uint64, MovingAverage> longAvgStatsExec;
110  //std::map<uint64, MovingAverage> longAvgStatsClients;
111  //std::map<uint64, MovingAverage> longAvgStatsWeb;
112 };
113 
114 THREAD_RET THREAD_FUNCTION_CALL RequestGatewayExecRun(THREAD_ARG arg);
115 THREAD_RET THREAD_FUNCTION_CALL RequestGatewayClientRun(THREAD_ARG arg);
116 
117 }
118 
119 #endif // _REQUESTGATEWAY_H_
Definition: NetworkManager.h:47
Definition: Bitmap.h:7
Definition: NetworkManager.h:136
Definition: Utils.h:421
Definition: ThreadManager.h:33
Definition: MovingAverage.h:14
Definition: NetworkManager.h:25
Definition: NetworkProtocols.h:81
Definition: Utils.h:529
Definition: NetworkManager.h:12
Definition: Utils.h:276
Definition: RequestGateway.h:18
Definition: RequestClient.h:21
Definition: DataMessage.h:95