Apollo Cyber Study. P11 Transport 2

// Study: 是我的筆記

cyber/transport/dispatcher/dispatcher

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#ifndef CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_
#define CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_

#include <atomic>
#include <functional>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>

#include "cyber/base/atomic_hash_map.h"
#include "cyber/base/atomic_rw_lock.h"
#include "cyber/common/global_data.h"
#include "cyber/common/log.h"
#include "cyber/proto/role_attributes.pb.h"
#include "cyber/transport/message/listener_handler.h"
#include "cyber/transport/message/message_info.h"

namespace apollo {
namespace cyber {
namespace transport {

using apollo::cyber::base::AtomicHashMap;
using apollo::cyber::base::AtomicRWLock;
using apollo::cyber::base::ReadLockGuard;
using apollo::cyber::base::WriteLockGuard;
using apollo::cyber::common::GlobalData;
using cyber::proto::RoleAttributes;

class Dispatcher;
using DispatcherPtr = std::shared_ptr<Dispatcher>;

template <typename MessageT>
// Study: Fuck this naming, if it is a callback, just name it xxxCallback
// Name it XXXListener will make people think this is a class instead of function
using MessageListener =
std::function<void(const std::shared_ptr<MessageT>&, const MessageInfo&)>;

class Dispatcher {
public:
Dispatcher();
virtual ~Dispatcher();

virtual void Shutdown();

template <typename MessageT>
void AddListener(const RoleAttributes& self_attr,
const MessageListener<MessageT>& listener);

template <typename MessageT>
void AddListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr,
const MessageListener<MessageT>& listener);

template <typename MessageT>
void RemoveListener(const RoleAttributes& self_attr);

template <typename MessageT>
void RemoveListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr);

bool HasChannel(uint64_t channel_id);

protected:
std::atomic<bool> is_shutdown_;
// key: channel_id of message
AtomicHashMap<uint64_t, ListenerHandlerBasePtr> msg_listeners_;
base::AtomicRWLock rw_lock_;
};

template <typename MessageT>
void Dispatcher::AddListener(const RoleAttributes& self_attr,
const MessageListener<MessageT>& listener) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id();

// Study: ListenerHandler is just one more layer of abstract
// It make the work that can be done in 1 abstract level
// into 3 to 4 layer, fuck it
std::shared_ptr<ListenerHandler<MessageT>> handler;
ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) {
handler =
std::dynamic_pointer_cast<ListenerHandler<MessageT>>(*handler_base);
if (handler == nullptr) {
AERROR << "please ensure that readers with the same channel["
<< self_attr.channel_name()
<< "] in the same process have the same message type";
return;
}
} else {
ADEBUG << "new reader for channel:"
<< GlobalData::GetChannelById(channel_id);
// Study Do think too much, Just a abstract to maintain all callback
handler.reset(new ListenerHandler<MessageT>());
msg_listeners_.Set(channel_id, handler);
}
handler->Connect(self_attr.id(), listener);
}

template <typename MessageT>
void Dispatcher::AddListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr,
const MessageListener<MessageT>& listener) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id();

std::shared_ptr<ListenerHandler<MessageT>> handler;
ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) {
// Study: Assume all the existing handler for same channel is same type
handler =
std::dynamic_pointer_cast<ListenerHandler<MessageT>>(*handler_base);
if (handler == nullptr) {
AERROR << "please ensure that readers with the same channel["
<< self_attr.channel_name()
<< "] in the same process have the same message type";
return;
}
} else {
ADEBUG << "new reader for channel:"
<< GlobalData::GetChannelById(channel_id);
handler.reset(new ListenerHandler<MessageT>());
msg_listeners_.Set(channel_id, handler);
}
handler->Connect(self_attr.id(), opposite_attr.id(), listener);
}

template <typename MessageT>
void Dispatcher::RemoveListener(const RoleAttributes& self_attr) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id();

ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) {
(*handler_base)->Disconnect(self_attr.id());
}
}

template <typename MessageT>
void Dispatcher::RemoveListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id();

ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) {
(*handler_base)->Disconnect(self_attr.id(), opposite_attr.id());
}
}

} // namespace transport
} // namespace cyber
} // namespace apollo

#endif // CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_

cyber/transport/receiver/receiver

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
68
69
70
71
72
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#ifndef CYBER_TRANSPORT_RECEIVER_RECEIVER_H_
#define CYBER_TRANSPORT_RECEIVER_RECEIVER_H_

#include <functional>
#include <memory>

#include "cyber/transport/common/endpoint.h"
#include "cyber/transport/message/history.h"
#include "cyber/transport/message/message_info.h"

namespace apollo {
namespace cyber {
namespace transport {

template <typename M>
class Receiver : public Endpoint {
public:
using MessagePtr = std::shared_ptr<M>;
using MessageListener = std::function<void(
const MessagePtr&, const MessageInfo&, const RoleAttributes&)>;

Receiver(const RoleAttributes& attr, const MessageListener& msg_listener);
virtual ~Receiver();

virtual void Enable() = 0;
virtual void Disable() = 0;
virtual void Enable(const RoleAttributes& opposite_attr) = 0;
virtual void Disable(const RoleAttributes& opposite_attr) = 0;

protected:
void OnNewMessage(const MessagePtr& msg, const MessageInfo& msg_info);

MessageListener msg_listener_;
};

template <typename M>
Receiver<M>::Receiver(const RoleAttributes& attr,
const MessageListener& msg_listener)
: Endpoint(attr), msg_listener_(msg_listener) {}

template <typename M>
Receiver<M>::~Receiver() {}

template <typename M>
void Receiver<M>::OnNewMessage(const MessagePtr& msg,
const MessageInfo& msg_info) {
if (msg_listener_ != nullptr) {
msg_listener_(msg, msg_info, attr_);
}
}

} // namespace transport
} // namespace cyber
} // namespace apollo

#endif // CYBER_TRANSPORT_RECEIVER_RECEIVER_H_