aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/factory.cpp
blob: 5b9fd700e69e99cc48ec33785e3be83043ace66b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "factory.h"
#include "udp.h"
#include "netliba.h"
#include "https.h"
#include "http2.h"
#include "inproc.h"
#include "tcp.h"
#include "tcp2.h"

#include <util/generic/hash.h>
#include <util/generic/strbuf.h>
#include <util/generic/singleton.h>

using namespace NNeh;

namespace {
    class TProtocolFactory: public IProtocolFactory, public THashMap<TStringBuf, IProtocol*> {
    public:
        inline TProtocolFactory() {
            Register(NetLibaProtocol());
            Register(Http1Protocol());
            Register(Post1Protocol());
            Register(Full1Protocol());
            Register(UdpProtocol());
            Register(InProcProtocol());
            Register(TcpProtocol());
            Register(Tcp2Protocol());
            Register(Http2Protocol());
            Register(Post2Protocol());
            Register(Full2Protocol());
            Register(SSLGetProtocol());
            Register(SSLPostProtocol());
            Register(SSLFullProtocol());
            Register(UnixSocketGetProtocol());
            Register(UnixSocketPostProtocol());
            Register(UnixSocketFullProtocol());
        }

        IProtocol* Protocol(const TStringBuf& proto) override {
            const_iterator it = find(proto);

            if (it == end()) {
                ythrow yexception() << "unsupported scheme " << proto;
            }

            return it->second;
        }

        void Register(IProtocol* proto) override {
            (*this)[proto->Scheme()] = proto;
        }
    };

    IProtocolFactory* GLOBAL_FACTORY = nullptr;
}

void NNeh::SetGlobalFactory(IProtocolFactory* factory) {
    GLOBAL_FACTORY = factory;
}

IProtocolFactory* NNeh::ProtocolFactory() {
    if (GLOBAL_FACTORY) {
        return GLOBAL_FACTORY;
    }

    return Singleton<TProtocolFactory>();
}