Skip to content
Snippets Groups Projects
Commit 28b43939 authored by Julian Pychy's avatar Julian Pychy
Browse files

missing files

parent de5a8cc7
No related branches found
No related tags found
No related merge requests found
// NetworkClient class definition file. -*- C++ -*-
// Copyright 2013 Julian Pychy
#include <iomanip>
#include <boost/asio.hpp>
#include "PwaUtils/NetworkClient.hh"
#include "PwaUtils/NetworkServer.hh"
#include "ErrLogger/ErrLogger.hh"
short NetworkClient::CLIENTMESSAGE_LOGIN = 1;
short NetworkClient::CLIENTMESSAGE_LH = 2;
NetworkClient::NetworkClient(std::string serverAddress, std::string port) :
_port(port)
, _serverAddress(serverAddress)
{
_eventLimits.resize(4, 0);
Info << "************* Client mode ****************" << endmsg;
}
bool NetworkClient::Login(){
Info << "Connecting to server " << _serverAddress << ":" << _port << endmsg;
_theStream.connect(_serverAddress, _port);
if(!_theStream){
Alert << "Error: " << _theStream.error().message() << endmsg;
return false;
}
_theStream << NetworkClient::CLIENTMESSAGE_LOGIN << "\n"
<< boost::asio::ip::host_name() << "\n";
_theStream >> _eventLimits[0] >> _eventLimits[1] >> _eventLimits[2] >> _eventLimits[3];
if(!_theStream){
Alert << "Error: " << _theStream.error().message() << endmsg;
return false;
}
Info << "Received data event range " << _eventLimits[0] << " - " << _eventLimits[1] << endmsg;
Info << "Received mc event range " << _eventLimits[2] << " - " << _eventLimits[3] << endmsg;
return true;
}
bool NetworkClient::SendLH(double llh_data, double weightSum, double lh_mc){
_theStream.connect(_serverAddress, _port);
if(!_theStream){
Alert << "Could not send LH." << endmsg;
return false;
}
_theStream << NetworkClient::CLIENTMESSAGE_LH << "\n";
_theStream << std::setprecision(16) << llh_data << "\n" << weightSum << "\n" << lh_mc << "\n";
return true;
}
bool NetworkClient::WaitForParams(){
if(!_theStream){
Alert << "Stream error." << endmsg;
return false;
}
short serverMessage;
_theStream >> serverMessage;
if(serverMessage == NetworkServer::SERVERMESSAGE_CLOSE){
Info << "Received goodbye. Exiting." << endmsg;
return false;
}
else if(serverMessage != NetworkServer::SERVERMESSAGE_PARAMS){
Alert << "Protocol error WaitForParams()" << endmsg;
return false;
}
int numParams;
_theStream >> numParams;
_theParams.clear();
for(int i=0; i< numParams; i++){
double tmpParam;
_theStream >> tmpParam;
_theParams.push_back(tmpParam);
}
return true;
}
// NetworkClient class definition file. -*- C++ -*-
// Copyright 2013 Julian Pychy
#pragma once
#include <vector>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class NetworkClient
{
public:
static short CLIENTMESSAGE_LOGIN;
static short CLIENTMESSAGE_LH;
NetworkClient(std::string serverAddress,std::string port);
bool Login();
bool SendLH(double llh_data, double weightSum, double lh_mc);
bool WaitForParams();
std::vector<double>& GetParams(){return _theParams;}
std::vector<double>& GetEventLimits(){return _eventLimits;}
private:
std::vector<double> _eventLimits;
std::string _port;
std::string _serverAddress;
tcp::iostream _theStream;
std::vector<double> _theParams;
};
// Networking class definition file. -*- C++ -*-
// Copyright 2013 Julian Pychy
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <iomanip>
#include "PwaUtils/NetworkServer.hh"
#include "PwaUtils/NetworkClient.hh"
#include "ErrLogger/ErrLogger.hh"
short NetworkServer::SERVERMESSAGE_PARAMS = 1;
short NetworkServer::SERVERMESSAGE_CLOSE = 2;
NetworkServer::NetworkServer(int port, short noOfClients, int numData, int numMC) :
_port(port)
, _timeout(10)
, _noOfClients(noOfClients)
, _firstLH(true)
, _numData(numData)
, _numMC(numMC)
{
theIOService = std::shared_ptr<boost::asio::io_service>(new boost::asio::io_service);
theAcceptor = std::shared_ptr<tcp::acceptor>(new tcp::acceptor(*theIOService, tcp::endpoint(tcp::v4(), _port)));
theDeadlineTimer = std::shared_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(*theIOService));
for(int i=0; i<_noOfClients; i++){
theStreams.push_back( std::shared_ptr<tcp::iostream>(new tcp::iostream) );
}
Info << "************* Server mode ****************" << endmsg;
Info << "Listening on port " << port << endmsg;
}
bool NetworkServer::WaitForFirstClientLogin(){
Info << "Waiting for " << _noOfClients << " clients ..." << endmsg;
for(int i=0; i<_noOfClients; i++){
theAcceptor->accept(*(theStreams.at(i)->rdbuf()));
int eventStepData = (int)((double)_numData / (double)_noOfClients);
int firstDataEvent = i*eventStepData;
int lastDataEvent = (i+1)*eventStepData - 1;
int eventStepMC = (int)((double)_numMC / (double)_noOfClients);
int firstMcEvent = i*eventStepMC;
int lastMcEvent = (i+1)*eventStepMC - 1;
if(i == (_noOfClients - 1)){
lastDataEvent = _numData - 1;
lastMcEvent = _numMC - 1;
}
short connectionPurpose;
*theStreams.at(i) >> connectionPurpose;
if(connectionPurpose != NetworkClient::CLIENTMESSAGE_LOGIN){
Alert << "ERROR: Client did not login" << endmsg;
return false;
}
std::string nodeName;
*theStreams.at(i) >> nodeName;
Info << "Client " << nodeName << " logged in." << endmsg;
*theStreams.at(i) << firstDataEvent << "\n" << lastDataEvent << "\n"
<< firstMcEvent << "\n" << lastMcEvent << "\n";
}
Info << "All clients ready." << endmsg;
return true;
}
void NetworkServer::AcceptHandler(const boost::system::error_code& err){
theDeadlineTimer->cancel();
theAcceptor->cancel();
}
void NetworkServer::Timeout(const boost::system::error_code& err){
if (err != boost::asio::error::operation_aborted){
Alert << "Timeout" << endmsg;
theAcceptor->cancel();
}
}
bool NetworkServer::WaitForLH(double& llh_data, double& weightSum, double& lh_mc){
for(int i=0; i<_noOfClients; i++){
if(_firstLH){
theDeadlineTimer->expires_from_now(boost::posix_time::seconds(300));
_firstLH=false;
}
else{
theDeadlineTimer->expires_from_now(boost::posix_time::seconds(_timeout));
}
theDeadlineTimer->async_wait(boost::bind(&NetworkServer::Timeout, this
,boost::asio::placeholders::error ));
theAcceptor->async_accept(*(theStreams.at(i)->rdbuf()),
boost::bind(&NetworkServer::AcceptHandler, this,
boost::asio::placeholders::error));
theIOService->run();
theIOService->reset();
short connectionPurpose;
*theStreams.at(i) >> connectionPurpose;
if(connectionPurpose == NetworkClient::CLIENTMESSAGE_LOGIN){
Alert << "Client tried to login too late." << endmsg;
theStreams.at(i)->close();
i--;
continue;
}
else if(connectionPurpose != NetworkClient::CLIENTMESSAGE_LH){
Alert << "Protocoll error in WaitForLH()" << endmsg;
return false;
}
double tempData, tempMc, tempWeightSum;
*theStreams.at(i) >> tempData >> tempWeightSum >> tempMc;
llh_data += tempData;
lh_mc += tempMc;
weightSum += tempWeightSum;
}
return true;
}
void NetworkServer::SendParams(std::shared_ptr<tcp::iostream> destinationStream, const std::vector<double>& par){
*destinationStream << NetworkServer::SERVERMESSAGE_PARAMS << "\n";
*destinationStream << par.size() << "\n";
for(auto it = par.begin(); it != par.end(); ++it){
*destinationStream << std::setprecision(16) << *it << "\n";
}
destinationStream->flush();
destinationStream->close();
}
void NetworkServer::BroadcastParams(const std::vector<double>& par){
for(auto it = theStreams.begin(); it != theStreams.end(); ++it){
SendParams(*it, par);
}
}
void NetworkServer::SendClosingMessage(){
for(auto it = theStreams.begin(); it != theStreams.end(); ++it){
**it << NetworkServer::SERVERMESSAGE_CLOSE << "\n";
(*it)->flush();
(*it)->close();
}
}
// NetworkServer class definition file. -*- C++ -*-
// Copyright 2013 Julian Pychy
#pragma once
#include <vector>
#include <memory>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class NetworkServer
{
public:
static short SERVERMESSAGE_PARAMS;
static short SERVERMESSAGE_CLOSE;
NetworkServer(int port, short noOfClients, int numData, int numMC);
bool WaitForLH(double& llh_data, double& weightSum, double& lh_mc);
bool WaitForFirstClientLogin();
void SendParams(std::shared_ptr<tcp::iostream> destinationStream, const std::vector<double>& par);
void BroadcastParams(const std::vector<double>& par);
void SendClosingMessage();
private:
unsigned int _port;
unsigned int _timeout;
unsigned short _noOfClients;
bool _firstLH;
int _numData;
int _numMC;
std::shared_ptr<boost::asio::io_service> theIOService;
std::shared_ptr<boost::asio::deadline_timer> theDeadlineTimer;
std::shared_ptr<tcp::acceptor> theAcceptor;
std::vector<std::shared_ptr<tcp::iostream>> theStreams;
void Timeout(const boost::system::error_code& err);
void AcceptHandler(const boost::system::error_code& err);
};
const char * welcomeScreen = R"(
MMMMM
M8NO +M
NO M ,MM
MM ?MN, D8MMMO :NMM
:MZ ZM MZ NM, M
M, :M MMN DMMN M ~
M 8MMM M M, 7O: :M, M+ N:
M =NMZ M ,M8 M ,O MM : M
D DM M $8 I= MM $M 7 M
Z~ M M O 8 8 M N MM DM
8, O MM M :MM8, ,, MMD
O: M 87MMO , MM~ ,M,
I=O M M M M M
:M M N M N, M M~
:M ?~ N 8I+ M MMMMMMMMMMMMMMMOM? 7,
M ++ M 8MZ7 M M MM
M D M N N M, ND DM8,
M M M M~M DMNM88= 8,
M 8 ?M? M M~
M N~8 ?$ : M
NN M M M M 8:
D M M DDM M M M,
D ,D D?M N M ND
M M ,O8$ ,7 M M M M~
NN 8IO M M I= = M M ,M
M M M M I M
~M ?8 M M M +M MM N
N+I8MMM~ MM M , M M D M M MON ~$
M NM M8 M O D M Z+ M: M, M M M
,MMMD 88 8M N M M M ~M M M+ M
7M, MI +ZM M M M I$ $NI M ,O M8 ?O
?N M I: M M 8 M , M M M
M +? 8, N M N :MMMMM7D M M
D M 8, N, ,$ M M 8 8:
N,D, ?: =MMMMMZ D M M M 8ZMM
M ND :Z=MO, N MM8ZN8 M M M+ ,:~MMM 8~
M DMMMMMNZ D ~MM :MM M M MM MNMM M
$7 DMMZZNOMMNMZ M =M MZ :7 MM MM$ M8 N$ M
MO MMZ Z + ,MMD + D M~8 N M M
?I= D 8 ,NMMMMO MM M 8 M ID
8 D MN, M ~, MM 8N
,MNZ, ,8MM :~ZMMM, :8 D 8 ,M N
88 :M M+ M ,D D $ M M
M: M M ,7 D 8N$ 8~ :D M M ?M,
M 7 M M M +MM? M ,8 D8 D= ,D Z? ,O
$I~ ~? Z8 M M M M 7+ M :, D$ 8Z M? M M$D+M 7M?
Z: MMN N, M M? M D M =$~ M :+ D, NMMMMM OMM MM MMM
O: :M $Z D8 M M M M M O: N M ,O M M M N
$~ M: M D DM O N M MM M :N M M +7 M M M D D
~= MNMMMM8 D: $ M ?Z M D M M M M M M M M M 7, M M D~ 7=
~I M N O :$ N M D 8 Z= M M~ M M Z N M M D, M Z
M M M D $~ M M N M M M M Z M M D M N M M
M M O M M M M M M ,M Z7 N~ N M NM M N M M M
M M M M M D M M MMM M M M M M N M N M 7$ D M M
MMM7 MN O$ M M N M + M M $ N +I ~+ M M , M M M
MMM+ M M M M, N M O= M M ~? M M M~NN M 8 M
N $$ 7M M+ D 8 M M M D, M M M M ,N 8 D
D8: M OM MN D M , M M M M O $ +~ $MZ D
:M88DM N +MM+ MMM$M N~+~N D, M :8 D M N
MMM M M Z= M M D
)";
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment