-
Notifications
You must be signed in to change notification settings - Fork 0
/
void.cpp
executable file
·269 lines (226 loc) · 7.8 KB
/
void.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/******************************************************************************\
* _ ___ ____ __ _ *
* | | | \ \___/ / \/ | ___ __ _ ___| |_ *
* | |_| |\ /| |\/| |/ __/ _` / __| __| *
* | _ | \ - / | | | | (_| (_| \__ \ |_ *
* |_| |_| \_/ |_| |_|\___\__,_|___/\__| *
* *
* This file is part of the HAMcast project. *
* *
* HAMcast is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* HAMcast is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with HAMcast. If not, see <http://www.gnu.org/licenses/>. *
* *
* Contact: HAMcast support <[email protected]> *
\******************************************************************************/
#include <cstddef>
#include <sstream>
#include <iostream>
#include <thread>
#include <mutex>
#include <functional>
#include "hamcast/hamcast_module.h"
#include "hamcast/hamcast_logging.h"
#include "void.hpp"
using std::cout;
using std::endl;
namespace {
hc_uri_list_t empty_uri_list()
{
hc_uri_list_t result;
result.next = 0;
result.type = HC_IGNORED;
result.uri_obj = 0;
result.uri_str = 0;
return result;
}
inline void_module* self(hc_module_instance_t instance)
{
return reinterpret_cast<void_module*>(instance);
}
} // namespace <anonymous>
void_module::void_module(hc_event_callback_t ecb, hc_recv_callback_t rcb)
: m_recv(rcb), m_event_cb(ecb), m_handle(0)
{
generator_running = false;
}
void_module::~void_module()
{
HC_LOG_TRACE("");
}
void void_module::set_handle(hc_module_instance_handle_t hdl)
{
m_handle = hdl;
}
void void_module::join(const uri& what)
{
HC_LOG_TRACE("uri = " << what.str());
if (m_handle)
{
m_event_cb(m_handle,
static_cast<int>(hamcast::join_event),
&what,
what.c_str());
}
if (what == "jo:ker")
{
std::lock_guard<std::mutex> guard(generator_mtx);
if (generator_running == false)
{
// go crazy
generator_thread = std::thread(std::bind(void_module_generator_loop, this));
generator_running = true;
}
}
}
void void_module::leave(const uri& what)
{
HC_LOG_TRACE("uri = " << what.str());
if (m_handle)
{
m_event_cb(m_handle,
static_cast<int>(hamcast::leave_event),
&what,
what.c_str());
}
}
void void_module::send(const uri& whom, const void*, int len, unsigned char)
{
HC_LOG_TRACE("uri = " << whom << " (" << len << " bytes)");
}
hamcast::uri void_module::map(const uri& what)
{
return what;
}
void void_module::generator_loop()
{
hamcast::uri joker("jo:ker");
std::string quote("Ever danced with the devil in the pale moonlight?");
for (;;)
{
// generate messages at the speed of light
m_recv(m_handle, quote.c_str(), quote.size(), &joker, joker.c_str());
}
}
void void_module::neighbor_set(std::vector<uri>&) { }
void void_module::group_set(std::vector<std::pair<uri, int> >&) { }
void void_module::children_set(std::vector<uri>&, const uri&) { }
void void_module::parent_set(std::vector<uri>&, const uri&) { }
bool void_module::designated_host(const uri&) { return false; }
void void_module_generator_loop(void_module* self)
{
self->generator_loop();
}
extern "C" void hc_init(hc_log_fun_t log_fun,
struct hc_module_handle* handle,
hc_new_instance_callback_t new_instance_cb,
hc_recv_callback_t recv_cb,
hc_event_callback_t event_cb,
hc_atomic_msg_size_callback_t,
size_t max_message_size,
hc_kvp_list_t*)
{
hc_set_log_fun(log_fun);
void_module* _this = new void_module(event_cb, recv_cb);
// build kvp list
std::string name_key = "if_name";
std::stringstream name_stream;
name_stream << "void_module(0x"
<< std::hex
<< reinterpret_cast<std::size_t>(_this)
<< ")";
std::string name_val = name_stream.str();
std::string addr_key = "if_addr";
std::string addr_val = "localhost";
std::string tech_key = "if_tech";
std::string tech_val = "none";
hc_kvp_list_t name;
name.key = name_key.c_str();
name.value = name_val.c_str();
name.next = 0;
hc_kvp_list_t addr;
addr.key = addr_key.c_str();
addr.value = addr_val.c_str();
addr.next = &name;
hc_kvp_list_t tech;
tech.key = tech_key.c_str();
tech.value = tech_val.c_str();
tech.next = &addr;
hc_module_instance_handle_t hdl = new_instance_cb(_this, handle, &tech, max_message_size);
_this->set_handle(hdl);
}
extern "C" int hc_join(hc_module_instance_t instance,
const hc_uri_t* group_uri,
const char*)
{
self(instance)->join(*group_uri);
return HC_SUCCESS;
}
extern "C" int hc_leave(hc_module_instance_t instance,
const hc_uri_t* group_uri,
const char*)
{
self(instance)->leave(*group_uri);
return HC_SUCCESS;
}
extern "C" int hc_sendto(hc_module_instance_t instance,
const void* buf,
int slen,
unsigned char ttl,
const hc_uri_t* group_uri,
const char*)
{
self(instance)->send(*group_uri, buf, slen, ttl);
return HC_SUCCESS;
}
extern "C" void hc_delete_instance(hc_module_instance_t instance)
{
delete self(instance);
}
extern "C" void hc_shutdown()
{
}
extern "C" hc_uri_result_t hc_map(hc_module_instance_t,
const hc_uri_t* group_uri,
const char*)
{
hc_uri_result_t result;
result.uri_obj = new hamcast::uri(*group_uri);
result.uri_str = NULL;
return result;
}
extern "C" hc_uri_list_t hc_neighbor_set(hc_module_instance_t)
{
return empty_uri_list();
}
extern "C" hc_uri_list_t hc_group_set(hc_module_instance_t)
{
return empty_uri_list();
}
extern "C" hc_uri_list_t hc_children_set(hc_module_instance_t,
const hc_uri_t*,
const char*)
{
return empty_uri_list();
}
extern "C" hc_uri_list_t hc_parent_set(hc_module_instance_t,
const hc_uri_t*,
const char*)
{
return empty_uri_list();
}
extern "C" int hc_designated_host(hc_module_instance_t,
const hc_uri_t*,
const char*)
{
return HC_IS_NOT_A_DESIGNATED_HOST;
}