mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 21:37:13 +02:00
merging refactor (replacing)
This commit is contained in:
311
slsDetectorGui/client/qClient.cpp
Normal file → Executable file
311
slsDetectorGui/client/qClient.cpp
Normal file → Executable file
@ -1,237 +1,124 @@
|
||||
/*
|
||||
* qClient.cpp
|
||||
*
|
||||
* Created on: Feb 27, 2013
|
||||
* Author: Dhanya Maliakal
|
||||
*/
|
||||
// Qt Project Class Headers
|
||||
#include "qClient.h"
|
||||
// Project Class Headers
|
||||
#include "MySocketTCP.h"
|
||||
#include "slsDetectorBase.h"
|
||||
|
||||
// C++ Include Headers
|
||||
#include "ClientSocket.h"
|
||||
#include "logger.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
qClient* cl = 0;
|
||||
try {
|
||||
cl = new qClient(argv[1]);
|
||||
} catch(...) {
|
||||
return 0;
|
||||
}
|
||||
cl->executeLine(argc-2, argv+2);
|
||||
delete cl;
|
||||
|
||||
return 0;
|
||||
int main(int argc, char *argv[]) {
|
||||
qClient *cl = 0;
|
||||
cl = new qClient(argv[1]);
|
||||
cl->executeLine(argc - 2, argv + 2);
|
||||
delete cl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
qClient::qClient(char* hostname):
|
||||
mySocket(0),
|
||||
myStopSocket(0){
|
||||
|
||||
try {
|
||||
// control socket
|
||||
mySocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO);
|
||||
// stop socket
|
||||
myStopSocket = new MySocketTCP(hostname, DEFAULT_GUI_PORTNO+1);
|
||||
} catch(...) {
|
||||
if (mySocket == 0)
|
||||
cout << "Error: could not connect to control server:" <<
|
||||
hostname << " with port " << DEFAULT_GUI_PORTNO << endl;
|
||||
else
|
||||
cout << "Error: could not connect to stop server:" <<
|
||||
hostname << " with port " << DEFAULT_GUI_PORTNO + 1 << endl;
|
||||
throw;
|
||||
}
|
||||
qClient::qClient(char *h)
|
||||
: controlPort(DEFAULT_GUI_PORTNO), stopPort(DEFAULT_GUI_PORTNO + 1) {
|
||||
hostname.assign(h);
|
||||
}
|
||||
|
||||
qClient::~qClient() {}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
void qClient::executeLine(int narg, char *args[]) {
|
||||
|
||||
qClient::~qClient() {
|
||||
if(mySocket) delete mySocket;
|
||||
if(myStopSocket) delete myStopSocket;
|
||||
std::string retval = "";
|
||||
std::string cmd = args[0];
|
||||
|
||||
// validate command structure
|
||||
if (narg < 1) {
|
||||
throw sls::RuntimeError("No command parsed. " + printCommands());
|
||||
}
|
||||
|
||||
// help
|
||||
if (cmd == "help") {
|
||||
retval = printCommands();
|
||||
}
|
||||
|
||||
// file name
|
||||
else if (cmd == "status") {
|
||||
|
||||
if (narg > 1) {
|
||||
std::string argument = args[1];
|
||||
// start acquisition
|
||||
if (argument == "start")
|
||||
startAcquisition();
|
||||
else if (argument == "stop")
|
||||
stopAcquisition();
|
||||
else {
|
||||
throw sls::RuntimeError("Could not parse arguments. " +
|
||||
printCommands());
|
||||
}
|
||||
}
|
||||
retval = getStatus();
|
||||
}
|
||||
|
||||
else if (cmd == "acquire") {
|
||||
startAcquisition(true);
|
||||
retval = getStatus();
|
||||
}
|
||||
|
||||
else if (cmd == "exit") {
|
||||
exitServer();
|
||||
retval.assign("Server exited successfully");
|
||||
}
|
||||
|
||||
// unrecognized command
|
||||
else {
|
||||
throw sls::RuntimeError("Unrecognized command. " + printCommands());
|
||||
}
|
||||
|
||||
// print result
|
||||
FILE_LOG(logINFO) << cmd << ": " << retval;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
int qClient::executeLine(int narg, char *args[]){
|
||||
|
||||
string retval = "";
|
||||
string cmd = args[0];
|
||||
string argument;
|
||||
|
||||
|
||||
//validate command structure
|
||||
if(narg<1){
|
||||
cout << "Error: no command parsed" << endl;
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
|
||||
//help
|
||||
if (cmd == "help"){
|
||||
retval = printCommands();
|
||||
}
|
||||
|
||||
//file name
|
||||
else if (cmd == "status"){
|
||||
|
||||
if(narg>1){
|
||||
argument = args[1];
|
||||
//start acquisition
|
||||
if(argument == "start")
|
||||
startAcquisition();
|
||||
else if (argument == "stop")
|
||||
stopAcquisition();
|
||||
else{
|
||||
cprintf(RED,"Error: could not parse arguments: %s\n", argument.c_str());
|
||||
printCommands();
|
||||
return FAIL;
|
||||
}
|
||||
}
|
||||
retval = getStatus();
|
||||
}
|
||||
|
||||
|
||||
else if (cmd == "acquire"){
|
||||
startAcquisition(true);
|
||||
retval = getStatus();
|
||||
}
|
||||
|
||||
|
||||
else if (cmd == "exit"){
|
||||
return exitServer();
|
||||
}
|
||||
|
||||
|
||||
//unrecognized command
|
||||
else{
|
||||
cout << "Error: unrecognized command" << endl;
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
|
||||
//print result
|
||||
cout << cmd << ": " << retval << endl;
|
||||
|
||||
return OK;
|
||||
std::string qClient::printCommands() {
|
||||
std::ostringstream os;
|
||||
os << "\nexit \t exits servers in gui" << std::endl;
|
||||
os << "status \t gets status of acquisition in gui. - can be running or "
|
||||
"idle"
|
||||
<< std::endl;
|
||||
os << "status i starts/stops acquistion in gui-non blocking. i is start "
|
||||
"or stop"
|
||||
<< std::endl;
|
||||
os << "acquire starts acquistion in gui-blocking" << std::endl;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
std::string qClient::getStatus() {
|
||||
int fnum = qDefs::F_GUI_GET_RUN_STATUS;
|
||||
int retvals[2] = {static_cast<int>(ERROR), 0};
|
||||
|
||||
auto client = sls::GuiSocket(hostname, controlPort);
|
||||
client.sendCommandThenRead(fnum, nullptr, 0, retvals, sizeof(retvals));
|
||||
|
||||
string qClient::printCommands(){
|
||||
ostringstream os;
|
||||
os << "\nexit \t exits servers in gui" << std::endl;
|
||||
os << "status \t gets status of acquisition in gui. - can be running or idle" << std::endl;
|
||||
os << "status i starts/stops acquistion in gui-non blocking. i is start or stop" << std::endl;
|
||||
os << "acquire starts acquistion in gui-blocking" << std::endl;
|
||||
return os.str();
|
||||
runStatus status = static_cast<runStatus>(retvals[0]);
|
||||
int progress = retvals[1];
|
||||
return std::to_string(progress) + std::string("% ") +
|
||||
slsDetectorDefs::runStatusType(status);
|
||||
}
|
||||
|
||||
void qClient::startAcquisition(bool blocking) {
|
||||
int fnum = qDefs::F_GUI_START_ACQUISITION;
|
||||
if (blocking)
|
||||
fnum = qDefs::F_GUI_START_AND_READ_ALL;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
string qClient::getStatus(){
|
||||
int fnum = F_GET_RUN_STATUS;
|
||||
int ret = FAIL;
|
||||
runStatus retval=ERROR;
|
||||
int progress = 0;
|
||||
char answer[100];
|
||||
|
||||
if (myStopSocket->Connect() >= 0) {
|
||||
myStopSocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
myStopSocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
myStopSocket->ReceiveDataOnly(&retval,sizeof(retval));
|
||||
myStopSocket->ReceiveDataOnly(&progress,sizeof(progress));
|
||||
myStopSocket->Disconnect();
|
||||
}else
|
||||
exit(-1);
|
||||
|
||||
|
||||
sprintf(answer,"%d%% ",progress);
|
||||
strcat(answer,slsDetectorBase::runStatusType((runStatus)retval).c_str());
|
||||
|
||||
return string(answer);
|
||||
auto client = sls::GuiSocket(hostname.c_str(), controlPort);
|
||||
client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0);
|
||||
}
|
||||
|
||||
void qClient::stopAcquisition() {
|
||||
int fnum = qDefs::F_GUI_STOP_ACQUISITION;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
int qClient::startAcquisition(bool blocking){
|
||||
int fnum = F_START_ACQUISITION;
|
||||
if(blocking) fnum = F_START_AND_READ_ALL;
|
||||
int ret = FAIL;
|
||||
|
||||
if (mySocket->Connect() >= 0) {
|
||||
mySocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
mySocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
if (ret == FAIL){
|
||||
mySocket->ReceiveDataOnly(mess,sizeof(mess));
|
||||
std::cout<< "Gui returned error: " << mess << std::endl;
|
||||
}
|
||||
mySocket->Disconnect();
|
||||
}else
|
||||
exit(-1);
|
||||
|
||||
return ret;
|
||||
auto client = sls::GuiSocket(hostname, stopPort);
|
||||
client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
int qClient::stopAcquisition(){
|
||||
int fnum = F_STOP_ACQUISITION;
|
||||
int ret = FAIL;
|
||||
|
||||
if (myStopSocket->Connect() >= 0) {
|
||||
myStopSocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
myStopSocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
if (ret == FAIL){
|
||||
myStopSocket->ReceiveDataOnly(mess,sizeof(mess));
|
||||
std::cout<< "Gui returned error: " << mess << std::endl;
|
||||
}
|
||||
myStopSocket->Disconnect();
|
||||
}else
|
||||
exit(-1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
int qClient::exitServer(){
|
||||
int fnum = F_EXIT_SERVER;
|
||||
int ret = FAIL;
|
||||
|
||||
if (myStopSocket->Connect() >= 0) {
|
||||
myStopSocket->SendDataOnly(&fnum,sizeof(fnum));
|
||||
myStopSocket->ReceiveDataOnly(&ret,sizeof(ret));
|
||||
myStopSocket->ReceiveDataOnly(mess,sizeof(mess));
|
||||
cout << mess << endl;
|
||||
myStopSocket->Disconnect();
|
||||
}else
|
||||
exit(-1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
void qClient::exitServer() {
|
||||
int fnum = qDefs::F_GUI_EXIT_SERVER;
|
||||
// closes both control and stop server
|
||||
auto client = sls::GuiSocket(hostname, controlPort);
|
||||
client.sendCommandThenRead(fnum, nullptr, 0, nullptr, 0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user