hrpcutil.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "platform.h"
  14. #include "jlib.hpp"
  15. #include "hrpcsock.hpp"
  16. #include "hrpcutil.hpp"
  17. #include "jsocket.hpp"
  18. #include "jmutex.hpp"
  19. #include "jthread.hpp"
  20. void SplitIpPort(StringAttr & ip, unsigned & port, const char * address)
  21. {
  22. const char * colon = strchr(address, ':');
  23. if (colon)
  24. {
  25. ip.set(address,colon-address);
  26. if (strcmp(ip, ".")==0)
  27. ip.set(GetCachedHostName());
  28. port = atoi(colon+1);
  29. }
  30. else
  31. ip.set(address);
  32. }
  33. IHRPCtransport *MakeTcpTransportFromUrl(const char *target, unsigned defaultPort)
  34. {
  35. StringAttr ip;
  36. SplitIpPort(ip, defaultPort, target);
  37. return MakeTcpTransport(ip, defaultPort);
  38. }
  39. void TcpWhoAmI(StringAttr & out)
  40. {
  41. out.set(GetCachedHostName());
  42. }
  43. void ListenUntilDead(HRPCserver & server, const char * errorMessage)
  44. {
  45. ListenUntilDead(server, NULL, errorMessage);
  46. }
  47. void ListenUntilDead(HRPCserver & server, IHRPCtransport * transport, const char * errorMessage)
  48. {
  49. bool alive = true;
  50. while (alive)
  51. {
  52. try
  53. {
  54. server.Listen(transport);
  55. alive=false;
  56. }
  57. catch(IHRPC_Exception *e)
  58. {
  59. switch (e->errorCode())
  60. {
  61. case HRPCERR_lost_connection:
  62. case HRPCERR_transport_not_open:
  63. PrintExceptionLog(e,"Listening connection lost - listen again");
  64. //MORE: The owner went down...
  65. e->Release();
  66. break;
  67. default:
  68. if (errorMessage)
  69. PrintExceptionLog(e,errorMessage);
  70. e->Release();
  71. alive = false;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. IHRPCtransport * TryMakeServerTransport(unsigned port, const char * errorMessage)
  78. {
  79. IHRPCtransport *transport = NULL;
  80. try
  81. {
  82. transport = MakeTcpTransport(NULL,port);
  83. }
  84. catch(IHRPC_Exception *e)
  85. {
  86. switch (e->errorCode())
  87. {
  88. case HRPCERR_transport_port_in_use:
  89. default:
  90. if (errorMessage)
  91. pexception(errorMessage, e);
  92. e->Release();
  93. break;
  94. }
  95. }
  96. return transport;
  97. }
  98. #define MAXCONNECTIONS 16
  99. static CriticalSection mcsect;
  100. bool FastMultipleConnect(unsigned n,HRPCmodule **modules,bool *done,int timeout)
  101. {
  102. CriticalSection sect;
  103. IPointerArrayOf<ISocket> sockets;
  104. SocketEndpointArray eps;
  105. unsigned i;
  106. for (i=0;i<n;i++) {
  107. SocketEndpoint ep;
  108. if (!getTcpTarget(modules[i]->queryTransport(),ep))
  109. return false;
  110. eps.append(ep);
  111. done[i] = false;
  112. }
  113. multiConnect(eps,sockets,60*1000);
  114. assertex(n==sockets.ordinality());
  115. bool ret = true;
  116. for (i=0;i<n;i++) {
  117. ISocket *sock = sockets.item(i);
  118. if (sock) {
  119. modules[i]->AttachConnect(sock,true);
  120. done[i] = true;
  121. }
  122. else {
  123. StringBuffer epstr;
  124. eps.item(i).getUrlStr(epstr);
  125. //ERRLOG("Failed to connect to %s",epstr.str());
  126. ret = false;
  127. }
  128. }
  129. return ret;
  130. }
  131. void MultipleConnect(unsigned n,HRPCmodule **modules,int timeout,bool fast)
  132. {
  133. CriticalBlock block(mcsect);
  134. bool *done = new bool[n];
  135. unsigned i;
  136. for (i=0;i<n;i++)
  137. done[i] = false;
  138. if (fast) {
  139. if (FastMultipleConnect(n,modules,done,timeout)) {
  140. delete [] done;
  141. return;
  142. }
  143. WARNLOG("FastMultipleConnect failed, falling back to MultipleConnect");
  144. }
  145. class casyncfor: public CAsyncFor
  146. {
  147. HRPCmodule **modules;
  148. bool *done;
  149. int timeout;
  150. public:
  151. casyncfor(HRPCmodule **_modules,bool *_done,int _timeout)
  152. {
  153. modules = _modules;
  154. done = _done;
  155. timeout = _timeout;
  156. }
  157. void Do(unsigned idx)
  158. {
  159. if (!done[idx])
  160. modules[idx]->TryConnect(timeout,true,true);
  161. }
  162. } afor(modules,done,timeout);
  163. afor.For(n,MAXCONNECTIONS,false,true);
  164. delete [] done;
  165. }
  166. void MultipleConnect(unsigned n,HRPCmodule *modules,int timeout,bool fast)
  167. {
  168. HRPCmodule **moduleptrs = (HRPCmodule **)malloc(n*sizeof(HRPCmodule *));
  169. for (unsigned i=0;i<n;i++)
  170. moduleptrs[i] = &modules[i];
  171. MultipleConnect(n,moduleptrs,timeout,fast);
  172. free(moduleptrs);
  173. }