Skip to content
Snippets Groups Projects
Commit ef33b587 authored by Maxime Graulich's avatar Maxime Graulich
Browse files

ONELAB2: MergePostProcessingFile when GUI and server are local

parent ac6fcda1
Branches
Tags
No related merge requests found
......@@ -70,9 +70,13 @@ void connect_cb(Fl_Widget *w, void *arg)
if(!obj->useServer()) return;
obj->clearTree();
GmshNetworkClient *cli = OnelabDatabase::instance()->useAsNetworkClient(obj->getServerIP(), obj->getServerPort());
if(cli) {
cli->setCallback(obj);
w->label("Disconnect");
}
else
fl_alert("Unable to connect to server");
}
else {
obj->clearTree();
OnelabDatabase::instance()->useAsClient()->setCallback(obj);
......@@ -99,39 +103,17 @@ void onelab_cb(Fl_Widget *w, void *data)
return;
}
if(action == "stop"){
// TODO
return;
}
if(action == "kill"){
// TODO
return;
}
if(FlGui::instance()->onelab->isBusy()){
Msg::Info("I'm busy! Ask me that later...");
return;
}
Msg::Info("Try to %s", action.c_str());
if(action == "reset"){
//TODO resetDb(true);
action = "check";
}
Msg::ResetErrorCounter();
//TODO FlGui::instance()->onelab->setButtonMode("", "stop");
if(action == "compute") initializeLoops();
do{
//OnelabDatabase::instance()->run("Gmsh", action);
OnelabDatabase::instance()->run(action);
} while(action == "compute"
//&& !FlGui::instance()->onelab->stop()
&& incrementLoops());
}
......@@ -142,10 +124,12 @@ void solver_cb(Fl_Widget *w, void *data)
int num = (intptr_t)data;
if(num >= 0){
std::string name = opt_solver_name(num, GMSH_GET, "");
if(name.empty()) return;// TODO
std::string exe = opt_solver_executable(num, GMSH_GET, "");
std::string host = opt_solver_remote_login(num, GMSH_GET, "");
OnelabDatabase::instance()->run("initialize", name);
}
if(FlGui::instance()->onelab->isBusy())
FlGui::instance()->onelab->show();
else{
......@@ -164,6 +148,16 @@ void solver_cb(Fl_Widget *w, void *data)
}
void solver_batch_cb(Fl_Widget *w, void *data)
{
int num = (intptr_t)data;
if(num >= 0) {
std::string name = opt_solver_name(num, GMSH_GET, "");
std::string exe = opt_solver_executable(num, GMSH_GET, "");
if(exe.empty()){
Msg::Error("Solver executable name not provided");
return;
}
}
Msg::Warning("solver_batch_cb TODO");
// TODO
}
......
......@@ -3,6 +3,8 @@
#ifdef HAVE_FLTK
#include "GmshMessage.h"
#include "onelab2Group.h"
#include "OpenFile.h"
#include "Context.h"
void GmshLocalClient::setCallback(onelabGroup *cb) {_cb_obj = cb;}
void GmshLocalClient::onNewParameter(onelab::parameter *p)
......@@ -49,6 +51,13 @@ void GmshLocalClient::onMessage(const std::string & name, const std::string &mes
Fl::unlock();
Fl::awake((void *)NULL);
}
void GmshLocalClient::mergeFile(const std::string &filename)
{
Fl::lock();
MergePostProcessingFile(filename, CTX::instance()->solver.autoShowViews, CTX::instance()->solver.autoShowLastStep, true);
Fl::unlock();
Fl::awake((void *)NULL);
}
#endif
void GmshLocalClient::run(std::string action) {
......
......@@ -25,6 +25,7 @@ public:
void onUpdateParameter(onelab::parameter *p);
void onRemoveParameter(onelab::parameter *p);
void onMessage(const std::string &name, const std::string &message, int level);
void mergeFile(const std::string &filename);
#else
GmshLocalClient(std::string name, onelab::parameterSpace *parameterSpace)
: OnelabLocalClient(name, parameterSpace){}
......
......@@ -89,7 +89,7 @@ int ip4_socket_send(Socket fd, UInt8 *src, int length)
ssize_t sent = send(fd, src, length, 0);
// TODO handle error (length != sent) for ??? and (sent < 0) for local error
std::cout << "ip: send " << sent << "/" << length << "bytes" << std::endl;
//std::cout << "ip: send " << sent << "/" << length << "bytes" << std::endl;
return (int)sent;
}
int ip4_socket_send(Socket fd, UInt8 *src, int length, IPv4 dst)
......@@ -105,7 +105,7 @@ int ip4_socket_send(Socket fd, UInt8 *src, int length, IPv4 dst)
ssize_t sent = sendto(fd, src, length, 0, (struct sockaddr *)&to, tol);
// TODO handle error (length != sent) for ??? and (sent < 0) for local error
std::cout << "ip: send " << sent << "/" << length << "bytes to " << ip4_inet_ntop(dst.address)<< ':' << dst.port << std::endl;
//std::cout << "ip: send " << sent << "/" << length << "bytes to " << ip4_inet_ntop(dst.address)<< ':' << dst.port << std::endl;
return (int)sent;
}
......@@ -114,7 +114,7 @@ int ip4_socket_recv(Socket fd, UInt8 *dst, int maxlength)
ssize_t recved = recv(fd, dst, maxlength, 0);
// TODO handle error if(recvlength < 0)
std::cout << "ip: recv " << recved << "bytes" << std::endl;
//std::cout << "ip: recv " << recved << "bytes" << std::endl;
return recved;
}
int ip4_socket_recv(Socket fd, UInt8 *dst, int maxlength, IPv4 &src)
......@@ -128,7 +128,7 @@ int ip4_socket_recv(Socket fd, UInt8 *dst, int maxlength, IPv4 &src)
src.address = ntoh32(from.sin_addr.s_addr);
// TODO handle error if(recvlength < 0)
std::cout << "ip: recv " << recvlength << "bytes from " << ip4_inet_ntop(src.address)<< ':' << src.port << std::endl;
//std::cout << "ip: recv " << recvlength << "bytes from " << ip4_inet_ntop(src.address)<< ':' << src.port << std::endl;
return recvlength;
}
......
......@@ -141,6 +141,38 @@ void OnelabAttrFileQuery::showAttribute() const
}
UInt8 *OnelabAttrMergeFile::encodeAttribute(UInt8 *dst)
{
dst = encode(dst, getAttributeType());
dst = encode(dst, getAttributeLength());
dst = encode(dst, (UInt8 *)_name, _length);
return dst;
}
UInt8 *OnelabAttrMergeFile::parseAttribute(UInt8 *src, UInt32 length)
{
if(_name != NULL) free(_name);
_length = length;
_name = (char *)malloc(_length+1);
src = parse(src, (UInt8 *)_name, _length);
_name[_length] = '\0';
return src;
}
void OnelabAttrMergeFile::setFilename(const std::string name)
{
if(_name != NULL) free(_name);
_length = name.size();
_name = strndup(name.c_str(), _length+1);
}
void OnelabAttrMergeFile::showAttribute() const
{
std::cout << "\033[1m" << "Attribute merge file:"<< getAttributeType() << "\033[0m"<< std::endl
<< "file: " << _name << std::endl;
}
UInt8 *OnelabAttrFile::encodeAttribute(UInt8 *dst)
{
dst = encode(dst, getAttributeType());
......
......@@ -177,6 +177,28 @@ public:
const char *getFilename() {return _name;}
};
class OnelabAttrMergeFile : public OnelabAttr
{
private:
char *_name;
UInt16 _length;
public:
OnelabAttrMergeFile() : _name(NULL), _length(0){}
OnelabAttrMergeFile(const std::string filename) : _name(NULL), _length(0) {setFilename(filename);}
~OnelabAttrMergeFile() {if(_name != NULL) free(_name);}
UInt8 *encodeAttribute(UInt8 *dst);
UInt8 *parseAttribute(UInt8 *src, UInt32 length);
void showAttribute() const;
static UInt16 attributeType() {return 0x0D;}
inline UInt16 getAttributeType() const {return attributeType();}
inline UInt16 getAttributeLength() const {return _length;}
void setFilename(std::string name);
const char *getFilename() {return _name;}
};
class OnelabAttrFile : public OnelabAttr
{
private:
......
......@@ -182,16 +182,21 @@ public:
return true;
}
}
else {
// run Gmsh client
run(action, "Gmsh");
else { // run all client
run(action, "Gmsh"); // run Gmsh client
// iterate over all other clients
if(CTX::instance()->solverToRun >= 0) {
if(CTX::instance()->solverToRun >= 0) { // launch the solver
std::string solver = opt_solver_name(CTX::instance()->solverToRun, GMSH_GET, "");
std::string exe = opt_solver_executable(CTX::instance()->solverToRun, GMSH_GET, "");
if(_client && exe.size()) {
onelab::string o(solver + "/CommandLine", exe);
o.setVisible(false);
o.setNeverChanged(true);
set(o, solver);
}
run(action, solver);
}
else {
else { // send action to all connected client except Gmsh
if(_client) {
std::cout << "server is remote" << std::endl;
msg.attrs.push_back(new OnelabAttrAction(action, client));
......
......@@ -22,6 +22,7 @@ public:
virtual void onUpdateParameter(onelab::parameter *p){}
virtual void onRemoveParameter(onelab::parameter *p){}
virtual void onMessage(const std::string &name, const std::string &message, int level){}
virtual void mergeFile(const std::string &filename){}
virtual void run(std::string action) {}
void stop() {}
......
......@@ -140,7 +140,8 @@ public:
}
virtual void onNewParameter(onelab::parameter *){}
virtual void onUpdateParameter(onelab::parameter *){}
virtual void onRemoveParameter(onelab::parameter *){} // TODO call on clear
virtual void onRemoveParameter(onelab::parameter *){}
void onMessage(const std::string &name, const std::string &message, int level) {}
// network specific method
bool connect();
bool isConnected(){return _connected;}
......@@ -168,7 +169,15 @@ public:
{
sendMessage(OnelabAttrMessage::Error, msg);
}
void mergeFile(const std::string &filename)
{
OnelabProtocol msg(OnelabProtocol::OnelabUpdate);
UInt8 buff[1024];
msg.attrs.push_back(new OnelabAttrMergeFile(filename));
int recvlen = msg.encodeMsg(buff, 1024);
std::cout <<recvlen << std::endl;
sendto(buff, recvlen);
}
};
#endif
......@@ -27,10 +27,8 @@ unsigned short OnelabProtocol::encodeMsg(UInt8 *buff, UInt32 len)
for (std::vector<OnelabAttr*>::iterator it = this->attrs.begin() ; it != this->attrs.end(); ++it) {
UInt16 attrLen = (*it)->getAttributeLength();
if(4+_size+attrLen > len) {
// FIXME
encode(sizeptr, _size);
return (unsigned short)(ptr-buff);
// size = 0;
}
ptr = (*it)->encodeAttribute(ptr);
_size+=attrLen+4;
......@@ -134,6 +132,10 @@ UInt32 OnelabProtocol::parseMessage(UInt8 *buff, UInt32 len)
case 0x0c:
this->attrs.push_back(new OnelabAttrFile());
((OnelabAttrFile *)this->attrs.back())->parseAttribute(ptr, attrSize);
break;
case 0x0d:
this->attrs.push_back(new OnelabAttrMergeFile());
((OnelabAttrMergeFile *)this->attrs.back())->parseAttribute(ptr, attrSize);
break;
default:
// FIXME unknown attribute
......
......@@ -63,13 +63,18 @@ OnelabLocalNetworkClient *OnelabServer::getClient(UDTSOCKET fd) // UDTSOCKET Soc
}
#endif
int OnelabServer::launchClient(const std::string &client) // FIXME OnelabDatabase instead of OnelabServer ?
int OnelabServer::launchClient(const std::string &client, bool blocking) // FIXME OnelabDatabase instead of OnelabServer ?
{
// launch a new client with a system call
std::string command = "";
if(getClient(client) != NULL || getLocalClient(client) != NULL) return -1; // client already exist
if(client == "Gmsh") {
std::vector<onelab::string> s;
get(s, client + "/CommandLine");
if(s.size()) {
command.assign(s[0].getValue());
}
else if(client == "Gmsh") {
command.assign(Msg::GetExecutableName());
}
else {
......@@ -106,7 +111,7 @@ int OnelabServer::launchClient(const std::string &client) // FIXME OnelabDatabas
sprintf(cmd, command.c_str(), (_ip.address==0)?"127.0.0.1":ip4_inet_ntop(_ip.address).c_str(), _ip.port);
std::cout << "launch " << client << " with command: " << cmd << std::endl;
SystemCall(cmd);
SystemCall(cmd, blocking);
return 0;
}
......@@ -177,7 +182,7 @@ void OnelabServer::waitForClient(const std::string &name)
fd_set errorfds;
FD_ZERO(&errorfds);
FD_SET(cli->getSSocket(), &errorfds);
select(cli->getSSocket()+1, NULL, NULL, &errorfds, NULL); // Wait for the server to answer
select(cli->getSSocket()+1, NULL, NULL, &errorfds, NULL);
// TODO wait until the client close
std::cout << "======= cli " << cli->getName() << "just ended ?" << std::cout;
}
......@@ -360,8 +365,13 @@ void *listenOnClients(void *param)
break;
case OnelabProtocol::OnelabMessage:
if(msg.attrs.size()==1 && msg.attrs[0]->getAttributeType() == OnelabAttrMessage::attributeType()) {
OnelabLocalClient *gui = OnelabServer::instance()->getLocalClient("localGUI");
if(gui) gui->onMessage(cli->getName(), ((OnelabAttrMessage *)msg.attrs[0])->getMessage(), ((OnelabAttrMessage *)msg.attrs[0])->getLevel());
OnelabLocalClient *localgui = OnelabServer::instance()->getLocalClient("localGUI");
OnelabLocalNetworkClient *gui = OnelabServer::instance()->getClient("GUI");
if(gui) {
recvlen = msg.encodeMsg(buff, 1024);
gui->sendto(buff, recvlen);
}
if(localgui) localgui->onMessage(cli->getName(), ((OnelabAttrMessage *)msg.attrs[0])->getMessage(), ((OnelabAttrMessage *)msg.attrs[0])->getLevel());
}
break;
case OnelabProtocol::OnelabRequest:
......@@ -442,6 +452,7 @@ void *listenOnClients(void *param)
break;
}
case 0x0C:
{
const char *filename = ((OnelabAttrFile *)*it)->getFilename();
std::clog << "try to open " << filename << " to write" << std::endl;
FILE *fp = fopen(filename, "wb");
......@@ -457,6 +468,13 @@ void *listenOnClients(void *param)
std::clog << "file ok" << std::endl;
break;
}
case 0x0D:
{
// merge file only if the GUI and the server are local
OnelabLocalClient *gui = OnelabServer::instance()->getLocalClient("localGUI");
if(gui) gui->mergeFile(((OnelabAttrMergeFile *)*it)->getFilename());
}
}
}
break;
case OnelabProtocol::OnelabAction:
......@@ -464,17 +482,6 @@ void *listenOnClients(void *param)
if(msg.attrs.size()==1 && msg.attrs[0]->getAttributeType() == OnelabAttrAction::attributeType()) {
std::clog << "\033[0;31m" << "Client " << cli->getName() << " ask " << ((OnelabAttrAction *)msg.attrs[0])->getClient() << " to " << ((OnelabAttrAction *)msg.attrs[0])->getAction() << "\033[0m" << std::endl;
OnelabServer::instance()->performAction(((OnelabAttrAction *)msg.attrs[0])->getAction(), ((OnelabAttrAction *)msg.attrs[0])->getClient());
//OnelabLocalNetworkClient *cli = OnelabServer::instance()->getClient(((OnelabAttrAction *)msg.attrs[0])->getClient());
//OnelabLocalClient *localcli = OnelabServer::instance()->getLocalClient(((OnelabAttrAction *)msg.attrs[0])->getClient());
//std::cout << ((OnelabAttrAction *)msg.attrs[0])->getAction() << " on " << ((OnelabAttrAction *)msg.attrs[0])->getClient() << "(" << cli << " or local " << localcli << ")" << std::endl;
//if(cli == NULL && localcli == NULL) {
//}
//if(cli != NULL)
// cli->run(((OnelabAttrAction *)msg.attrs[0])->getAction());
//else if(localcli != NULL)
// localcli->run(((OnelabAttrAction *)msg.attrs[0])->getAction());
//else
// ;// TODO save action and wait for the cli ?
}
}
break;
......
......@@ -58,7 +58,7 @@ public:
void addClient(std::string name, UInt32 ip, UInt16 port);
#endif
void addClient(OnelabLocalClient *cli) {_localClients.push_back(cli);}
int launchClient(const std::string &);
int launchClient(const std::string &, bool blocking=false);
void removeClient(OnelabLocalNetworkClient *client);
std::vector<OnelabLocalNetworkClient> &getClients() {return _clients;}
std::vector<OnelabLocalClient *> &getLocalClients() {return _localClients;}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment