int main(int argc, char** argv) { int rank; assertz("MPI_Init", MPI_Init(&argc, &argv)); assertz("MPI_Comm_rank", MPI_Comm_rank(MPI_COMM_WORLD, &rank)); const int data_size = 2; int data_replace[data_size]; const int tag_1 = 12345; const int tag_2 = 67890; const int jack = 0; const int jill = 1; MPI_Status stat; if (rank == jack) { data_replace[0] = 11; data_replace[1] = 12; MPI_Sendrecv_replace(&data_replace, data_size, MPI_INT, jill, tag_1, jill, tag_2, MPI_COMM_WORLD, &stat); } if (rank == jill) { data_replace[0] = 21; data_replace[1] = 22; MPI_Sendrecv_replace(&data_replace, data_size, MPI_INT, jack, tag_2, jack, tag_1, MPI_COMM_WORLD, &stat); } if (rank < 2) { printf("rank = %d : data_replace[] = {%d, %d} \n", rank, data_replace[0], data_replace[1]); } assertz("MPI_Finalize", MPI_Finalize()); exit(EXIT_SUCCESS); }
std::string zpt::rest::authorization::serialize(zpt::json _info) { assertz( _info["owner"]->type() == zpt::JSString && _info["application"]->type() == zpt::JSString && _info["grant_type"]->type() == zpt::JSString, "token serialization failed: required fields are 'owner', 'application' and 'grant_type'", 412, 0); return _info["owner"]->str() + std::string("@") + _info["application"]->str() + std::string("/") + _info["grant_type"]->str() + std::string("/") + (_info["key"]->type() == zpt::JSString ? _info["key"]->str() : zpt::generate_key() + std::to_string(time_t(nullptr)) + zpt::generate_key()); }
zpt::redis::ZList::ZList(zpt::json _options, std::string _conf_path) : __options(_options), __conn(nullptr) { try { std::string _bind((std::string)_options->getPath(_conf_path)["bind"]); std::string _address(_bind.substr(0, _bind.find(":"))); uint _port = std::stoi(_bind.substr(_bind.find(":") + 1)); this->connection(_options->getPath(_conf_path) + zpt::json{"host", _address, "port", _port}); } catch (std::exception& _e) { assertz(false, std::string("could not connect to Redis server: ") + _e.what(), 500, 0); } }
std::string zpt::rest::scopes::serialize(zpt::json _info) { assertz( _info->type() == zpt::JSObject && _info->obj()->size() != 0, "scope serialization failed: required at least one scope", 412, 0); std::string _scopes; for (auto _field : _info->obj()){ if (_scopes.length() != 0) { _scopes += std::string(","); } _scopes += _field.first + std::string("{") + ((std::string) _field.second) + std::string("}"); } return _scopes; }
int main(int o_argc, const string const* o_argv) { MyData _data; int _shmd; size_t _off ; const string SEM_NAME = "/posix_sem"; const string SHM_NAME = "/posix_shm"; /* Nowaday, memory is so cheap that I just do not care about the unused memory. * If is it too waste, then just compile with DYN_SEG_SIZE switch. **/ #ifndef DYN_SEG_SIZE const off_t SEG_SIZE = sizeof(mydata_t); #else off_t _segSz = sizeof(size_t); for(size_t i = 1; i < o_argc; i++) { _segSz += strlen(o_argv[i]) + 1; } #endif /* DYN_SEG_SIZE */ /* open/create semaphore and lock */ sem_t* _sem = sem_open(SEM_NAME, O_CREAT, S_IRUSR | S_IWUSR, 1); assertv(_sem, SEM_FAILED); assertz( sem_wait(_sem) ); /* Critical section: */ /* there is no arguments so print shared memory object content */ if(o_argc == 1) { /* obtain the descriptor of shared memory object */ asserts( _shmd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR | S_IWUSR) ); /* map shared memory object into a virtual address */ #ifdef DYN_SEG_SIZE struct stat _buf; assertz( fstat(_shmd, &_buf) ); assertv(_data = mmap(NULL, _segSz = _buf.st_size, PROT_READ, MAP_SHARED, _shmd, 0), MAP_FAILED); #else assertv(_data = mmap(NULL, SEG_SIZE, PROT_READ, MAP_SHARED, _shmd, 0), MAP_FAILED); #endif /* DYN_SEG_SIZE */ /* read from shared memory object */ _off = 0; for(size_t i = 0; i < _data->len; i++) { print(_data->vals + _off); print(" "); _off += strlen(_data->vals + _off) + 1; } println(""); } /* write arguments in the reveres order into shared memory object */ else { #ifdef ALLOW_CLEANUP int _ret; /* if shared memory object already exist obtain its id and destroy, otherwise do nothing */ if( o_argc == 2 && !strcmp(o_argv[1], "cleanup") ) { _ret = shm_unlink(SHM_NAME); if(_ret == -1 && errno != ENOENT) asserts(_ret); /* destroy the semaphore before exit */ _ret = sem_unlink(SEM_NAME); if(_ret == -1 && errno != ENOENT) asserts(_ret); exit(EXIT_SUCCESS); } #endif /* ALLOW_CLEANUP */ /* use existing shared memory object or create the new one */ asserts( _shmd = shm_open(SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR) ); /* allocate a space for the maximum line length */ #ifdef DYN_SEG_SIZE assertz( ftruncate(_shmd, _segSz) ); #else assertz( ftruncate(_shmd, SEG_SIZE) ); #endif /* DYN_SEG_SIZE */ /* map shared memory object into virtual address */ #ifdef DYN_SEG_SIZE assertv(_data = mmap(NULL, _segSz, PROT_READ | PROT_WRITE, MAP_SHARED, _shmd, 0), MAP_FAILED); #else assertv(_data = mmap(NULL, SEG_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, _shmd, 0), MAP_FAILED); #endif /* DYN_SEG_SIZE */ /* write into the shared memory object */ _data->len = o_argc - 1; _off = 0; for(size_t i = o_argc - 1; i > 0; i--) { /* it is safe to use strcpy, because we got enought memory */ strcpy(_data->vals + _off, o_argv[i]); _off += strlen(o_argv[i]) + 1; } } /* unmap shared memory object from virtual address space */ #ifdef DYN_SEG_SIZE assertz( munmap(_data, _segSz) ); #else assertz( munmap(_data, SEG_SIZE) ); #endif /* DYN_SEG_SIZE */ _data = NULL; /* close the unused descriptor of shared memory object */ assertz( close(_shmd) ); /* unlock the semaphore */ assertz( sem_post(_sem) ); exit(EXIT_SUCCESS); }
bool zpt::RESTServer::route_http(zpt::socketstream_ptr _cs) { zpt::http::rep _reply; _reply->status((zpt::HTTPStatus) 200); zpt::http::req _request; try { (*_cs) >> _request; } catch(zpt::SyntaxErrorException& _e) { assertz(false, "error parsing HTTP data", 500, 0); } bool _return = false; bool _api_found = false; std::string _prefix(_request->url()); if (this->__options["directory"]->ok()) { for (auto _api : this->__options["directory"]->obj()) { bool _endpoint_found = false; for (auto _endpoint : _api.second["endpoints"]->arr()) { if (_prefix.find(_endpoint->str()) == 0) { _endpoint_found = true; _api_found = true; short _type = zpt::str2type(_api.second["type"]->str()); zpt::json _in = zpt::rest::http2zmq(_request); switch(_type) { case ZMQ_REP : case ZMQ_REQ : case ZMQ_ROUTER_DEALER : { zpt::socket _client = this->__poll->bind(ZMQ_ASSYNC_REQ, _api.second["connect"]->str()); _client->relay_for(_cs, [] (zpt::json _envelope) -> std::string { return std::string(zpt::rest::zmq2http(_envelope)); } ); _client->send(_in); _return = false; break; } case ZMQ_PUB_SUB : { std::string _connect = _api.second["connect"]->str(); zpt::socket _client = this->__poll->bind(ZMQ_PUB, _connect.substr(0, _connect.find(","))); _client->send(_in); zpt::http::rep _reply = zpt::rest::zmq2http(zpt::rest::accepted(_prefix)); (*_cs) << _reply << flush; _return = true; _client->unbind(); break; } case ZMQ_PUSH : { zpt::socket _client = this->__poll->bind(ZMQ_PUSH, _api.second["connect"]->str()); _client->send(_in); zpt::http::rep _reply = zpt::rest::zmq2http(zpt::rest::accepted(_prefix)); (*_cs) << _reply << flush; _return = true; _client->unbind(); break; } } break; } } if (_endpoint_found) { break; } } } if (!_api_found) { zlog(string("-> | \033[33;40m") + zpt::method_names[_request->method()] + string("\033[0m ") + _request->url(), zpt::info); zlog(string("<- | \033[31;40m404\033[0m"), zpt::info); zpt::http::rep _reply = zpt::rest::zmq2http(zpt::rest::not_found(_prefix)); (*_cs) << _reply << flush; _return = true; } return _return; }
zpt::RESTServer::RESTServer(std::string _name, zpt::json _options) : __name(_name), __emitter( new zpt::RESTEmitter(_options)), __poll(new zpt::ZMQPoll(_options, __emitter)), __options(_options) { assertz(this->__options["zmq"]->ok() && this->__options["zmq"]->type() == zpt::JSArray && this->__options["zmq"]->arr()->size() != 0, "zmq settings (bind, type) must be provided in the configuration file", 500, 0); ((zpt::RESTEmitter*) this->__emitter.get())->poll(this->__poll); for (auto _definition : this->__options["zmq"]->arr()) { short int _type = zpt::str2type(_definition["type"]->str()); switch(_type) { case ZMQ_ROUTER_DEALER : { this->__router_dealer.push_back(this->__poll->bind(ZMQ_ROUTER_DEALER, _definition["bind"]->str())); break; } case ZMQ_PUB_SUB : { this->__pub_sub.push_back(this->__poll->bind(ZMQ_XPUB_XSUB, _definition["bind"]->str())); /*std::string _connect(_definition["bind"]->str()); zpt::replace(_connect, "*", ((std::string) this->__options["host"])); this->__poll->bind(ZMQ_PUB_SUB, _connect)->in().setsockopt(ZMQ_SUBSCRIBE, "/", 1);*/ break; } default : { this->__poll->bind(_type, _definition["bind"]->str()); } } } if (this->__options["rest"]["modules"]->ok()) { for (auto _i : this->__options["rest"]["modules"]->arr()) { std::string _lib_file("lib"); _lib_file.append((std::string) _i); _lib_file.append(".so"); if (_lib_file.length() > 6) { zlog(std::string("loading module '") + _lib_file + std::string("'"), zpt::notice); void *hndl = dlopen(_lib_file.data(), RTLD_NOW); if (hndl == nullptr) { zlog(std::string(dlerror()), zpt::error); } else { void (*_populate)(zpt::ev::emitter); _populate = (void (*)(zpt::ev::emitter)) dlsym(hndl, "restify"); _populate(this->__emitter); } } } } if (!!this->__options["rest"]["uploads"]["upload_controller"]) { /* * definition of handlers for the file upload controller * registered as a Controller */ this->__emitter->on(zpt::ev::Post, zpt::rest::url_pattern({ this->__emitter->version(), "files" }), [] (zpt::ev::performative _performative, std::string _resource, zpt::json _payload, zpt::ev::emitter _pool) -> zpt::json { return zpt::undefined; } ); /* * definition of handlers for the file upload removal controller * registered as a Controller */ this->__emitter->on(zpt::ev::Delete, zpt::rest::url_pattern({ this->__emitter->version(), "files", "(.+)" }), [] (zpt::ev::performative _performative, std::string _resource, zpt::json _payload, zpt::ev::emitter _pool) -> zpt::json { return zpt::undefined; } ); } }
int main(int o_argc, const string const* o_argv) { string _path = getExecPath(*o_argv); key_t _key = ftok(_path, 'x'); asserts(_key, "ftok"); free(_path); _path = NULL; union semun _arg ; struct sembuf _buf ; int _shmid; MyData _data ; size_t _off ; /* Nowaday, memory is so cheap that I just do not care about the unused memory. * If is it too waste, then just compile with DYN_SEG_SIZE switch. **/ #ifndef DYN_SEG_SIZE const off_t SEG_SIZE = sizeof(mydata_t); #endif /* DYN_SEG_SIZE */ _buf.sem_num = 0; _buf.sem_flg = 0; /* Try to create a set of semaphores. */ int _semid = semget(_key, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); if(_semid == -1) { const int MAX_TRIES = 6; /* If semget failed, and the set does not exist then exit */ if(errno != EEXIST) asserts(_semid, "semget"); _semid = semget(_key, 1, S_IRUSR | S_IWUSR); asserts(_semid); struct semid_ds _ds; _arg.buf = &_ds; for(size_t i = 0; i < MAX_TRIES; i++) { asserts( semctl(_semid, 0, IPC_STAT, _arg) ); if(_ds.sem_otime != 0) break; sleep(5); } if(_ds.sem_otime == 0) fatal ( "The set of semaphores already exists, but it is not initialized.\n" "This is a permanent error, and I have given up." ) ; } /* init semaphore */ else { /* Note: * Some systems, like Linux, implicitly initializes a set of semaphores by value 0, * but unfortunately some does not. For that reason, this operation is necessary to ensure * a portability. **/ _arg.val = 0; asserts( semctl(_semid, 0, SETVAL, _arg) ); /* post semaphore */ _buf.sem_op = 1; asserts( semop(_semid, &_buf, 1) ); } /* lock the semaphore */ _buf.sem_op = -1; asserts( semop(_semid, &_buf, 1) ); /* Critical section: */ /* there is no arguments so print shared memory object content */ if(o_argc == 1) { /* obtain the descriptor of shared memory object */ asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR | SHM_RDONLY) ); /* map shared memory object into virtual address space */ assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1); /* read from shared memory object */ _off = 0; for(size_t i = 0; i < _data->len; i++) { print(_data->vals + _off); print(" "); _off += strlen(_data->vals + _off) + 1; } println(""); } /* write arguments in the reveres order into shared memory object */ else { #if (defined ALLOW_CLEANUP || defined DYN_SEG_SIZE) struct shmid_ds _shmds; #endif /* ALLOW_CLEANUP || DYN_SEG_SIZE */ #ifdef ALLOW_CLEANUP union semun _semun; /* if shared memory object already exist obtain its id and destroy, otherwise do nothing */ if( o_argc == 2 && !strcmp(o_argv[1], "cleanup") ) { _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR); if(_shmid == -1) { if(errno != ENOENT) asserts(_shmid); } else asserts( shmctl(_shmid, IPC_RMID, &_shmds) ); /* destroy the semaphore before exit */ asserts( semctl(_semid, 0, IPC_RMID, _semun) ); exit(EXIT_SUCCESS); } #endif /* ALLOW_CLEANUP */ /* use existing shared memory object or create the new one */ #ifdef DYN_SEG_SIZE off_t _segSz = sizeof(size_t); for(size_t i = 1; i < o_argc; i++) { _segSz += strlen(o_argv[i]) + 1; } /* Try to create a new shared memory object. * If such object already exits the destoy it before. **/ _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL); if(_shmid == -1) { if(errno == EEXIST) { asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR) ); asserts( shmctl(_shmid, IPC_RMID, &_shmds) ); asserts( _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT) ); } } #else asserts( _shmid = shmget(_key, SEG_SIZE, S_IRUSR | S_IWUSR | IPC_CREAT) ); #endif /* DYN_SEG_SIZE */ /* map shared memory object into virtual address space */ assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1); /* write into the shared memory object */ _data->len = o_argc - 1; _off = 0; for(size_t i = o_argc - 1; i > 0; i--) { /* it is safe to use strcpy, because we got enought memory */ strcpy(_data->vals + _off, o_argv[i]); _off += strlen(o_argv[i]) + 1; } } /* unmap shared memory object from virtual address space */ assertz( shmdt(_data) ); _data = NULL; /* unlock the semaphore */ _buf.sem_op = 1; asserts( semop(_semid, &_buf, 1) ); exit(EXIT_SUCCESS); }