-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMessage.cpp
130 lines (109 loc) · 2.98 KB
/
Message.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
#include <boost/lexical_cast.hpp>
#include <iterator>
#include <algorithm>
#include "Message.h"
#include "Exceptions.h"
using namespace FCPLib;
Message::Ptr
Message::factory(std::string header, bool isData){
Message::Ptr m;
if (!isData)
m = Message::Ptr( new Message() );
else
m = Message::Ptr( new DataMessage() );
m->header = header;
return m;
}
void
Message::setField(std::string key, std::string value) {
isReprValid = false;
fields[key] = value;
}
void
Message::setFields(const std::map<std::string, std::string> &fields) {
for (std::map<std::string, std::string>::const_iterator it = fields.begin(); it != fields.end(); ++it) {
this->setField(it->first, it->second);
}
}
const std::string
Message::getField(std::string key) const
{
std::map<std::string, std::string>::const_iterator it;
it = fields.find(key);
if (it == fields.end())
return "";
else
return it->second;
}
void
Message::setStream(std::istream* s_, int dataLength)
{
throw NotImplemented("Message::setStream is not implemented.");
}
const std::string&
Message::toString() {
if (isReprValid)
return repr;
repr = header + "\n";
for (std::map<std::string, std::string>::iterator it = fields.begin(); it != fields.end(); ++it)
repr += it->first + "=" + it->second + "\n";
if (!isDataType)
repr += "EndMessage\n";
isReprValid = true;
return repr;
}
const std::string&
Message::getHeader() const
{
return header;
}
void
Message::toSocket(boost::asio::ip::tcp::socket& socket)
{
boost::system::error_code error;
if (!socket.is_open())
throw std::runtime_error("Message::toSocket :: socket is closed");
boost::asio::write(socket, boost::asio::buffer(toString()), boost::asio::transfer_all(), error);
if (error)
throw boost::system::system_error(error);
}
void
DataMessage::setStream(std::istream* s_, int dataLength)
{
stream_ = s_;
dataLength_ = dataLength;
}
const std::string&
DataMessage::toString() {
if (isReprValid)
return repr;
Message::toString();
repr += "DataLength=" + boost::lexical_cast<std::string>(dataLength_) + "\n";
repr += "Data\n";
isReprValid = true;
return repr;
}
void
DataMessage::toSocket(boost::asio::ip::tcp::socket& socket)
{
boost::system::error_code error;
char buf[1024];
int tmp = dataLength_;
if (!socket.is_open())
throw std::runtime_error("DataMessage::toSocket :: socket is closed");
boost::asio::write( socket, boost::asio::buffer(toString()), boost::asio::transfer_all(), error );
if (error)
throw boost::system::system_error(error);
while (tmp > 0) {
int m = std::min<int>(tmp, 1024);
stream_->read(buf, m);
if (stream_->fail())
throw std::runtime_error("Error while reading data stream.");
if (!socket.is_open())
throw std::runtime_error("DataMessage::toSocket :: socket is closed");
boost::asio::write( socket, boost::asio::buffer(buf, m), boost::asio::transfer_all(), error );
if (error)
throw boost::system::system_error(error);
tmp -= m;
}
}