aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/network/rpcservice.cpp
blob: fd1b84f545f35ad9812d784b1da1dbd699e5698b (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "rpcservice.h"
#include "rpcnetwork.h"

namespace mbus {

RPCService::RPCService(const Mirror &mirror, const string &pattern) :
    _mirror(mirror),
    _pattern(pattern),
    _addressIdx(random()),
    _addressGen(0),
    _addressList()
{ }

RPCService::~RPCService() = default;

RPCServiceAddress::UP
RPCService::resolve()
{
    if (_pattern.find("tcp/") == 0) {
        size_t pos = _pattern.find_last_of('/');
        if (pos != string::npos && pos < _pattern.size() - 1) {
            auto ret = std::make_unique<RPCServiceAddress>(_pattern, _pattern.substr(0, pos));
            if (!ret->isMalformed()) {
                return ret;
            }
        }
    } else {
        if (_addressGen != _mirror.updates()) {
            _addressGen = _mirror.updates();
            _addressList = _mirror.lookup(_pattern);
        }
        if (!_addressList.empty()) {
            _addressIdx = (_addressIdx + 1) % _addressList.size();
            const AddressList::value_type &entry = _addressList[_addressIdx];
            return std::make_unique<RPCServiceAddress>(entry.first, entry.second);
        }
    }
    return RPCServiceAddress::UP();
}


} // namespace mbus