//Public API ReturnCode Socket::bind(int port) { MSS_BEGIN(ReturnCode); MSS(createSocket_()); MSS(pimpl_->bind(port)); MSS_END(); }
ReturnCode Socket::receive(IOBuffer &buffer) { MSS_BEGIN(ReturnCode); MSS(pimpl_); MSS(pimpl_->receive(buffer)); MSS_END(); }
ReturnCode Socket::listen() { MSS_BEGIN(ReturnCode); MSS(pimpl_); MSS(pimpl_->listen()); MSS_END(); }
bool Planner::add_absence(const std::string &worker, const std::string &span) { MSS_BEGIN(bool); gubg::Strange strange(span); int year, month, day; MSS(strange.pop_decimal(year)); MSS(strange.pop_if('/')); MSS(strange.pop_decimal(month)); MSS(strange.pop_if('/')); MSS(strange.pop_decimal(day)); if (strange.pop_if('-')) { int year2, month2, day2; MSS(strange.pop_decimal(year2)); MSS(strange.pop_if('/')); MSS(strange.pop_decimal(month2)); MSS(strange.pop_if('/')); MSS(strange.pop_decimal(day2)); absence_per_worker[worker].push_back(Day(year, month, day)); for (auto d: dayRange(Day(year, month, day), Day(year2, month2, day2))) absence_per_worker[worker].push_back(d); } else { absence_per_worker[worker].push_back(Day(year, month, day)); } MSS_END(); }
ReturnCode Linker::addOption(const std::string &option) { MSS_BEGIN(ReturnCode); MSS((bool)itf_); MSS(itf_->setOption(option), UnknownOption); MSS_END(); }
ReturnCode process_() { MSS_BEGIN(ReturnCode, logns); Name name; switch (const auto rc = pop_name_(name)) { case ReturnCode::OK: break; case ReturnCode::NotFound: MSS_RETURN_OK(); break; default: MSS(rc); break; } Attributes attrs; MSS(pop_attrs_(attrs)); receiver_().parser_open(name, attrs); pop_whitespace_(); if (content_.pop_if('{')) { pop_whitespace_(); while (!content_.pop_if('}')) { MSS(process_()); } } receiver_().parser_close(); MSS_END(); }
ReturnCode computeMeanSigma(Result &mean, Result &sigma, const Values &values, const Weights &weights) { MSS_BEGIN(ReturnCode); MSS(!values.empty()); MSS(values.size() == weights.size()); //Compute mean and the sum of the weights mean = 0.0; Result sumW = 0.0; typename Values::const_iterator value = values.begin(); for (auto weight: weights) { mean += *(value++)*weight; sumW += weight; } mean /= sumW; //Compute sigma sigma = 0.0; typename Weights::const_iterator weight = weights.begin(); for (auto value: values) sigma += *(weight++)*(value-mean)*(value-mean); sigma = ::sqrt(sigma/sumW); MSS_END(); }
ReturnCode read(Type &type) { MSS_BEGIN(ReturnCode); MSS(it_ != end_, RangeIsEmpty); MSS(type.read(*(it_++))); MSS_END(); }
ReturnCode Socket::send(IOBuffer &buffer) { MSS_BEGIN(ReturnCode); MSS(pimpl_); MSS(pimpl_->send(buffer)); MSS_END(); }
ReturnCode Socket::shutdown() { MSS_BEGIN(ReturnCode); MSS(pimpl_); MSS(pimpl_->shutdown()); MSS_END(); }
ReturnCode listen() { MSS_BEGIN(ReturnCode); MSS(state == Bound, InvalidState); MSS(::listen(fid, MaxNrWaitingConnections) != -1, CouldNotListen); changeState(Listening); MSS_END(); }
ReturnCode State::execute(const std::string &code) { MSS_BEGIN(ReturnCode); CHECK_AND_LOCK(pimpl_); MSS(luaL_loadstring(s, code.c_str()) == 0, CompileError); MSS(lua_pcall(s, 0, 0, 0) == 0, RuntimeError); MSS_END(); }
ReturnCode Socket::accept(Socket &socket) { MSS_BEGIN(ReturnCode); MSS(pimpl_); int fid; MSS(pimpl_->accept(fid)); socket = Socket(fid); MSS_END(); }
ReturnCode Socket::createSocket_() { MSS_BEGIN(ReturnCode); MSS(!pimpl_, SocketAlreadyPresent); int fid = ::socket(PF_INET, SOCK_STREAM, 0); MSS(fid != InvalidFID, CouldNotGetSocketDescriptor); pimpl_.reset(new Pimpl(fid)); MSS_END(); }
ReturnCode send(IOBuffer &buffer) { MSS_BEGIN(ReturnCode); MSS(state == Connected, InvalidState); auto nrSent = ::send(fid, buffer.data(), buffer.size(), 0); MSS(nrSent != -1, CouldNotSend); buffer.scrollBegin(nrSent); MSS_END(); }
ReturnCode Library::ctor_(File fn) { MSS_BEGIN(ReturnCode); MSS(resolve(fn)); MSS(isRegular(fn)); Handle h; MSS(load_(h, fn.name())); pimpl_.reset(new Pimpl(h, fn)); MSS_END(); }
ReturnCode Socket::connect(const string &ip, int port) { MSS_BEGIN(ReturnCode); //TODO::In stead of splitting the socket creation and connect, this should be combined //to allow us to use the PF_INET/PF_INET6 settings resolved by getaddrinfo and to be used in //the socket creation MSS(createSocket_()); MSS(pimpl_->connect(ip, port)); MSS_END(); }
ReturnCode learn(const Data &data, double *width = nullptr, const Weights *weights = nullptr) { MSS_BEGIN(ReturnCode); MSS(setData_(data, weights)); if (width) MSS(setWidth(*width)); else MSS(learnWidth()); MSS_END(); }
bool add(Range &range, size_t dim) { MSS_BEGIN(bool); MSS(dim <= (data_.size()-next_ix_)); MSS(next_field_ix_ != fields.size()); range = fields[next_field_ix_] = Range(&data_[next_ix_], &data_[next_ix_+dim]); next_ix_ += dim; ++next_field_ix_; MSS_END(); }
ReturnCode accept(int &f) { MSS_BEGIN(ReturnCode); MSS(state == Listening, InvalidState); struct sockaddr_storage peer; socklen_t peerSize = sizeof(peer); int peerFid = ::accept(fid, (struct sockaddr *)&peer, &peerSize); MSS(peerFid != -1, CouldNotAccept); f = peerFid; MSS_END(); }
ReturnCode computeMeanSigma(Result &mean, Result &sigma, const Values &values) { MSS_BEGIN(ReturnCode); MSS(values.size() >= 2); MSS(computeMean(mean, values)); sigma = 0.0; for (auto value: values) sigma += (value-mean)*(value-mean); sigma /= values.size()-1; sigma = ::sqrt(sigma); MSS_END(); }
ReturnCode learnWidth() { MSS_BEGIN(ReturnCode); //Silvermans rule of thumb for kernel bandwidth selection //self.width=(1.06*@data.gStd*(Math.sqrt(@data.length).to_f**(-0.20))) //pg 16 of a comparison of kernel density estimates //self.width=(1.6644*@data.gStd*(Math.sqrt(@data.length).to_f**(-0.20))) double sum, sigma; MSS(computeMeanSigma(sum, sigma, data_, weights_)); sum *= data_.size(); MSS(sigma > 0.0, StdDevTooSmall); MSS(setWidth(1.6644*sigma*pow(sqrt(sum), -0.50))); MSS_END(); }
ReturnCode pop_attrs_(Attributes &attrs) { MSS_BEGIN(ReturnCode, logns); gubg::Strange attr; while ((pop_whitespace_(), content_.pop_bracket(attr, "[]"))) { std::string key, value; pop_whitespace_(attr); MSS(attr.pop_until(key, ':')); MSS(attr.pop_all(value)); L(C(key)C(value)); attrs[key] = value; } MSS_END(); }
ReturnCode read(char &ch) { MSS_BEGIN(ReturnCode); MSS(it_ != end_, RangeIsEmpty); ch = (char)*(it_++); MSS_END(); }
ReturnCode read(unsigned char &b) { MSS_BEGIN(ReturnCode); MSS(it_ != end_, RangeIsEmpty); b = (unsigned char)*(it_++); MSS_END(); }
ReturnCode Linker::addLibrary(const std::string &lib) { MSS_BEGIN(ReturnCode); MSS((bool)itf_); itf_->libraries.push_back(lib); MSS_END(); }
ReturnCode Linker::addLibraryPath(const gubg::file::File &lp) { MSS_BEGIN(ReturnCode); MSS((bool)itf_); itf_->libraryPaths.push_back(lp); MSS_END(); }
ReturnCode Linker::addObject(const gubg::file::File &obj) { MSS_BEGIN(ReturnCode); MSS((bool)itf_); itf_->objects.push_back(obj); MSS_END(); }
bool has_dim(size_t fix, unsigned int wanted_dim) const { MSS_BEGIN(bool); MSS(fix < fields.size()); MSS_Q(fields[fix].size() == wanted_dim); MSS_END(); }
bool write(Writer &w) const { MSS_BEGIN(bool); auto fix = 0u; //Copy of field is intentional for (Range field: fields) { auto write_field = [&](Writer &w1){ MSS_BEGIN(bool); if (!field.empty()) { w.text(field.front()); field.pop_front(); while (!field.empty()) { w.text(' '); w.text(field.front()); field.pop_front(); } } MSS_END(); }; MSS(w(fix, write_field)); ++fix; } MSS_END(); }