connect.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "connect.hpp"
  14. #include "jlib.hpp"
  15. #include "jfile.hpp"
  16. #include "dasds.hpp"
  17. #include "mpbase.hpp"
  18. #include "mpcomm.hpp"
  19. #include "daclient.hpp"
  20. #include "util.hpp"
  21. #define CONNECTION_TIMEOUT 10000
  22. class Connection : public CInterface
  23. {
  24. protected:
  25. Connection_t type;
  26. IPropertyTree * pTree;
  27. LPSTR fname;
  28. public:
  29. Connection()
  30. {
  31. type = CT_none;
  32. pTree = NULL;
  33. fname = NULL;
  34. }
  35. ~Connection()
  36. {
  37. if(fname) delete(fname);
  38. }
  39. };
  40. class LocalConnection : public Connection, implements IConnection
  41. {
  42. private:
  43. IPropertyTree * loadTree(LPCSTR fname)
  44. {
  45. IPropertyTree * r = NULL;
  46. try
  47. {
  48. r = createPTreeFromXMLFile(fname);
  49. }
  50. catch(IException * e)
  51. {
  52. reportException(e);
  53. }
  54. catch(...)
  55. {
  56. showFIOErr(fname, true);
  57. }
  58. return r;
  59. }
  60. public:
  61. IMPLEMENT_IINTERFACE;
  62. LocalConnection(LPCSTR filename) : Connection()
  63. {
  64. fname = strdup(filename);
  65. try
  66. {
  67. pTree = loadTree(fname);
  68. type = CT_local;
  69. }
  70. catch(IException * e)
  71. {
  72. reportException(e);
  73. e->Release();
  74. pTree = NULL;
  75. type = CT_none;
  76. }
  77. }
  78. ~LocalConnection()
  79. {
  80. ::Release(pTree);
  81. }
  82. // IConnection
  83. virtual bool lockWrite() { return true; }
  84. virtual bool unlockWrite() { return true; }
  85. virtual Connection_t getType() { return type; }
  86. virtual void commit() { /* NULL */ }
  87. virtual IPropertyTree * queryRoot(const char * xpath)
  88. {
  89. if(xpath)
  90. {
  91. IPropertyTree * oldpTree = pTree;
  92. try
  93. {
  94. pTree = pTree->getPropTree(xpath); // this links
  95. if(oldpTree) oldpTree->Release();
  96. }
  97. catch(IException * e)
  98. {
  99. reportException(e);
  100. e->Release();
  101. pTree = oldpTree;
  102. }
  103. }
  104. return pTree;
  105. }
  106. virtual LPCSTR queryName() { return type == CT_none ? NULL : fname; }
  107. };
  108. class RemoteConnection : public Connection, implements IConnection
  109. {
  110. private:
  111. IRemoteConnection * conn;
  112. StringBuffer pathToRoot;
  113. void killConnection()
  114. {
  115. if(pTree)
  116. {
  117. pTree->Release();
  118. pTree = NULL;
  119. }
  120. if(conn)
  121. {
  122. conn->commit();
  123. conn->Release();
  124. conn = NULL;
  125. }
  126. }
  127. public:
  128. IMPLEMENT_IINTERFACE;
  129. RemoteConnection(SocketEndpointArray & epa) : Connection()
  130. {
  131. pathToRoot.append("/");
  132. fname = strdup("Remote Server");
  133. conn = NULL;
  134. try
  135. {
  136. IGroup * group = createIGroup(epa);
  137. // CSystemCapability capability(DCR_TreeView, "DALITREE");
  138. // capability.secure((byte *)CLIENT_ENCRYPT_KEY, strlen(CLIENT_ENCRYPT_KEY));
  139. if(initClientProcess(group, DCR_TreeView, 0, NULL, NULL, CONNECTION_TIMEOUT))//, &capability))
  140. type = CT_remote;
  141. group->Release();
  142. }
  143. catch(IException * e)
  144. {
  145. reportException(e);
  146. return;
  147. }
  148. catch(...)
  149. {
  150. MessageBox(NULL, "An error occured whilst attempting connection.", "Connection Error", MB_OK | MB_ICONEXCLAMATION);
  151. return;
  152. }
  153. }
  154. ~RemoteConnection()
  155. {
  156. killConnection();
  157. closedownClientProcess();
  158. }
  159. // IConnection
  160. virtual bool lockWrite()
  161. {
  162. if (!conn) return false;
  163. try
  164. {
  165. conn->changeMode(RTM_LOCK_WRITE, CONNECTION_TIMEOUT);
  166. }
  167. catch (ISDSException *e)
  168. {
  169. if (SDSExcpt_LockTimeout == e->errorCode())
  170. {
  171. e->Release();
  172. return false;
  173. }
  174. else
  175. throw e;
  176. }
  177. return true;
  178. }
  179. virtual bool unlockWrite()
  180. {
  181. if (!conn) return false;
  182. try
  183. {
  184. conn->changeMode(0, CONNECTION_TIMEOUT);
  185. }
  186. catch (ISDSException *e)
  187. {
  188. if (SDSExcpt_LockTimeout == e->errorCode())
  189. {
  190. e->Release();
  191. return false;
  192. }
  193. else
  194. throw e;
  195. }
  196. return true;
  197. }
  198. virtual Connection_t getType() { return type; }
  199. virtual void commit() { if(conn) conn->commit(); }
  200. virtual IPropertyTree * queryRoot(const char * xpath)
  201. {
  202. killConnection();
  203. try
  204. {
  205. ISDSManager & sdsManager = querySDS();
  206. if(xpath) pathToRoot.append(xpath).append("/");
  207. conn = sdsManager.connect(pathToRoot.str(), myProcessSession(), 0, 5000);
  208. }
  209. catch(IException * e)
  210. {
  211. conn = NULL;
  212. type = CT_none;
  213. reportException(e);
  214. return NULL;
  215. }
  216. pTree = conn->getRoot();
  217. type = CT_remote;
  218. return pTree;
  219. }
  220. virtual LPCSTR queryName() { return type == CT_none ? NULL : fname; }
  221. };
  222. IConnection * createLocalConnection(LPCSTR filename)
  223. {
  224. return new LocalConnection(filename);
  225. }
  226. IConnection * createRemoteConnection(SocketEndpointArray & epa)
  227. {
  228. return new RemoteConnection(epa);
  229. }