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

up

parent d95a6284
No related branches found
No related tags found
No related merge requests found
Pipeline #2274 passed
......@@ -74,9 +74,11 @@ namespace onelab{
bool _visible;
// sould the paramete be "read-only" (not settable by the user)
bool _readOnly;
protected:
// optional additional attributes
std::map<std::string, std::string> _attributes;
public:
parameter(const std::string &name = "", const std::string &label = "",
const std::string &help = "")
......@@ -116,11 +118,13 @@ namespace onelab{
{
_attributes = attributes;
}
void setClients(const std::map<std::string, int> &clients){ _clients = clients; }
void setClients(const std::map<std::string, int> &clients)
{
_clients = clients;
}
void addClient(const std::string &client, int changed)
{
if(_clients.find(client) == _clients.end())
_clients[client] = changed;
if(_clients.find(client) == _clients.end()) _clients[client] = changed;
}
void addClients(const std::map<std::string, int> &clients)
{
......@@ -152,19 +156,15 @@ namespace onelab{
std::string s = _name;
// remove path
std::string::size_type last = _name.find_last_of('/');
if(last != std::string::npos)
s = _name.substr(last + 1);
if(last != std::string::npos) s = _name.substr(last + 1);
// remove starting white space
while(s.size() && s[0] == ' ')
s = s.substr(1);
while(s.size() && s[0] == ' ') s = s.substr(1);
// remove starting braces: can be used to order parameters 'from the end',
// as the ASCII code is after numbers and letters
while(s.size() && (s[0] == '}' || s[0] == '{'))
s = s.substr(1);
while(s.size() && (s[0] == '}' || s[0] == '{')) s = s.substr(1);
// remove starting numbers: can be used to order parameters 'from the
// start'
while(s.size() && s[0] >= '0' && s[0] <= '9')
s = s.substr(1);
while(s.size() && s[0] >= '0' && s[0] <= '9') s = s.substr(1);
if(units.empty())
return s;
else
......@@ -174,8 +174,10 @@ namespace onelab{
{
if(client.size()) {
std::map<std::string, int>::const_iterator it = _clients.find(client);
if(it != _clients.end()) return it->second;
else return 0;
if(it != _clients.end())
return it->second;
else
return 0;
}
else {
int changed = 0;
......@@ -192,7 +194,8 @@ namespace onelab{
bool getReadOnly() const { return _readOnly; }
std::string getAttribute(const std::string &key) const
{
std::map<std::string, std::string>::const_iterator it = _attributes.find(key);
std::map<std::string, std::string>::const_iterator it =
_attributes.find(key);
if(it != _attributes.end()) return it->second;
return "";
}
......@@ -239,8 +242,7 @@ namespace onelab{
const std::string &whitespace = " \t\n")
{
std::string::size_type strBegin = str.find_first_not_of(whitespace);
if(strBegin == std::string::npos)
return ""; // no content
if(strBegin == std::string::npos) return ""; // no content
std::string::size_type strEnd = str.find_last_not_of(whitespace);
std::string::size_type strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
......@@ -256,22 +258,21 @@ namespace onelab{
{
std::ostringstream sstream;
sstream << version() << charSep() << getType() << charSep()
<< sanitize(getName()) << charSep()
<< sanitize(getLabel()) << charSep()
<< sanitize(getHelp()) << charSep()
<< getChangedValue() << charSep()
<< (getVisible() ? 1 : 0) << charSep()
<< (getReadOnly() ? 1 : 0) << charSep()
<< sanitize(getName()) << charSep() << sanitize(getLabel())
<< charSep() << sanitize(getHelp()) << charSep()
<< getChangedValue() << charSep() << (getVisible() ? 1 : 0)
<< charSep() << (getReadOnly() ? 1 : 0) << charSep()
<< _attributes.size() << charSep();
for(std::map<std::string, std::string>::const_iterator it = _attributes.begin();
for(std::map<std::string, std::string>::const_iterator it =
_attributes.begin();
it != _attributes.end(); it++)
sstream << sanitize(it->first) << charSep()
<< sanitize(it->second) << charSep();
sstream << sanitize(it->first) << charSep() << sanitize(it->second)
<< charSep();
sstream << getClients().size() << charSep();
for(std::map<std::string, int>::const_iterator it = getClients().begin();
it != getClients().end(); it++)
sstream << sanitize(it->first) << charSep()
<< (it->second ? 1 : 0) << charSep();
sstream << sanitize(it->first) << charSep() << (it->second ? 1 : 0)
<< charSep();
return sstream.str();
}
virtual std::string::size_type fromChar(const std::string &msg)
......@@ -316,8 +317,7 @@ namespace onelab{
if(!fscanf(fp, "%d ", &numc)) break; // space is important
if(!numc) break;
msg.push_back("");
for(int i = 0; i < numc; i++)
msg.back() += fgetc(fp);
for(int i = 0; i < numc; i++) msg.back() += fgetc(fp);
if(!fgets(tmp, sizeof(tmp), fp)) break; // end of line
}
return true;
......@@ -327,12 +327,11 @@ namespace onelab{
{
time_t now;
time(&now);
fprintf(fp, "ONELAB database created by %s on %s",
creator.c_str(), ctime(&now));
fprintf(fp, "ONELAB database created by %s on %s", creator.c_str(),
ctime(&now));
for(unsigned int i = 0; i < msg.size(); i++) {
fprintf(fp, "%d ", (int)msg[i].size());
for(unsigned int j = 0; j < msg[i].size(); j++)
fputc(msg[i][j], fp);
for(unsigned int j = 0; j < msg[i].size(); j++) fputc(msg[i][j], fp);
fputc('\n', fp);
}
return true;
......@@ -356,7 +355,8 @@ namespace onelab{
<< ", \"readOnly\":" << (getReadOnly() ? "true" : "false");
if(_attributes.size()) {
sstream << ", \"attributes\":{ ";
for(std::map<std::string, std::string>::const_iterator it = _attributes.begin();
for(std::map<std::string, std::string>::const_iterator it =
_attributes.begin();
it != _attributes.end(); it++) {
if(it != _attributes.begin()) sstream << ", ";
sstream << "\"" << sanitizeJSON(it->first) << "\":\""
......@@ -366,11 +366,11 @@ namespace onelab{
}
if(getClients().size()) {
sstream << ", \"clients\":{ ";
for(std::map<std::string, int>::const_iterator it = getClients().begin();
for(std::map<std::string, int>::const_iterator it =
getClients().begin();
it != getClients().end(); it++) {
if(it != getClients().begin()) sstream << ", ";
sstream << "\"" << sanitizeJSON(it->first) << "\":"
<< it->second;
sstream << "\"" << sanitizeJSON(it->first) << "\":" << it->second;
}
sstream << " }";
}
......@@ -379,7 +379,8 @@ namespace onelab{
#if defined(HAVE_PICOJSON)
virtual bool fromJSON(const picojson::value::object &par)
{
for(picojson::value::object::const_iterator it = par.begin(); it != par.end(); ++it){
for(picojson::value::object::const_iterator it = par.begin();
it != par.end(); ++it) {
if(it->first == "name") {
if(!it->second.is<std::string>()) return false;
setName(it->second.get<std::string>());
......@@ -406,8 +407,10 @@ namespace onelab{
}
else if(it->first == "attributes") {
if(!it->second.is<picojson::object>()) return false;
const picojson::value::object &obj = it->second.get<picojson::object>();
for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
const picojson::value::object &obj =
it->second.get<picojson::object>();
for(picojson::value::object::const_iterator i = obj.begin();
i != obj.end(); ++i) {
std::string key(i->first);
if(!i->second.is<std::string>()) return false;
setAttribute(key, i->second.get<std::string>());
......@@ -415,8 +418,10 @@ namespace onelab{
}
else if(it->first == "clients") {
if(!it->second.is<picojson::object>()) return false;
const picojson::value::object &obj = it->second.get<picojson::object>();
for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
const picojson::value::object &obj =
it->second.get<picojson::object>();
for(picojson::value::object::const_iterator i = obj.begin();
i != obj.end(); ++i) {
std::string client(i->first);
if(!i->second.is<double>()) return false;
addClient(client, (int)i->second.get<double>());
......@@ -446,16 +451,25 @@ namespace onelab{
// when not in a loop
int _index;
std::map<double, std::string> _valueLabels;
public:
number(const std::string &name = "", double value = 0.,
const std::string &label = "", const std::string &help = "")
: parameter(name, label, help), _values(std::vector<double>(1, value)),
_min(-maxNumber()), _max(maxNumber()), _step(0.), _index(-1){}
_min(-maxNumber()), _max(maxNumber()), _step(0.), _index(-1)
{
}
number(const std::string &name, const std::vector<double> &values,
const std::string &label = "", const std::string &help = "")
: parameter(name, label, help), _values(values),
_min(-maxNumber()), _max(maxNumber()), _step(0.), _index(-1){}
void setValue(double value){ _values.resize(1); _values[0] = value; }
: parameter(name, label, help), _values(values), _min(-maxNumber()),
_max(maxNumber()), _step(0.), _index(-1)
{
}
void setValue(double value)
{
_values.resize(1);
_values[0] = value;
}
void setValues(const std::vector<double> &values) { _values = values; }
void setMin(double min) { _min = min; }
void setMax(double max) { _max = max; }
......@@ -478,7 +492,11 @@ namespace onelab{
_valueLabels[value] = label;
}
std::string getType() const { return "number"; }
double getValue() const { if(_values.empty()) return 0.; return _values[0]; }
double getValue() const
{
if(_values.empty()) return 0.;
return _values[0];
}
const std::vector<double> &getValues() const { return _values; }
unsigned int getNumValues() const { return _values.size(); }
double getMin() const { return _min; }
......@@ -492,7 +510,8 @@ namespace onelab{
}
std::string getValueLabel(double value) const
{
std::map<double, std::string>::const_iterator it = _valueLabels.find(value);
std::map<double, std::string>::const_iterator it =
_valueLabels.find(value);
if(it != _valueLabels.end()) return it->second;
return "";
}
......@@ -527,22 +546,18 @@ namespace onelab{
{
std::ostringstream sstream;
sstream.precision(16);
sstream << parameter::toChar()
<< _values.size() << charSep();
sstream << parameter::toChar() << _values.size() << charSep();
for(unsigned int i = 0; i < _values.size(); i++)
sstream << _values[i] << charSep();
sstream << _min << charSep()
<< _max << charSep()
<< _step << charSep()
<< _index << charSep()
<< _choices.size() << charSep();
sstream << _min << charSep() << _max << charSep() << _step << charSep()
<< _index << charSep() << _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++)
sstream << _choices[i] << charSep();
sstream << _valueLabels.size() << charSep();
for(std::map<double, std::string>::const_iterator it = _valueLabels.begin();
for(std::map<double, std::string>::const_iterator it =
_valueLabels.begin();
it != _valueLabels.end(); it++) {
sstream << it->first << charSep()
<< sanitize(it->second) << charSep();
sstream << it->first << charSep() << sanitize(it->second) << charSep();
}
return sstream.str();
}
......@@ -571,17 +586,14 @@ namespace onelab{
{
std::ostringstream sstream;
sstream.precision(16);
sstream << "{ " << parameter::toJSON()
<< ", \"values\":[ ";
sstream << "{ " << parameter::toJSON() << ", \"values\":[ ";
for(unsigned int i = 0; i < _values.size(); i++) {
if(i) sstream << ", ";
sstream << _values[i];
}
sstream << " ]"
<< ", \"min\":" << _min
<< ", \"max\":" << _max
<< ", \"step\":" << _step
<< ", \"index\":" << _index;
<< ", \"min\":" << _min << ", \"max\":" << _max
<< ", \"step\":" << _step << ", \"index\":" << _index;
if(_choices.size()) {
sstream << ", \"choices\":[ ";
for(unsigned int i = 0; i < _choices.size(); i++) {
......@@ -592,7 +604,8 @@ namespace onelab{
}
if(_valueLabels.size()) {
sstream << ", \"valueLabels\":{ ";
for(std::map<double, std::string>::const_iterator it = _valueLabels.begin();
for(std::map<double, std::string>::const_iterator it =
_valueLabels.begin();
it != _valueLabels.end(); it++) {
if(it != _valueLabels.begin()) sstream << ", ";
sstream << "\"" << sanitizeJSON(it->second) << "\":" << it->first;
......@@ -606,7 +619,8 @@ namespace onelab{
bool fromJSON(const picojson::value::object &par)
{
if(!parameter::fromJSON(par)) return false;
for(picojson::value::object::const_iterator it = par.begin(); it != par.end(); ++it){
for(picojson::value::object::const_iterator it = par.begin();
it != par.end(); ++it) {
if(it->first == "values") {
if(!it->second.is<picojson::array>()) return false;
const picojson::value::array &arr = it->second.get<picojson::array>();
......@@ -643,8 +657,10 @@ namespace onelab{
}
else if(it->first == "valueLabels") {
if(!it->second.is<picojson::object>()) return false;
const picojson::value::object &obj = it->second.get<picojson::object>();
for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
const picojson::value::object &obj =
it->second.get<picojson::object>();
for(picojson::value::object::const_iterator i = obj.begin();
i != obj.end(); ++i) {
if(!i->second.is<double>()) return false;
_valueLabels[i->second.get<double>()] = i->first;
}
......@@ -661,24 +677,36 @@ namespace onelab{
private:
std::vector<std::string> _values, _choices;
std::string _kind;
public:
string(const std::string &name = "", const std::string &value = "",
const std::string &label = "", const std::string &help = "")
: parameter(name, label, help), _values(std::vector<std::string>(1, value)),
_kind("generic") {}
: parameter(name, label, help),
_values(std::vector<std::string>(1, value)), _kind("generic")
{
}
string(const std::string &name, const std::vector<std::string> &values,
const std::string &label = "", const std::string &help = "")
: parameter(name, label, help), _values(values),
_kind("generic") {}
void setValue(const std::string &value){ _values.resize(1); _values[0] = value; }
: parameter(name, label, help), _values(values), _kind("generic")
{
}
void setValue(const std::string &value)
{
_values.resize(1);
_values[0] = value;
}
void setValues(const std::vector<std::string> &values) { _values = values; }
void setKind(const std::string &kind) { _kind = kind; }
void setChoices(const std::vector<std::string> &choices){ _choices = choices; }
void setChoices(const std::vector<std::string> &choices)
{
_choices = choices;
}
std::string getType() const { return "string"; }
const std::string &getValue() const
{
static std::string n("");
if(_values.empty()) return n; return _values[0];
if(_values.empty()) return n;
return _values[0];
}
const std::vector<std::string> &getValues() const { return _values; }
unsigned int getNumValues() const { return _values.size(); }
......@@ -713,12 +741,10 @@ namespace onelab{
std::string toChar() const
{
std::ostringstream sstream;
sstream << parameter::toChar()
<< _values.size() << charSep();
sstream << parameter::toChar() << _values.size() << charSep();
for(unsigned int i = 0; i < _values.size(); i++)
sstream << sanitize(_values[i]) << charSep();
sstream << sanitize(_kind) << charSep()
<< _choices.size() << charSep();
sstream << sanitize(_kind) << charSep() << _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++)
sstream << sanitize(_choices[i]) << charSep();
return sstream.str();
......@@ -739,8 +765,7 @@ namespace onelab{
std::string toJSON() const
{
std::ostringstream sstream;
sstream << "{ " << parameter::toJSON()
<< ", \"values\":[ " ;
sstream << "{ " << parameter::toJSON() << ", \"values\":[ ";
for(unsigned int i = 0; i < _values.size(); i++) {
if(i) sstream << ", ";
sstream << "\"" << sanitizeJSON(_values[i]) << "\"";
......@@ -763,7 +788,8 @@ namespace onelab{
bool fromJSON(const picojson::value::object &par)
{
if(!parameter::fromJSON(par)) return false;
for(picojson::value::object::const_iterator it = par.begin(); it != par.end(); ++it){
for(picojson::value::object::const_iterator it = par.begin();
it != par.end(); ++it) {
if(it->first == "values") {
if(!it->second.is<picojson::array>()) return false;
const picojson::value::array &arr = it->second.get<picojson::array>();
......@@ -799,8 +825,8 @@ namespace onelab{
std::set<number *, parameterLessThan> _numbers;
std::set<string *, parameterLessThan> _strings;
// delete a parameter from the parameter space
template <class T> bool _clear(const std::string &name,
const std::string &client,
template <class T>
bool _clear(const std::string &name, const std::string &client,
std::set<T *, parameterLessThan> &ps)
{
if(name.empty() && client.size()) {
......@@ -835,17 +861,20 @@ namespace onelab{
// (adding new clients if necessary). This would need to be locked to avoid
// race conditions when several clients try to set a parameter at the same
// time.
template <class T> bool _set(const T &p, const std::string &client,
template <class T>
bool _set(const T &p, const std::string &client,
std::set<T *, parameterLessThan> &ps)
{
typename std::set<T *, parameterLessThan>::iterator it = ps.find((T *)&p);
if(it != ps.end()) {
(*it)->update(p);
if(client.size()) (*it)->addClient(client, parameter::defaultChangedValue());
if(client.size())
(*it)->addClient(client, parameter::defaultChangedValue());
}
else {
T *newp = new T(p);
if(client.size()) newp->addClient(client, parameter::defaultChangedValue());
if(client.size())
newp->addClient(client, parameter::defaultChangedValue());
ps.insert(newp);
}
return true;
......@@ -854,9 +883,9 @@ namespace onelab{
// category if no name is given. If we find a given parameter by name, we
// add the client requesting the parameter to the list of clients for this
// parameter. This would also need to be locked.
template <class T> bool _get(std::vector<T> &p, const std::string &name,
const std::string &client,
std::set<T*, parameterLessThan> &ps)
template <class T>
bool _get(std::vector<T> &p, const std::string &name,
const std::string &client, std::set<T *, parameterLessThan> &ps)
{
p.clear();
if(name.empty()) {
......@@ -868,23 +897,27 @@ namespace onelab{
T tmp(name);
typename std::set<T *, parameterLessThan>::iterator it = ps.find(&tmp);
if(it != ps.end()) {
if(client.size()) (*it)->addClient(client, parameter::defaultChangedValue());
if(client.size())
(*it)->addClient(client, parameter::defaultChangedValue());
p.push_back(**it);
}
}
return true;
}
template <class T> T* _getPtr(std::string name, const std::string client,
template <class T>
T *_getPtr(std::string name, const std::string client,
std::set<T *, parameterLessThan> ps)
{
T tmp(name);
typename std::set<T *, parameterLessThan>::iterator it = ps.find(&tmp);
if(it != ps.end()) {
if(client.size()) (*it)->addClient(client, parameter::defaultChangedValue());
if(client.size())
(*it)->addClient(client, parameter::defaultChangedValue());
return *it;
}
return NULL;
}
public:
parameterSpace() {}
~parameterSpace() { clear(); }
......@@ -981,8 +1014,7 @@ namespace onelab{
for(std::set<parameter *, parameterLessThan>::iterator it = ps.begin();
it != ps.end(); it++) {
int changed = (*it)->getChanged(client);
if(changed > threshold)
(*it)->setChanged(threshold, client);
if(changed > threshold) (*it)->setChanged(threshold, client);
}
}
// serialize the parameter space (optionally only serialize those parameters
......@@ -992,7 +1024,8 @@ namespace onelab{
std::vector<std::string> s;
std::set<parameter *, parameterLessThan> ps;
getAllParameters(ps);
for(std::set<parameter*, parameterLessThan>::const_iterator it = ps.begin();
for(std::set<parameter *, parameterLessThan>::const_iterator it =
ps.begin();
it != ps.end(); it++)
if(client.empty() || (*it)->hasClient(client)) {
if((*it)->getAttribute("NotInDb") != "True")
......@@ -1001,17 +1034,22 @@ namespace onelab{
return s;
}
// unserialize the parameter space
bool fromChar(const std::vector<std::string> &msg, const std::string &client="")
bool fromChar(const std::vector<std::string> &msg,
const std::string &client = "")
{
for(unsigned int i = 0; i < msg.size(); i++) {
std::string version, type, name;
onelab::parameter::getInfoFromChar(msg[i], version, type, name);
if(onelab::parameter::version() != version) return false;
if(type == "number") {
number p; p.fromChar(msg[i]); set(p, client);
number p;
p.fromChar(msg[i]);
set(p, client);
}
else if(type == "string") {
string p; p.fromChar(msg[i]); set(p, client);
string p;
p.fromChar(msg[i]);
set(p, client);
}
else
return false;
......@@ -1033,7 +1071,8 @@ namespace onelab{
json += " \"parameters\":[\n";
std::set<parameter *, parameterLessThan> ps;
getAllParameters(ps);
for(std::set<parameter*, parameterLessThan>::const_iterator it = ps.begin();
for(std::set<parameter *, parameterLessThan>::const_iterator it =
ps.begin();
it != ps.end(); it++) {
if(it != ps.begin()) json += ",\n";
if(client.empty() || (*it)->hasClient(client)) {
......@@ -1053,28 +1092,37 @@ namespace onelab{
if(err.size()) return false;
if(!v.is<picojson::object>()) return false;
const picojson::value::object &obj = v.get<picojson::object>();
for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
for(picojson::value::object::const_iterator i = obj.begin();
i != obj.end(); ++i) {
if(i->first == "onelab") { // onelab database
if(!i->second.is<picojson::object>()) return false;
const picojson::value::object &db = i->second.get<picojson::object>();
for (picojson::value::object::const_iterator j = db.begin(); j != db.end(); ++j) {
for(picojson::value::object::const_iterator j = db.begin();
j != db.end(); ++j) {
if(j->first == "version") {
if(!j->second.is<std::string>()) return false;
if(j->second.get<std::string>() != parameter::version()) return false;
if(j->second.get<std::string>() != parameter::version())
return false;
}
else if(j->first == "parameters") {
if(!j->second.is<picojson::array>()) return false;
const picojson::value::array &arr = j->second.get<picojson::array>();
const picojson::value::array &arr =
j->second.get<picojson::array>();
for(unsigned int k = 0; k < arr.size(); k++) {
if(!arr[k].is<picojson::object>()) return false;
const picojson::value::object &par = arr[k].get<picojson::object>();
const picojson::value::object &par =
arr[k].get<picojson::object>();
picojson::value::object::const_iterator it = par.find("type");
if(it == par.end()) return false;
if(it->second.to_str() == "number") {
number p; p.fromJSON(par); set(p, client);
number p;
p.fromJSON(par);
set(p, client);
}
else if(it->second.to_str() == "string") {
string p; p.fromJSON(par); set(p, client);
string p;
p.fromJSON(par);
set(p, client);
}
}
}
......@@ -1099,6 +1147,7 @@ namespace onelab{
int _id;
// the index of the client in an external client list (if any)
int _index;
public:
client(const std::string &name) : _name(name), _id(0), _index(-1) {}
virtual ~client() {}
......@@ -1110,10 +1159,22 @@ namespace onelab{
virtual bool run() { return false; }
virtual bool isNetworkClient() { return false; }
virtual bool kill() { return false; }
virtual void sendInfo(const std::string &msg){ std::cout << msg << std::endl; }
virtual void sendWarning(const std::string &msg){ std::cerr << msg << std::endl; }
virtual void sendError(const std::string &msg){ std::cerr << msg << std::endl; }
virtual void sendProgress(const std::string &msg){ std::cout << msg << std::endl; }
virtual void sendInfo(const std::string &msg)
{
std::cout << msg << std::endl;
}
virtual void sendWarning(const std::string &msg)
{
std::cerr << msg << std::endl;
}
virtual void sendError(const std::string &msg)
{
std::cerr << msg << std::endl;
}
virtual void sendProgress(const std::string &msg)
{
std::cout << msg << std::endl;
}
virtual void sendMergeFileRequest(const std::string &msg) {}
virtual void sendOpenProjectRequest(const std::string &msg) {}
virtual void sendParseStringRequest(const std::string &msg) {}
......@@ -1125,14 +1186,18 @@ namespace onelab{
virtual bool get(std::vector<string> &ps, const std::string &name = "") = 0;
virtual bool setAndAppendChoices(const number &p) = 0;
virtual bool setAndAppendChoices(const string &p) = 0;
virtual bool getWithoutChoices(std::vector<number> &ps, const std::string &name="") = 0;
virtual bool getWithoutChoices(std::vector<string> &ps, const std::string &name="") = 0;
virtual bool getWithoutChoices(std::vector<number> &ps,
const std::string &name = "") = 0;
virtual bool getWithoutChoices(std::vector<string> &ps,
const std::string &name = "") = 0;
std::vector<std::string> toChar()
{
std::vector<std::string> out;
std::vector<number> n; get(n);
std::vector<number> n;
get(n);
for(unsigned int i = 0; i < n.size(); i++) out.push_back(n[i].toChar());
std::vector<string> s; get(s);
std::vector<string> s;
get(s);
for(unsigned int i = 0; i < s.size(); i++) out.push_back(s[i].toChar());
return out;
}
......@@ -1143,20 +1208,21 @@ namespace onelab{
onelab::parameter::getInfoFromChar(msg[i], version, type, name);
if(onelab::parameter::version() != version) return false;
if(type == "number") {
number p; p.fromChar(msg[i]); set(p);
number p;
p.fromChar(msg[i]);
set(p);
}
else if(type == "string") {
string p; p.fromChar(msg[i]); set(p);
string p;
p.fromChar(msg[i]);
set(p);
}
else
return false;
}
return true;
}
bool toFile(FILE *fp)
{
return parameter::toFile(toChar(), fp, getName());
}
bool toFile(FILE *fp) { return parameter::toFile(toChar(), fp, getName()); }
bool fromFile(FILE *fp)
{
std::vector<std::string> msg;
......@@ -1177,6 +1243,7 @@ namespace onelab{
std::set<client *> _clients;
// the parameter space
parameterSpace _parameterSpace;
public:
server(const std::string &address = "") : _address(address) {}
~server() {}
......@@ -1194,7 +1261,8 @@ namespace onelab{
{
return _parameterSpace.set(p, client);
}
template <class T> bool get(std::vector<T> &ps, const std::string &name="",
template <class T>
bool get(std::vector<T> &ps, const std::string &name = "",
const std::string &client = "")
{
return _parameterSpace.get(ps, name, client);
......@@ -1227,12 +1295,16 @@ namespace onelab{
{
_parameterSpace.thresholdChanged(value, client);
}
unsigned int getNumParameters(){ return _parameterSpace.getNumParameters(); }
unsigned int getNumParameters()
{
return _parameterSpace.getNumParameters();
}
std::vector<std::string> toChar(const std::string &client = "")
{
return _parameterSpace.toChar(client);
}
bool fromChar(const std::vector<std::string> &msg, const std::string &client="")
bool fromChar(const std::vector<std::string> &msg,
const std::string &client = "")
{
return _parameterSpace.fromChar(msg, client);
}
......@@ -1264,21 +1336,19 @@ namespace onelab{
server::instance()->set(p, _name);
return true;
}
template <class T> bool _get(std::vector<T> &ps,
const std::string &name="")
template <class T>
bool _get(std::vector<T> &ps, const std::string &name = "")
{
server::instance()->get(ps, name, _name);
return true;
}
public:
localClient(const std::string &name) : client(name)
{
server::instance()->registerClient(this);
}
virtual ~localClient()
{
server::instance()->unregisterClient(this);
}
virtual ~localClient() { server::instance()->unregisterClient(this); }
virtual bool clear(const std::string &name = "")
{
server::instance()->clear(name);
......@@ -1286,17 +1356,22 @@ namespace onelab{
}
virtual bool set(const number &p) { return _set(p); }
virtual bool set(const string &p) { return _set(p); }
virtual bool get(std::vector<number> &ps,
const std::string &name=""){ return _get(ps, name); }
virtual bool get(std::vector<string> &ps,
const std::string &name=""){ return _get(ps, name); }
virtual bool get(std::vector<number> &ps, const std::string &name = "")
{
return _get(ps, name);
}
virtual bool get(std::vector<string> &ps, const std::string &name = "")
{
return _get(ps, name);
}
virtual bool setAndAppendChoices(const number &p)
{
std::vector<number> ps;
_get(ps, _name);
std::vector<double> choices;
if(ps.size()) choices = ps[0].getChoices();
choices.insert(choices.end(), p.getChoices().begin(), p.getChoices().end());
choices.insert(choices.end(), p.getChoices().begin(),
p.getChoices().end());
number p2(p);
p2.setChoices(choices);
return _set(p2);
......@@ -1307,7 +1382,8 @@ namespace onelab{
_get(ps, _name);
std::vector<std::string> choices;
if(ps.size()) choices = ps[0].getChoices();
choices.insert(choices.end(), p.getChoices().begin(), p.getChoices().end());
choices.insert(choices.end(), p.getChoices().begin(),
p.getChoices().end());
string p2(p);
p2.setChoices(choices);
return _set(p2);
......@@ -1347,6 +1423,7 @@ namespace onelab{
int _pid;
// underlying GmshServer
GmshServer *_gmshServer;
public:
localNetworkClient(const std::string &name, const std::string &executable,
const std::string &remoteLogin = "",
......@@ -1354,7 +1431,9 @@ namespace onelab{
: localClient(name), _executable(executable),
_treatExecutableAsFullCommandLine(treatExecutableAsFullCommandLine),
_remoteLogin(remoteLogin), _socketSwitch("-onelab"), _pid(-1),
_gmshServer(0) {}
_gmshServer(0)
{
}
virtual ~localNetworkClient() {}
virtual bool isNetworkClient() { return true; }
const std::string &getExecutable() { return _executable; }
......@@ -1388,12 +1467,14 @@ namespace onelab{
{
if(!_gmshClient) return false;
std::string msg = p.toChar();
_gmshClient->SendMessage(withChoices ? GmshSocket::GMSH_PARAMETER :
_gmshClient->SendMessage(withChoices ?
GmshSocket::GMSH_PARAMETER :
GmshSocket::GMSH_PARAMETER_WITHOUT_CHOICES,
msg.size(), &msg[0]);
return true;
}
template <class T> bool _get(std::vector<T> &ps, const std::string &name="",
template <class T>
bool _get(std::vector<T> &ps, const std::string &name = "",
bool withChoices = true)
{
ps.clear();
......@@ -1401,11 +1482,13 @@ namespace onelab{
T p(name);
std::string msg = p.toChar();
if(name.size())
_gmshClient->SendMessage(withChoices ? GmshSocket::GMSH_PARAMETER_QUERY :
_gmshClient->SendMessage(
withChoices ? GmshSocket::GMSH_PARAMETER_QUERY :
GmshSocket::GMSH_PARAMETER_QUERY_WITHOUT_CHOICES,
msg.size(), &msg[0]);
else // get all parameters
_gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER_QUERY_ALL, msg.size(), &msg[0]);
_gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER_QUERY_ALL,
msg.size(), &msg[0]);
while(1) {
// stop if we have no communications for 5 minutes
......@@ -1420,12 +1503,14 @@ namespace onelab{
}
int type, length, swap;
if(!_gmshClient->ReceiveHeader(&type, &length, &swap)) {
_gmshClient->Error("Did not receive message header: aborting remote get");
_gmshClient->Error(
"Did not receive message header: aborting remote get");
return false;
}
std::string msg(length, ' ');
if(!_gmshClient->ReceiveMessage(length, &msg[0])) {
_gmshClient->Error("Did not receive message body: aborting remote get");
_gmshClient->Error(
"Did not receive message body: aborting remote get");
return false;
}
if(type == GmshSocket::GMSH_PARAMETER) {
......@@ -1458,6 +1543,7 @@ namespace onelab{
}
return true;
}
public:
void waitOnSubClients()
{
......@@ -1474,20 +1560,23 @@ namespace onelab{
}
int type, length, swap;
if(!_gmshClient->ReceiveHeader(&type, &length, &swap)) {
_gmshClient->Error("Did not receive message header: aborting wait on subclients");
_gmshClient->Error(
"Did not receive message header: aborting wait on subclients");
return;
}
std::string msg(length, ' ');
if(!_gmshClient->ReceiveMessage(length, &msg[0])) {
_gmshClient->Error("Did not receive message body: aborting wait on subclients");
_gmshClient->Error(
"Did not receive message body: aborting wait on subclients");
return;
}
if(type == GmshSocket::GMSH_STOP)
_numSubClients -= 1;
if(type == GmshSocket::GMSH_STOP) _numSubClients -= 1;
}
}
public:
remoteNetworkClient(const std::string &name, const std::string &serverAddress)
remoteNetworkClient(const std::string &name,
const std::string &serverAddress)
: client(name), _serverAddress(serverAddress), _numSubClients(0)
{
_gmshClient = new GmshClient();
......@@ -1516,7 +1605,8 @@ namespace onelab{
if(!_gmshClient) return false;
std::string msg = name;
if(msg.empty()) msg = "*";
_gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER_CLEAR, msg.size(), &msg[0]);
_gmshClient->SendMessage(GmshSocket::GMSH_PARAMETER_CLEAR, msg.size(),
&msg[0]);
return true;
}
virtual bool set(const number &p) { return _set(p); }
......@@ -1531,11 +1621,13 @@ namespace onelab{
}
virtual bool setAndAppendChoices(const number &p) { return _set(p, false); }
virtual bool setAndAppendChoices(const string &p) { return _set(p, false); }
virtual bool getWithoutChoices(std::vector<number> &ps, const std::string &name="")
virtual bool getWithoutChoices(std::vector<number> &ps,
const std::string &name = "")
{
return _get(ps, name, false);
}
virtual bool getWithoutChoices(std::vector<string> &ps, const std::string &name="")
virtual bool getWithoutChoices(std::vector<string> &ps,
const std::string &name = "")
{
return _get(ps, name, false);
}
......@@ -1567,7 +1659,8 @@ namespace onelab{
{
if(_gmshClient) _gmshClient->ParseString(msg.c_str());
}
void runNonBlockingSubClient(const std::string &name, const std::string &command)
void runNonBlockingSubClient(const std::string &name,
const std::string &command)
{
#if !defined(BUILD_IOS)
if(!_gmshClient) {
......@@ -1589,6 +1682,6 @@ namespace onelab{
}
};
}
} // namespace onelab
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment