factory.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "factory.h"
  2. #include "udp.h"
  3. #include "netliba.h"
  4. #include "https.h"
  5. #include "http2.h"
  6. #include "inproc.h"
  7. #include "tcp.h"
  8. #include "tcp2.h"
  9. #include <util/generic/hash.h>
  10. #include <util/generic/strbuf.h>
  11. #include <util/generic/singleton.h>
  12. using namespace NNeh;
  13. namespace {
  14. class TProtocolFactory: public IProtocolFactory, public THashMap<TStringBuf, IProtocol*> {
  15. public:
  16. inline TProtocolFactory() {
  17. Register(NetLibaProtocol());
  18. Register(Http1Protocol());
  19. Register(Post1Protocol());
  20. Register(Full1Protocol());
  21. Register(UdpProtocol());
  22. Register(InProcProtocol());
  23. Register(TcpProtocol());
  24. Register(Tcp2Protocol());
  25. Register(Http2Protocol());
  26. Register(Post2Protocol());
  27. Register(Full2Protocol());
  28. Register(SSLGetProtocol());
  29. Register(SSLPostProtocol());
  30. Register(SSLFullProtocol());
  31. Register(UnixSocketGetProtocol());
  32. Register(UnixSocketPostProtocol());
  33. Register(UnixSocketFullProtocol());
  34. }
  35. IProtocol* Protocol(const TStringBuf& proto) override {
  36. const_iterator it = find(proto);
  37. if (it == end()) {
  38. ythrow yexception() << "unsupported scheme " << proto;
  39. }
  40. return it->second;
  41. }
  42. void Register(IProtocol* proto) override {
  43. (*this)[proto->Scheme()] = proto;
  44. }
  45. };
  46. IProtocolFactory* GLOBAL_FACTORY = nullptr;
  47. }
  48. void NNeh::SetGlobalFactory(IProtocolFactory* factory) {
  49. GLOBAL_FACTORY = factory;
  50. }
  51. IProtocolFactory* NNeh::ProtocolFactory() {
  52. if (GLOBAL_FACTORY) {
  53. return GLOBAL_FACTORY;
  54. }
  55. return Singleton<TProtocolFactory>();
  56. }