Added reconnect-mechanism in JobClient with random backoff time.
git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1001 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
@ -1,39 +1,55 @@
|
|||||||
#include "JobClient.hpp"
|
#include "JobClient.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace fi {
|
namespace fi {
|
||||||
|
|
||||||
JobClient::JobClient(std::string server, int port)
|
JobClient::JobClient(std::string server, int port)
|
||||||
{
|
{
|
||||||
m_server_port = port;
|
m_server_port = port;
|
||||||
m_server = server;
|
m_server = server;
|
||||||
m_server_ent = gethostbyname(m_server.c_str());
|
m_server_ent = gethostbyname(m_server.c_str());
|
||||||
if(m_server_ent == NULL){
|
if(m_server_ent == NULL) {
|
||||||
perror("Cannot resolve host.");
|
perror("[Client@gethostbyname()]");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
srand(time(NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JobClient::connectToServer(){
|
bool JobClient::connectToServer()
|
||||||
// Connect to server
|
{
|
||||||
struct sockaddr_in serv_addr;
|
// Connect to server
|
||||||
m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
struct sockaddr_in serv_addr;
|
||||||
if(m_sockfd < 0){
|
m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
perror("socket");
|
if(m_sockfd < 0) {
|
||||||
exit(0);
|
perror("[Client@socket()]");
|
||||||
}
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
/* Enable address reuse */
|
/* Enable address reuse */
|
||||||
int on = 1;
|
int on = 1;
|
||||||
setsockopt( m_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
|
setsockopt( m_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
|
||||||
|
|
||||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||||
serv_addr.sin_family = AF_INET;
|
serv_addr.sin_family = AF_INET;
|
||||||
memcpy(&serv_addr.sin_addr.s_addr, m_server_ent->h_addr, m_server_ent->h_length);
|
memcpy(&serv_addr.sin_addr.s_addr, m_server_ent->h_addr, m_server_ent->h_length);
|
||||||
serv_addr.sin_port = htons(m_server_port);
|
serv_addr.sin_port = htons(m_server_port);
|
||||||
|
|
||||||
if (connect(m_sockfd, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
|
||||||
perror("connect()");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int retries = 3;
|
||||||
|
while(true) {
|
||||||
|
if(connect(m_sockfd, (sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||||
|
perror("[Client@connect()]");
|
||||||
|
if(retries > 0) {
|
||||||
|
int delay = rand() % 5 + 3;
|
||||||
|
cout << "[Client] Retrying to connect to server in " << delay << "s..." << endl;
|
||||||
|
sleep(delay);
|
||||||
|
--retries;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return false; // finally: unable to connect, give it up :-(
|
||||||
|
}
|
||||||
|
break; // connected! :-)
|
||||||
|
}
|
||||||
|
cout << "[Client] Connected established!" << endl;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -89,7 +105,7 @@ bool JobClient::sendResult(ExperimentData& result)
|
|||||||
ctrlmsg.set_command(FailControlMessage_Command_RESULT_FOLLOWS);
|
ctrlmsg.set_command(FailControlMessage_Command_RESULT_FOLLOWS);
|
||||||
ctrlmsg.set_build_id(42);
|
ctrlmsg.set_build_id(42);
|
||||||
ctrlmsg.set_workloadid(result.getWorkloadID());
|
ctrlmsg.set_workloadid(result.getWorkloadID());
|
||||||
cout << "Sending back result [" << std::dec << result.getWorkloadID() << "]" << endl;
|
cout << "[Client] Sending back result [" << std::dec << result.getWorkloadID() << "]..." << endl;
|
||||||
SocketComm::send_msg(m_sockfd, ctrlmsg);
|
SocketComm::send_msg(m_sockfd, ctrlmsg);
|
||||||
SocketComm::send_msg(m_sockfd, result.getMessage());
|
SocketComm::send_msg(m_sockfd, result.getMessage());
|
||||||
// close connection.
|
// close connection.
|
||||||
|
|||||||
@ -8,8 +8,12 @@
|
|||||||
#ifndef __JOB_CLIENT_H__
|
#ifndef __JOB_CLIENT_H__
|
||||||
#define __JOB_CLIENT_H__
|
#define __JOB_CLIENT_H__
|
||||||
|
|
||||||
#include "SocketComm.hpp"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <ctime>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "SocketComm.hpp"
|
||||||
#include "controller/ExperimentData.hpp"
|
#include "controller/ExperimentData.hpp"
|
||||||
#include "jobserver/messagedefs/FailControlMessage.pb.h"
|
#include "jobserver/messagedefs/FailControlMessage.pb.h"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user