Message sending

This commit is contained in:
Julien CLEMENT 2020-05-05 20:50:55 +02:00
parent 6b80cc68ce
commit 0354ce0bea
5 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,16 @@
{
"legislators" : [
{
"ip" : "127.0.0.1",
"port" : 8000,
"name" : "legislator1",
"self" : true
},
{
"ip" : "127.0.0.1",
"port" : 8001,
"name" : "legislator2",
"self" : false
}
]
}

View File

@ -0,0 +1,16 @@
{
"legislators" : [
{
"ip" : "127.0.0.1",
"port" : 8000,
"name" : "legislator1",
"self" : false
},
{
"ip" : "127.0.0.1",
"port" : 8001,
"name" : "legislator2",
"self" : true
}
]
}

View File

@ -0,0 +1 @@
9

View File

@ -0,0 +1 @@
0

View File

@ -21,7 +21,33 @@ namespace paxos
void SendEW::operator()()
{
shared_socket sock = connection_->get_socket();
auto buffer = connection_->get_buffer_out();
std::string response = buffer.to_string();
log("Sending a message: ", blue);
log(response, white);
ssize_t write = 0;
try
{
write = sock->send(response.c_str(), response.size());
}
catch (const std::system_error& e)
{
std::cerr << "Connection closed while sending\n";
event_register.unregister_ew(this);
return;
}
if (write == -1)
return;
buffer.pop_front(write);
if (!buffer.empty())
return;
event_register.unregister_ew(this);
}
static shared_socket connect_to_ip_port(std::string ip, std::string port)
@ -63,6 +89,7 @@ namespace paxos
catch (const ConnectionFailed& e)
{
log("Could not connect to " + legislator->config_.name, red);
return;
}
shared_connection connection = std::make_shared<Connection>(socket);