-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingonly.hpp
57 lines (45 loc) · 1.29 KB
/
pingonly.hpp
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
#ifndef DHTSIM_PINGONLY_H
#define DHTSIM_PINGONLY_H
#include "application.hpp"
#include "base.hpp"
#include "message.hpp"
#include <iostream>
#include <optional>
namespace dhtsim {
enum PingMessageType {
PM_PING, PM_PONG,
};
template <typename A> class PingOnlyApplication : public BaseApplication<A> {
public:
using MessageCallbackSet = typename Application<A>::template CallbackSet<>;
PingOnlyApplication(){};
void ping(A other, MessageCallbackSet callback = MessageCallbackSet());
virtual void handleMessage(const Message<A>& m);
virtual ~PingOnlyApplication(){};
};
template <typename A> void PingOnlyApplication<A>::ping(A other, MessageCallbackSet callback) {
auto tag = this->randomTag();
std::vector<unsigned char> data;
Message<A> m(PM_PING, this->getAddress(), other, tag, data);
this->send(m, callback);
}
template <typename A> void PingOnlyApplication<A>::handleMessage(const Message<A>& m) {
BaseApplication<A>::handleMessage(m);
auto resp = m;
switch (m.type) {
case PM_PING:
// Rhandle_messageg! Send a message back.
resp.type = PM_PONG;
resp.destination = m.originator;
resp.originator = this->getAddress();
this->send(resp);
break;
case PM_PONG:
// Received a pong.
break;
default:
std::clog << "received an unknown message!" << std::endl;
}
}
}
#endif