espp.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef __ESPP_HPP__
  15. #define __ESPP_HPP__
  16. #include "espthread.hpp"
  17. #include "espcfg.ipp"
  18. typedef ISocket * isockp;
  19. typedef MapBetween<int, int, isockp, isockp> SocketPortMap;
  20. MAKEPointerArray(ISocket, SocketPortArray);
  21. class CEspTerminator : public Thread
  22. {
  23. public:
  24. IMPLEMENT_IINTERFACE;
  25. virtual int run()
  26. {
  27. sleep(15);
  28. _exit(0);
  29. }
  30. };
  31. class CEspServer : public CInterface,
  32. implements ISocketSelectHandler,
  33. implements IEspServer,
  34. implements IEspContainer,
  35. implements IRestartHandler
  36. {
  37. private:
  38. SocketEndpoint m_address;
  39. Owned<ISocketSelectHandler> m_selectHndlr;
  40. SocketPortMap m_srvSockets;
  41. SocketPortArray m_socketCleanup;
  42. Semaphore m_waitForExit;
  43. bool m_exiting;
  44. bool m_useDali;
  45. LogLevel m_logLevel;
  46. bool m_logReq;
  47. bool m_logResp;
  48. unsigned m_slowProcessingTime;
  49. StringAttr m_frameTitle;
  50. Mutex abortMutex;
  51. bool m_SEHMappingEnabled;
  52. CEspConfig* m_config;
  53. public:
  54. IMPLEMENT_IINTERFACE;
  55. //CEspServer(SocketEndpoint address)
  56. CEspServer(CEspConfig* config) : m_config(config)
  57. {
  58. //m_address = address;
  59. m_address = config->getLocalEndpoint();
  60. m_selectHndlr.setown(createSocketSelectHandler());
  61. m_exiting = false;
  62. m_useDali = false;
  63. m_logLevel = config->m_options.logLevel;
  64. m_logReq = config->m_options.logReq;
  65. m_logResp = config->m_options.logResp;
  66. m_slowProcessingTime = config->m_options.slowProcessingTime;
  67. m_frameTitle.set(config->m_options.frameTitle);
  68. m_SEHMappingEnabled = false;
  69. }
  70. ~CEspServer()
  71. {
  72. ForEachItemIn(sindex, m_socketCleanup)
  73. {
  74. m_socketCleanup.item(sindex).Release();
  75. }
  76. m_socketCleanup.kill();
  77. }
  78. void waitForExit(CEspConfig &config)
  79. {
  80. if (config.usesDali())
  81. {
  82. m_useDali = true;
  83. while (!m_exiting)
  84. {
  85. bool daliOk;
  86. {
  87. synchronized sync(abortMutex);
  88. daliOk=config.checkDali();
  89. }
  90. if (daliOk)
  91. m_waitForExit.wait(1000);
  92. else
  93. {
  94. DBGLOG("Exiting ESP -- Lost DALI connection!");
  95. break;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. m_useDali = false;
  102. m_waitForExit.wait();
  103. sleep(1);
  104. }
  105. }
  106. //IRestartHandler
  107. void Restart()
  108. {
  109. exitESP();
  110. }
  111. //IEspContainer
  112. void exitESP()
  113. {
  114. if(m_SEHMappingEnabled)
  115. {
  116. DisableSEHtoExceptionMapping();
  117. m_SEHMappingEnabled = false;
  118. }
  119. // YMA: there'll be a leak here, but it's ok.
  120. CEspTerminator* terminator = new CEspTerminator;
  121. terminator->start();
  122. m_exiting=true;
  123. if(!m_useDali)
  124. m_waitForExit.signal();
  125. }
  126. void setLogLevel(LogLevel level) { m_logLevel = level; }
  127. LogLevel getLogLevel() { return m_logLevel; }
  128. bool getLogRequests() { return m_logReq; }
  129. bool getLogResponses() { return m_logResp; }
  130. void setFrameTitle(const char* title) { m_frameTitle.set(title); }
  131. const char* getFrameTitle() { return m_frameTitle.get(); }
  132. unsigned getSlowProcessingTime() { return m_slowProcessingTime; }
  133. void log(LogLevel level, const char* fmt, ...) __attribute__((format(printf, 3, 4)))
  134. {
  135. if (getLogLevel()>=level)
  136. {
  137. va_list args;
  138. va_start(args, fmt);
  139. VALOG(MCdebugInfo, unknownJob, fmt, args);
  140. va_end(args);
  141. }
  142. }
  143. //IEspServer
  144. void addProtocol(IEspProtocol &protocol)
  145. {
  146. }
  147. void addBinding(const char * name, const char * host, unsigned short port, IEspProtocol &protocol, IEspRpcBinding &binding, bool isdefault, IPropertyTree* cfgtree)
  148. {
  149. StringBuffer strIP;
  150. if (host != NULL)
  151. strIP.append(host);
  152. else
  153. m_address.getIpText(strIP);
  154. LOG(MCprogress, "binding %s, on %s:%d", name, strIP.str(), port);
  155. ISocket **socketp = m_srvSockets.getValue(port);
  156. ISocket *socket=(socketp!=NULL) ? *socketp : NULL;
  157. if (socket==NULL)
  158. {
  159. int backlogsize = 0;
  160. if(cfgtree)
  161. {
  162. const char* blstr = cfgtree->queryProp("@maxBacklogQueueSize");
  163. if(blstr && *blstr)
  164. backlogsize = atoi(blstr);
  165. }
  166. if(backlogsize > 0)
  167. {
  168. socket = ISocket::create_ip(port, strIP.str(), backlogsize);
  169. }
  170. else
  171. {
  172. socket = ISocket::create_ip(port, strIP.str());
  173. }
  174. m_socketCleanup.append(*socket);
  175. LOG(MCprogress, " created server socket(%d)", socket->OShandle());
  176. m_srvSockets.setValue(port, socket);
  177. add(socket, SELECTMODE_READ | SELECTMODE_WRITE, dynamic_cast<ISocketSelectNotify*>(&protocol));
  178. LOG(MCprogress, " Socket(%d) listening.", socket->OShandle());
  179. }
  180. if (socket)
  181. {
  182. protocol.addBindingMap(socket, &binding, isdefault);
  183. socket->Release();
  184. }
  185. else
  186. {
  187. IERRLOG("Can't create socket on %s:%d", strIP.str(), port);
  188. throw MakeStringException(-1, "Can't create socket on %s:%d", strIP.str(), port);
  189. }
  190. }
  191. //ISocketHandler
  192. void start()
  193. {
  194. m_selectHndlr->start();
  195. }
  196. void add(ISocket *sock,unsigned mode,ISocketSelectNotify *nfy)
  197. {
  198. m_selectHndlr->add(sock, mode, nfy);
  199. }
  200. void remove(ISocket *sock)
  201. {
  202. m_selectHndlr->remove(sock);
  203. }
  204. void stop(bool wait)
  205. {
  206. if(m_selectHndlr)
  207. {
  208. m_selectHndlr->stop(wait);
  209. DBGLOG("select handler stopped.");
  210. }
  211. }
  212. void setSavedSEHHandler(bool mappingEnabled)
  213. {
  214. m_SEHMappingEnabled = mappingEnabled;
  215. }
  216. virtual void sendSnmpMessage(const char* msg) { throwUnexpected(); }
  217. };
  218. class CEspAbortHandler : public CInterface,
  219. implements IAbortHandler
  220. {
  221. CEspConfig* m_config;
  222. CEspServer* m_srv;
  223. public:
  224. IMPLEMENT_IINTERFACE;
  225. CEspAbortHandler()
  226. {
  227. m_config=NULL;
  228. m_srv=NULL;
  229. addAbortHandler(*this);
  230. }
  231. ~CEspAbortHandler()
  232. {
  233. removeAbortHandler(*this);
  234. }
  235. void setConfig(CEspConfig* config)
  236. {
  237. m_config = config;
  238. }
  239. void setServer(CEspServer* srv)
  240. {
  241. m_srv = srv;
  242. }
  243. //IAbortHandler
  244. bool onAbort()
  245. {
  246. LOG(MCprogress, "ESP Abort Handler...");
  247. m_srv->exitESP();
  248. return false;
  249. }
  250. };
  251. #define MAX_CHILDREN 1
  252. #endif //__ESPP_HPP__