Skip to content
Snippets Groups Projects
Commit 62f58215 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

onelab core communication ok

parent 62a2e350
No related branches found
No related tags found
No related merge requests found
...@@ -61,22 +61,23 @@ class GmshSocket{ ...@@ -61,22 +61,23 @@ class GmshSocket{
// receive data from a machine with a different byte ordering, and // receive data from a machine with a different byte ordering, and
// we swap the bytes in the payload) // we swap the bytes in the payload)
enum MessageType{ enum MessageType{
GMSH_START = 1, GMSH_START = 1,
GMSH_STOP = 2, GMSH_STOP = 2,
GMSH_INFO = 10, GMSH_INFO = 10,
GMSH_WARNING = 11, GMSH_WARNING = 11,
GMSH_ERROR = 12, GMSH_ERROR = 12,
GMSH_PROGRESS = 13, GMSH_PROGRESS = 13,
GMSH_MERGE_FILE = 20, GMSH_MERGE_FILE = 20,
GMSH_PARSE_STRING = 21, GMSH_PARSE_STRING = 21,
GMSH_VERTEX_ARRAY = 22, GMSH_VERTEX_ARRAY = 22,
GMSH_ONELAB_PARAM = 23, GMSH_PARAMETER = 23,
GMSH_SPEED_TEST = 30, GMSH_PARAMETER_QUERY = 24,
GMSH_OPTION_1 = 100, GMSH_SPEED_TEST = 30,
GMSH_OPTION_2 = 101, GMSH_OPTION_1 = 100,
GMSH_OPTION_3 = 102, GMSH_OPTION_2 = 101,
GMSH_OPTION_4 = 103, GMSH_OPTION_3 = 102,
GMSH_OPTION_5 = 104}; GMSH_OPTION_4 = 103,
GMSH_OPTION_5 = 104};
protected: protected:
// the socket descriptor // the socket descriptor
int _sock; int _sock;
......
...@@ -35,8 +35,6 @@ ...@@ -35,8 +35,6 @@
namespace onelab{ namespace onelab{
typedef enum { NUMBER = 1, STRING = 2, REGION = 3, FUNCTION = 4 } parameterType;
// The base parameter class. // The base parameter class.
class parameter{ class parameter{
private: private:
...@@ -50,29 +48,24 @@ namespace onelab{ ...@@ -50,29 +48,24 @@ namespace onelab{
// client code(s) for which this parameter makes sense // client code(s) for which this parameter makes sense
std::set<std::string> _clients; std::set<std::string> _clients;
public: public:
parameter(const std::string &name, const std::string &shortHelp="", parameter(const std::string &name="", const std::string &shortHelp="",
const std::string &help="") const std::string &help="")
: _name(name), _shortHelp(shortHelp), _help(help){} : _name(name), _shortHelp(shortHelp), _help(help){}
void setShortHelp(std::string &shortHelp){ _shortHelp = shortHelp; } void setName(const std::string &name){ _name = name; }
void setHelp(std::string &help){ _help = help; } void setShortHelp(const std::string &shortHelp){ _shortHelp = shortHelp; }
void setHelp(const std::string &help){ _help = help; }
void setClients(std::set<std::string> &clients){ _clients = clients; } void setClients(std::set<std::string> &clients){ _clients = clients; }
void addClient(std::string &client){ _clients.insert(client); } void addClient(const std::string &client){ _clients.insert(client); }
void addClients(std::set<std::string> &clients) void addClients(std::set<std::string> &clients)
{ {
_clients.insert(clients.begin(), clients.end()); _clients.insert(clients.begin(), clients.end());
} }
virtual parameterType getType() const = 0; virtual std::string getType() const = 0;
std::string getTypeAsString()
{
std::ostringstream sstream;
sstream << getType();
return sstream.str();
}
const std::string &getName() const { return _name; } const std::string &getName() const { return _name; }
const std::string &getShortHelp() const { return _shortHelp; } const std::string &getShortHelp() const { return _shortHelp; }
const std::string &getHelp() const { return _help; } const std::string &getHelp() const { return _help; }
const std::set<std::string> &getClients() const { return _clients; } const std::set<std::string> &getClients() const { return _clients; }
char charSep(){ return '\0'; } static char charSep(){ return '|' /* '\0' */; }
std::string sanitize(const std::string &in) std::string sanitize(const std::string &in)
{ {
std::string out(in); std::string out(in);
...@@ -80,8 +73,28 @@ namespace onelab{ ...@@ -80,8 +73,28 @@ namespace onelab{
if(out[i] == charSep()) out[i] = ' '; if(out[i] == charSep()) out[i] = ' ';
return out; return out;
} }
virtual std::string toChar() = 0; virtual std::string toChar()
virtual void fromChar(const std::string &c){} {
std::ostringstream sstream;
sstream << getType() << charSep() << sanitize(getName()) << charSep()
<< sanitize(getShortHelp()) << charSep() << sanitize(getHelp());
return sstream.str();
}
virtual void fromChar(const std::string &msg){}
static std::string getNextToken(const std::string &msg, std::string::size_type &first)
{
std::string::size_type last = msg.find_first_of(charSep(), first);
std::string next = msg.substr(first, last - first);
first = (last == std::string::npos) ? last : last + 1;
return next;
}
static void getTypeAndNameFromChar(const std::string &msg, std::string &type,
std::string &name)
{
std::string::size_type first = 0;
type = getNextToken(msg, first);
name = getNextToken(msg, first);
}
}; };
class parameterLessThan{ class parameterLessThan{
...@@ -103,34 +116,24 @@ namespace onelab{ ...@@ -103,34 +116,24 @@ namespace onelab{
double _defaultValue, _min, _max, _step; double _defaultValue, _min, _max, _step;
std::vector<double> _choices; std::vector<double> _choices;
public: public:
number(const std::string &name) number(const std::string &name="", double defaultValue=0.,
: parameter(name), _value(0.), _defaultValue(0.),
_min(1.), _max(0.), _step(0.)
{
}
number(const std::string &name, double defaultValue,
const std::string &shortHelp="", const std::string &help="") const std::string &shortHelp="", const std::string &help="")
: parameter(name, shortHelp, help), _value(defaultValue), : parameter(name, shortHelp, help), _value(defaultValue),
_defaultValue(defaultValue), _min(1.), _max(0.), _step(0.) _defaultValue(defaultValue), _min(1.), _max(0.), _step(0.) {}
{
}
void setValue(double value){ _value = value; } void setValue(double value){ _value = value; }
void setMin(double min){ _min = min; } void setMin(double min){ _min = min; }
void setMax(double max){ _min = max; } void setMax(double max){ _min = max; }
void setStep(double step){ _step = step; } void setStep(double step){ _step = step; }
void setChoices(std::vector<double> &choices){ _choices = choices; } void setChoices(std::vector<double> &choices){ _choices = choices; }
parameterType getType() const { return NUMBER; } std::string getType() const { return "number"; }
double getValue() const { return _value; } double getValue() const { return _value; }
double getDefaultValue() const { return _defaultValue; } double getDefaultValue() const { return _defaultValue; }
std::string toChar() std::string toChar()
{ {
std::ostringstream sstream; std::ostringstream sstream;
sstream << getType() << charSep() << sanitize(getName()) << charSep() sstream << parameter::toChar() << charSep() << _value << charSep()
<< sanitize(getShortHelp()) << charSep() << _defaultValue << charSep() << _min << charSep() << _max
<< sanitize(getHelp()) << charSep() << _value << charSep() << charSep() << _step << charSep() << _choices.size() << charSep();
<< _defaultValue << charSep()
<< _min << charSep() << _max << charSep() << _step << charSep()
<< _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++) for(unsigned int i = 0; i < _choices.size(); i++)
sstream << _choices[i] << charSep(); sstream << _choices[i] << charSep();
sstream << getClients().size() << charSep(); sstream << getClients().size() << charSep();
...@@ -139,6 +142,15 @@ namespace onelab{ ...@@ -139,6 +142,15 @@ namespace onelab{
sstream << *it << charSep(); sstream << *it << charSep();
return sstream.str(); return sstream.str();
} }
void fromChar(const std::string &msg)
{
std::string::size_type pos = 0;
if(getNextToken(msg, pos) != getType()) return;
setName(getNextToken(msg, pos));
setShortHelp(getNextToken(msg, pos));
setHelp(getNextToken(msg, pos));
setValue(atof(getNextToken(msg, pos).c_str()));
}
}; };
// The string class. // The string class.
...@@ -148,29 +160,20 @@ namespace onelab{ ...@@ -148,29 +160,20 @@ namespace onelab{
std::string _defaultValue; std::string _defaultValue;
std::vector<std::string> _choices; std::vector<std::string> _choices;
public: public:
string(const std::string &name) string(const std::string &name="", const std::string &defaultValue="",
: parameter(name), _value(""), _defaultValue("")
{
}
string(const std::string &name, const std::string &defaultValue,
const std::string &shortHelp="", const std::string &help="") const std::string &shortHelp="", const std::string &help="")
: parameter(name, shortHelp, help), _value(defaultValue), : parameter(name, shortHelp, help), _value(defaultValue),
_defaultValue(defaultValue) _defaultValue(defaultValue) {}
{
}
void setValue(const std::string &value){ _value = value; } void setValue(const std::string &value){ _value = value; }
void setChoices(std::vector<std::string> &choices){ _choices = choices; } void setChoices(std::vector<std::string> &choices){ _choices = choices; }
parameterType getType() const { return STRING; } std::string getType() const { return "string"; }
const std::string &getValue() const { return _value; } const std::string &getValue() const { return _value; }
const std::string &getDefaultValue() const { return _defaultValue; } const std::string &getDefaultValue() const { return _defaultValue; }
std::string toChar() std::string toChar()
{ {
std::ostringstream sstream; std::ostringstream sstream;
sstream << getType() << charSep() << sanitize(getName()) << charSep() sstream << parameter::toChar() << charSep() << sanitize(_value) << charSep()
<< sanitize(getShortHelp()) << charSep() << sanitize(_defaultValue) << charSep() << _choices.size() << charSep();
<< sanitize(getHelp()) << charSep() << sanitize(_value) << charSep()
<< sanitize(_defaultValue) << charSep()
<< _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++) for(unsigned int i = 0; i < _choices.size(); i++)
sstream << sanitize(_choices[i]) << charSep(); sstream << sanitize(_choices[i]) << charSep();
sstream << getClients().size() << charSep(); sstream << getClients().size() << charSep();
...@@ -187,26 +190,20 @@ namespace onelab{ ...@@ -187,26 +190,20 @@ namespace onelab{
std::string _value, _defaultValue; std::string _value, _defaultValue;
std::vector<std::string> _choices; std::vector<std::string> _choices;
public: public:
region(const std::string &name) region(const std::string &name="", const std::string &defaultValue="",
: parameter(name), _value(""), _defaultValue("")
{
}
region(const std::string &name, const std::string &defaultValue,
const std::string &shortHelp="", const std::string &help="") const std::string &shortHelp="", const std::string &help="")
: parameter(name, shortHelp, help), _value(defaultValue), : parameter(name, shortHelp, help), _value(defaultValue),
_defaultValue(defaultValue) _defaultValue(defaultValue)
{ {
} }
parameterType getType() const { return REGION; } std::string getType() const { return "region"; }
const std::string &getValue() const { return _value; } const std::string &getValue() const { return _value; }
const std::string &getDefaultValue() const { return _defaultValue; } const std::string &getDefaultValue() const { return _defaultValue; }
std::string toChar() std::string toChar()
{ {
std::ostringstream sstream; std::ostringstream sstream;
sstream << getType() << charSep() << sanitize(getName()) << charSep() sstream << parameter::toChar() << charSep() << _value << charSep()
<< sanitize(getShortHelp()) << charSep() << sanitize(getHelp()) << _defaultValue << charSep() << _choices.size() << charSep();
<< charSep() << _value << charSep() << _defaultValue << charSep()
<< _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++) for(unsigned int i = 0; i < _choices.size(); i++)
sstream << _choices[i] << charSep(); sstream << _choices[i] << charSep();
sstream << getClients().size() << charSep(); sstream << getClients().size() << charSep();
...@@ -227,16 +224,10 @@ namespace onelab{ ...@@ -227,16 +224,10 @@ namespace onelab{
std::map<std::string, std::string> _pieceWiseValues; std::map<std::string, std::string> _pieceWiseValues;
std::vector<std::string> _choices; std::vector<std::string> _choices;
public: public:
function(const std::string &name) function(const std::string &name="", const std::string &defaultValue="",
: parameter(name), _value(""), _defaultValue("")
{
}
function(const std::string &name, const std::string &defaultValue,
const std::string &shortHelp="", const std::string &help="") const std::string &shortHelp="", const std::string &help="")
: parameter(name, shortHelp, help), _value(defaultValue), : parameter(name, shortHelp, help), _value(defaultValue),
_defaultValue(defaultValue) _defaultValue(defaultValue) {}
{
}
void setValue(const std::string &value, const std::string &region="") void setValue(const std::string &value, const std::string &region="")
{ {
if(region.empty()) if(region.empty())
...@@ -244,7 +235,7 @@ namespace onelab{ ...@@ -244,7 +235,7 @@ namespace onelab{
else else
_pieceWiseValues[region] = value; _pieceWiseValues[region] = value;
} }
parameterType getType() const { return FUNCTION; } std::string getType() const { return "function"; }
const std::string getValue(const std::string &region="") const const std::string getValue(const std::string &region="") const
{ {
if(region.size()){ if(region.size()){
...@@ -263,10 +254,8 @@ namespace onelab{ ...@@ -263,10 +254,8 @@ namespace onelab{
std::string toChar() std::string toChar()
{ {
std::ostringstream sstream; std::ostringstream sstream;
sstream << getType() << charSep() << sanitize(getName()) << charSep() sstream << parameter::toChar() << charSep() << sanitize(_value) << charSep()
<< sanitize(getShortHelp()) << charSep() << sanitize(_defaultValue) << charSep()
<< sanitize(getHelp()) << charSep() << sanitize(_value) << charSep()
<< sanitize(_defaultValue) << charSep()
<< _pieceWiseValues.size() << charSep(); << _pieceWiseValues.size() << charSep();
for(std::map<std::string, std::string>::const_iterator it = for(std::map<std::string, std::string>::const_iterator it =
_pieceWiseValues.begin(); it != _pieceWiseValues.end(); it++) _pieceWiseValues.begin(); it != _pieceWiseValues.end(); it++)
...@@ -292,7 +281,9 @@ namespace onelab{ ...@@ -292,7 +281,9 @@ namespace onelab{
std::set<region*, parameterLessThan> _regions; std::set<region*, parameterLessThan> _regions;
std::set<function*, parameterLessThan> _functions; std::set<function*, parameterLessThan> _functions;
// set a parameter in the parameter space; if it already exists, // set a parameter in the parameter space; if it already exists,
// use the new value but make sure to add new clients if necessary // use the new value but make sure to add new clients if
// necessary. This needs to be locked to avoid race conditions
// when several clients try to set a parameter at the same time
template <class T> bool _set(T &p, std::set<T*, parameterLessThan> &parameters) template <class T> bool _set(T &p, std::set<T*, parameterLessThan> &parameters)
{ {
std::set<std::string> clients; std::set<std::string> clients;
...@@ -363,6 +354,17 @@ namespace onelab{ ...@@ -363,6 +354,17 @@ namespace onelab{
{ {
return _get(p, name, _functions); return _get(p, name, _functions);
} }
void print()
{
for(std::set<number*, parameterLessThan>::iterator it = _numbers.begin();
it != _numbers.end(); it++) std::cout << (*it)->toChar() << std::endl;
for(std::set<string*, parameterLessThan>::iterator it = _strings.begin();
it != _strings.end(); it++) std::cout << (*it)->toChar() << std::endl;
for(std::set<region*, parameterLessThan>::iterator it = _regions.begin();
it != _regions.end(); it++) std::cout << (*it)->toChar() << std::endl;
for(std::set<function*, parameterLessThan>::iterator it = _functions.begin();
it != _functions.end(); it++) std::cout << (*it)->toChar() << std::endl;
}
}; };
// The onelab client: a class that communicates with the onelab // The onelab client: a class that communicates with the onelab
...@@ -401,8 +403,6 @@ namespace onelab{ ...@@ -401,8 +403,6 @@ namespace onelab{
} }
template <class T> bool set(T &p) template <class T> bool set(T &p)
{ {
// this needs to be locked to avoid race conditions when several
// clients try to set a parameter at the same time
return _parameterSpace.set(p); return _parameterSpace.set(p);
} }
template <class T> bool get(std::vector<T> &p, const std::string &name="") template <class T> bool get(std::vector<T> &p, const std::string &name="")
...@@ -420,6 +420,7 @@ namespace onelab{ ...@@ -420,6 +420,7 @@ namespace onelab{
citer lastClient(){ return _clients.end(); } citer lastClient(){ return _clients.end(); }
citer findClient(const std::string &name){ return _clients.find(name); } citer findClient(const std::string &name){ return _clients.find(name); }
int getNumClients(){ return _clients.size(); } int getNumClients(){ return _clients.size(); }
void print(){ _parameterSpace.print(); }
}; };
class localClient : public client{ class localClient : public client{
...@@ -445,6 +446,7 @@ namespace onelab{ ...@@ -445,6 +446,7 @@ namespace onelab{
_server->get(parameters, name); _server->get(parameters, name);
return true; return true;
} }
void print(){ _server->print(); }
}; };
class localNetworkClient : public localClient{ class localNetworkClient : public localClient{
...@@ -495,7 +497,7 @@ namespace onelab{ ...@@ -495,7 +497,7 @@ namespace onelab{
if(!_gmshClient) return false; if(!_gmshClient) return false;
parameter.addClient(_name); parameter.addClient(_name);
std::string msg = parameter.toChar(); std::string msg = parameter.toChar();
_gmshClient->SendMessage(GmshSocket::GMSH_ONELAB_PARAM, msg.size(), &msg[0]); _gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER, msg.size(), &msg[0]);
return true; return true;
} }
template <class T> bool get(std::vector<T> &parameters, template <class T> bool get(std::vector<T> &parameters,
...@@ -505,10 +507,10 @@ namespace onelab{ ...@@ -505,10 +507,10 @@ namespace onelab{
T parameter(name); T parameter(name);
parameter.addClient(_name); parameter.addClient(_name);
std::string msg = parameter.toChar(); std::string msg = parameter.toChar();
_gmshClient->SendMessage(GmshSocket::GMSH_ONELAB_PARAM, msg.size(), &msg[0]); _gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER_QUERY, msg.size(), &msg[0]);
while(1){ while(1){
// stop if we have no communications for 30 seconds // stop if we have no communications for 10 secs
int ret = _gmshClient->Select(30, 0); int ret = _gmshClient->Select(10, 0);
if(!ret){ if(!ret){
_gmshClient->Info("Timout: aborting remote get"); _gmshClient->Info("Timout: aborting remote get");
return false; return false;
...@@ -527,8 +529,10 @@ namespace onelab{ ...@@ -527,8 +529,10 @@ namespace onelab{
_gmshClient->Error("Did not receive message body: aborting remote get"); _gmshClient->Error("Did not receive message body: aborting remote get");
return false; return false;
} }
if(type == GmshSocket::GMSH_ONELAB_PARAM){ if(type == GmshSocket::GMSH_PARAMETER){
printf("Remote: got %s!\n", msg.c_str()); T p;
p.fromChar(msg);
parameters.push_back(p);
return true; return true;
} }
else{ else{
......
...@@ -115,8 +115,31 @@ bool onelab::localNetworkClient::run(const std::string &what) ...@@ -115,8 +115,31 @@ bool onelab::localNetworkClient::run(const std::string &what)
case GmshSocket::GMSH_STOP: case GmshSocket::GMSH_STOP:
_pid = -1; _pid = -1;
break; break;
case GmshSocket::GMSH_ONELAB_PARAM: case GmshSocket::GMSH_PARAMETER:
printf("server: got onelab param %s!\n", message.c_str()); {
std::string type, name;
onelab::parameter::getTypeAndNameFromChar(message, type, name);
if(type == "number"){
onelab::number par;
par.fromChar(message);
set(par);
printf("gmsh got '%s' from %s\n", par.toChar().c_str(), _name.c_str());
}
}
break;
case GmshSocket::GMSH_PARAMETER_QUERY:
{
std::string type, name;
onelab::parameter::getTypeAndNameFromChar(message, type, name);
if(type == "number"){
std::vector<onelab::number> par;
get(par, name);
std::string reply;
if(par.size() == 1) reply = par[0].toChar();
else reply = message;
server->SendMessage(GmshSocket::GMSH_PARAMETER, reply.size(), &reply[0]);
}
}
break; break;
case GmshSocket::GMSH_PROGRESS: case GmshSocket::GMSH_PROGRESS:
Msg::StatusBar(2, false, "%s %s", _name.c_str(), message.c_str()); Msg::StatusBar(2, false, "%s %s", _name.c_str(), message.c_str());
...@@ -173,11 +196,9 @@ bool onelab::localNetworkClient::kill() ...@@ -173,11 +196,9 @@ bool onelab::localNetworkClient::kill()
void onelab_cb(Fl_Widget *w, void *data) void onelab_cb(Fl_Widget *w, void *data)
{ {
printf("onelab has %d clients\n", onelab::server::instance()->getNumClients());
for(onelab::server::citer it = onelab::server::instance()->firstClient(); for(onelab::server::citer it = onelab::server::instance()->firstClient();
it != onelab::server::instance()->lastClient(); it++){ it != onelab::server::instance()->lastClient(); it++){
onelab::client *c = it->second; onelab::client *c = it->second;
printf("client name = %s\n", c->getName().c_str());
c->run("/Users/geuzaine/src/getdp/demos/test.pro"); c->run("/Users/geuzaine/src/getdp/demos/test.pro");
} }
FlGui::instance()->onelab->show(); FlGui::instance()->onelab->show();
...@@ -185,11 +206,9 @@ void onelab_cb(Fl_Widget *w, void *data) ...@@ -185,11 +206,9 @@ void onelab_cb(Fl_Widget *w, void *data)
void onelab_compute_cb(Fl_Widget *w, void *data) void onelab_compute_cb(Fl_Widget *w, void *data)
{ {
printf("onelab has %d clients\n", onelab::server::instance()->getNumClients());
for(onelab::server::citer it = onelab::server::instance()->firstClient(); for(onelab::server::citer it = onelab::server::instance()->firstClient();
it != onelab::server::instance()->lastClient(); it++){ it != onelab::server::instance()->lastClient(); it++){
onelab::client *c = it->second; onelab::client *c = it->second;
printf("client name = %s\n", c->getName().c_str());
c->run("/Users/geuzaine/src/getdp/demos/test.pro -solve MagSta_phi -pos phi"); c->run("/Users/geuzaine/src/getdp/demos/test.pro -solve MagSta_phi -pos phi");
} }
} }
......
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