jsmartsock.ipp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef JSMARTSOCK_IPP
  14. #define JSMARTSOCK_IPP
  15. #include "jsmartsock.hpp"
  16. #include "jlog.hpp"
  17. struct SmartSocketEndpoint
  18. {
  19. SocketEndpoint ep;
  20. StringBuffer name;
  21. unsigned lastHostUpdate;
  22. bool status;
  23. SmartSocketEndpoint(const char *_name,unsigned short port=0) : ep(_name, port)
  24. {
  25. if (ep.isNull())
  26. throw MakeStringException(-1,"SmartSocketEndpoint resolution failed for '%s' %d",_name,port);
  27. StringBuffer ipStr;
  28. ep.getIpText(ipStr);
  29. if (strcmp(ipStr.str(), _name)!=0)
  30. name.append(_name);
  31. lastHostUpdate=msTick();
  32. status = true;
  33. }
  34. void checkHost(unsigned interval)
  35. {
  36. if (interval!=((unsigned) -1))
  37. {
  38. if (name.length() && ((msTick()-lastHostUpdate)>(interval*1000)))
  39. {
  40. ep.set(name, ep.port);
  41. lastHostUpdate=msTick();
  42. }
  43. }
  44. }
  45. };
  46. class SmartSocketEndpointArray : public SafePointerArrayOf<SmartSocketEndpoint> {};
  47. class jlib_decl CSmartSocketFactory: public Thread,
  48. implements ISmartSocketFactory
  49. {
  50. SmartSocketEndpointArray sockArray;
  51. Mutex lock;
  52. unsigned nextEndpointIndex;
  53. bool retry;
  54. unsigned retryInterval;
  55. unsigned dnsInterval;
  56. void shuffleEndpoints();
  57. SmartSocketEndpoint *findEndpoint(SocketEndpoint &ep);
  58. public:
  59. IMPLEMENT_IINTERFACE;
  60. CSmartSocketFactory(const char *_socklist, bool _retry = false, unsigned _retryInterval = 60, unsigned _dnsInterval = (unsigned)-1);
  61. ~CSmartSocketFactory();
  62. int run();
  63. bool getStatus(SocketEndpoint &ep);
  64. void setStatus(SocketEndpoint &ep, bool status);
  65. ISmartSocket *connect();
  66. ISmartSocket *connect_timeout( unsigned timeoutms = 0);
  67. ISmartSocket *connectNextAvailableSocket();
  68. SmartSocketEndpoint *nextSmartEndpoint();
  69. SocketEndpoint& nextEndpoint();
  70. virtual void stop();
  71. virtual void resolveHostnames();
  72. };
  73. class jlib_thrown_decl SmartSocketException: public CInterface, public ISmartSocketException
  74. {
  75. public:
  76. IMPLEMENT_IINTERFACE;
  77. SmartSocketException(int code, const char *_msg) : errcode(code), msg(_msg) { };
  78. int errorCode() const { return (errcode); };
  79. StringBuffer & errorMessage(StringBuffer &str) const
  80. {
  81. return str.append("CSmartSocket: (").append(msg).append(")");
  82. };
  83. MessageAudience errorAudience() const { return (MSGAUD_user); };
  84. private:
  85. int errcode;
  86. StringAttr msg;
  87. };
  88. ISmartSocketException *createSmartSocketException(int errorCode, const char *msg)
  89. {
  90. return new SmartSocketException(errorCode, msg);
  91. }
  92. #endif