-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.hpp
52 lines (40 loc) · 1.33 KB
/
message.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
#ifndef DHTSIM_MESSAGE_H
#define DHTSIM_MESSAGE_H
#include <random>
#include <vector>
#include <sstream>
#include <nop/structure.h>
#include <nop/serializer.h>
#include <nop/utility/stream_reader.h>
#include <nop/utility/stream_writer.h>
namespace dhtsim {
template <typename A> class Message {
private:
public:
unsigned int type;
A originator, destination;
unsigned long tag;
unsigned int hops;
std::vector<unsigned char> data;
Message() {}
Message(unsigned int type, A originator, A destination,
unsigned long tag, std::vector<unsigned char> data)
: type(type), originator(originator), destination(destination),
tag(tag), hops(0), data(data) {}
};
template <typename T, typename A> static void writeToMessage(T msg_data, Message<A>& m) {
nop::Serializer<nop::StreamWriter<std::stringstream>> serializer;
serializer.Write(msg_data);
const std::string data = serializer.writer().take().str();
m.data.resize(data.length());
std::copy(data.begin(), data.end(), m.data.begin());
}
template <typename T, typename A> static void readFromMessage(T& msg_data, const Message<A>& m) {
std::string data(m.data.begin(), m.data.end());
std::stringstream ss;
ss.str(data);
nop::Deserializer<nop::StreamReader<std::stringstream>> deserializer{std::move(ss)};
deserializer.Read(&msg_data);
}
}
#endif