mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 17:41:45 +00:00
Update FairMQStateMachine & introduce FairMQChannels
Organize sockets as a map of vectors of FairMQChannels. Update FairMQStateMachine by removing SETTINGINPUT, SETTINGOUTPUT, BIND and CONNECT states and by adding INITIALIZING_TASK, RESETTING_TASK and RESETTING_DEVICE states. Run states functions in their own thread.
This commit is contained in:
@@ -36,9 +36,7 @@ void FairMQExampleClient::CustomCleanup(void *data, void *hint)
|
||||
|
||||
void FairMQExampleClient::Run()
|
||||
{
|
||||
// boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
|
||||
|
||||
while (fState == RUNNING)
|
||||
while (GetCurrentState() == RUNNING)
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
||||
|
||||
@@ -49,26 +47,17 @@ void FairMQExampleClient::Run()
|
||||
|
||||
LOG(INFO) << "Sending \"" << fText << "\" to server.";
|
||||
|
||||
fPayloadOutputs->at(0)->Send(request);
|
||||
fPayloadOutputs->at(0)->Receive(reply);
|
||||
fChannels["data"].at(0).Send(request);
|
||||
fChannels["data"].at(0).Receive(reply);
|
||||
|
||||
LOG(INFO) << "Received reply from server: \"" << string(static_cast<char*>(reply->GetData()), reply->GetSize()) << "\"";
|
||||
|
||||
delete reply;
|
||||
}
|
||||
|
||||
// rateLogger.interrupt();
|
||||
// rateLogger.join();
|
||||
|
||||
FairMQDevice::Shutdown();
|
||||
|
||||
boost::lock_guard<boost::mutex> lock(fRunningMutex);
|
||||
fRunningFinished = true;
|
||||
fRunningCondition.notify_one();
|
||||
}
|
||||
|
||||
|
||||
void FairMQExampleClient::SetProperty(const int key, const string& value, const int slot /*= 0*/)
|
||||
void FairMQExampleClient::SetProperty(const int key, const string& value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
@@ -76,12 +65,12 @@ void FairMQExampleClient::SetProperty(const int key, const string& value, const
|
||||
fText = value;
|
||||
break;
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value, slot);
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string FairMQExampleClient::GetProperty(const int key, const string& default_ /*= ""*/, const int slot /*= 0*/)
|
||||
string FairMQExampleClient::GetProperty(const int key, const string& default_ /*= ""*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
@@ -89,25 +78,25 @@ string FairMQExampleClient::GetProperty(const int key, const string& default_ /*
|
||||
return fText;
|
||||
break;
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_, slot);
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQExampleClient::SetProperty(const int key, const int value, const int slot /*= 0*/)
|
||||
void FairMQExampleClient::SetProperty(const int key, const int value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value, slot);
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int FairMQExampleClient::GetProperty(const int key, const int default_ /*= 0*/, const int slot /*= 0*/)
|
||||
int FairMQExampleClient::GetProperty(const int key, const int default_ /*= 0*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_, slot);
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
||||
|
@@ -30,12 +30,12 @@ class FairMQExampleClient : public FairMQDevice
|
||||
FairMQExampleClient();
|
||||
virtual ~FairMQExampleClient();
|
||||
|
||||
static void CustomCleanup(void *data, void* hint);
|
||||
static void CustomCleanup(void* data, void* hint);
|
||||
|
||||
virtual void SetProperty(const int key, const std::string& value, const int slot = 0);
|
||||
virtual std::string GetProperty(const int key, const std::string& default_ = "", const int slot = 0);
|
||||
virtual void SetProperty(const int key, const int value, const int slot = 0);
|
||||
virtual int GetProperty(const int key, const int default_ = 0, const int slot = 0);
|
||||
virtual void SetProperty(const int key, const std::string& value);
|
||||
virtual std::string GetProperty(const int key, const std::string& default_ = "");
|
||||
virtual void SetProperty(const int key, const int value);
|
||||
virtual int GetProperty(const int key, const int default_ = 0);
|
||||
|
||||
protected:
|
||||
std::string fText;
|
||||
|
@@ -31,15 +31,13 @@ void FairMQExampleServer::CustomCleanup(void *data, void *hint)
|
||||
|
||||
void FairMQExampleServer::Run()
|
||||
{
|
||||
// boost::thread rateLogger(boost::bind(&FairMQDevice::LogSocketRates, this));
|
||||
|
||||
while (fState == RUNNING)
|
||||
while (GetCurrentState() == RUNNING)
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
||||
|
||||
FairMQMessage* request = fTransportFactory->CreateMessage();
|
||||
|
||||
fPayloadInputs->at(0)->Receive(request);
|
||||
fChannels["data"].at(0).Receive(request);
|
||||
|
||||
LOG(INFO) << "Received request from client: \"" << string(static_cast<char*>(request->GetData()), request->GetSize()) << "\"";
|
||||
|
||||
@@ -51,17 +49,8 @@ void FairMQExampleServer::Run()
|
||||
|
||||
FairMQMessage* reply = fTransportFactory->CreateMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text);
|
||||
|
||||
fPayloadInputs->at(0)->Send(reply);
|
||||
fChannels["data"].at(0).Send(reply);
|
||||
}
|
||||
|
||||
// rateLogger.interrupt();
|
||||
// rateLogger.join();
|
||||
|
||||
FairMQDevice::Shutdown();
|
||||
|
||||
boost::lock_guard<boost::mutex> lock(fRunningMutex);
|
||||
fRunningFinished = true;
|
||||
fRunningCondition.notify_one();
|
||||
}
|
||||
|
||||
FairMQExampleServer::~FairMQExampleServer()
|
||||
|
@@ -32,12 +32,11 @@ FairMQExampleClient client;
|
||||
|
||||
static void s_signal_handler(int signal)
|
||||
{
|
||||
cout << endl << "Caught signal " << signal << endl;
|
||||
LOG(INFO) << "Caught signal " << signal;
|
||||
|
||||
client.ChangeState(FairMQExampleClient::STOP);
|
||||
client.ChangeState(FairMQExampleClient::END);
|
||||
|
||||
cout << "Shutdown complete. Bye!" << endl;
|
||||
LOG(INFO) << "Shutdown complete.";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -115,33 +114,31 @@ int main(int argc, char** argv)
|
||||
|
||||
client.SetProperty(FairMQExampleClient::Id, "client");
|
||||
client.SetProperty(FairMQExampleClient::NumIoThreads, 1);
|
||||
client.SetProperty(FairMQExampleClient::NumInputs, 0);
|
||||
client.SetProperty(FairMQExampleClient::NumOutputs, 1);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::INIT);
|
||||
FairMQChannel requestChannel("req", "connect", "tcp://localhost:5005");
|
||||
requestChannel.fSndBufSize = 10000;
|
||||
requestChannel.fRcvBufSize = 10000;
|
||||
requestChannel.fRateLogging = 1;
|
||||
|
||||
client.SetProperty(FairMQExampleClient::OutputSocketType, "req", 0);
|
||||
client.SetProperty(FairMQExampleClient::OutputSndBufSize, 10000, 0);
|
||||
client.SetProperty(FairMQExampleClient::OutputRcvBufSize, 10000, 0);
|
||||
client.SetProperty(FairMQExampleClient::OutputMethod, "connect", 0);
|
||||
client.SetProperty(FairMQExampleClient::OutputAddress, "tcp://localhost:5005", 0);
|
||||
client.fChannels["data"].push_back(requestChannel);
|
||||
|
||||
client.SetProperty(FairMQExampleClient::Text, options.text);
|
||||
client.ChangeState(FairMQExampleClient::INIT_DEVICE);
|
||||
client.WaitForEndOfState(FairMQExampleClient::INIT_DEVICE);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::INIT_TASK);
|
||||
client.WaitForEndOfState(FairMQExampleClient::INIT_TASK);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::SETOUTPUT);
|
||||
client.ChangeState(FairMQExampleClient::SETINPUT);
|
||||
client.ChangeState(FairMQExampleClient::BIND);
|
||||
client.ChangeState(FairMQExampleClient::CONNECT);
|
||||
client.ChangeState(FairMQExampleClient::RUN);
|
||||
|
||||
// wait until the running thread has finished processing.
|
||||
boost::unique_lock<boost::mutex> lock(client.fRunningMutex);
|
||||
while (!client.fRunningFinished)
|
||||
{
|
||||
client.fRunningCondition.wait(lock);
|
||||
}
|
||||
client.WaitForEndOfState(FairMQExampleClient::RUN);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::STOP);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::RESET_TASK);
|
||||
client.WaitForEndOfState(FairMQExampleClient::RESET_TASK);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::RESET_DEVICE);
|
||||
client.WaitForEndOfState(FairMQExampleClient::RESET_DEVICE);
|
||||
|
||||
client.ChangeState(FairMQExampleClient::END);
|
||||
|
||||
return 0;
|
||||
|
@@ -30,12 +30,11 @@ FairMQExampleServer server;
|
||||
|
||||
static void s_signal_handler(int signal)
|
||||
{
|
||||
cout << endl << "Caught signal " << signal << endl;
|
||||
LOG(INFO) << "Caught signal " << signal;
|
||||
|
||||
server.ChangeState(FairMQExampleServer::STOP);
|
||||
server.ChangeState(FairMQExampleServer::END);
|
||||
|
||||
cout << "Shutdown complete. Bye!" << endl;
|
||||
LOG(INFO) << "Caught signal " << signal;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -65,34 +64,31 @@ int main(int argc, char** argv)
|
||||
|
||||
server.SetProperty(FairMQExampleServer::Id, "server");
|
||||
server.SetProperty(FairMQExampleServer::NumIoThreads, 1);
|
||||
server.SetProperty(FairMQExampleServer::NumInputs, 1);
|
||||
server.SetProperty(FairMQExampleServer::NumOutputs, 0);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::INIT);
|
||||
FairMQChannel replyChannel("rep", "bind", "tcp://*:5005");
|
||||
replyChannel.fSndBufSize = 10000;
|
||||
replyChannel.fRcvBufSize = 10000;
|
||||
replyChannel.fRateLogging = 1;
|
||||
|
||||
server.SetProperty(FairMQExampleServer::InputSocketType, "rep", 0);
|
||||
server.SetProperty(FairMQExampleServer::InputSndBufSize, 10000, 0);
|
||||
server.SetProperty(FairMQExampleServer::InputRcvBufSize, 10000, 0);
|
||||
server.SetProperty(FairMQExampleServer::InputMethod, "bind", 0);
|
||||
server.SetProperty(FairMQExampleServer::InputAddress, "tcp://*:5005", 0);
|
||||
server.fChannels["data"].push_back(replyChannel);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::SETOUTPUT);
|
||||
server.ChangeState(FairMQExampleServer::SETINPUT);
|
||||
server.ChangeState(FairMQExampleServer::BIND);
|
||||
server.ChangeState(FairMQExampleServer::CONNECT);
|
||||
server.ChangeState(FairMQExampleServer::INIT_DEVICE);
|
||||
server.WaitForEndOfState(FairMQExampleServer::INIT_DEVICE);
|
||||
|
||||
LOG(INFO) << "Listening for requests!";
|
||||
server.ChangeState(FairMQExampleServer::INIT_TASK);
|
||||
server.WaitForEndOfState(FairMQExampleServer::INIT_TASK);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::RUN);
|
||||
|
||||
// wait until the running thread has finished processing.
|
||||
boost::unique_lock<boost::mutex> lock(server.fRunningMutex);
|
||||
while (!server.fRunningFinished)
|
||||
{
|
||||
server.fRunningCondition.wait(lock);
|
||||
}
|
||||
server.WaitForEndOfState(FairMQExampleServer::RUN);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::STOP);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::RESET_TASK);
|
||||
server.WaitForEndOfState(FairMQExampleServer::RESET_TASK);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::RESET_DEVICE);
|
||||
server.WaitForEndOfState(FairMQExampleServer::RESET_DEVICE);
|
||||
|
||||
server.ChangeState(FairMQExampleServer::END);
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user