int main(int argc, char *argv[]){ google::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]); int32_t myid = FLAGS_serverid; int32_t num_clients = FLAGS_num_clients; VLOG(0) << "server started, parameters: " << "\n" << "server id = " << myid << "\n" << "number expected clients = " << num_clients; std::vector<petuum::ServerInfo> serverinfos = petuum::GetServerInfos(FLAGS_serverfile); petuum::ServerInfo myinfo; bool found = false; // based on my id, figure out my ip and port for(int i = 0; i < serverinfos.size(); ++i){ if (serverinfos[i].id_ == myid) { myinfo = serverinfos[i]; found = true; break; } } CHECK(found); VLOG(0) << "ip = " << myinfo.ip_ << "\n" << "port = " << myinfo.port_; // create zmq context zmq::context_t zmq_ctx(1); // CommHandler config parameters petuum::ConfigParam comm_config(myid, true, myinfo.ip_, myinfo.port_); petuum::CommHandler *comm = new petuum::CommHandler(comm_config); int suc = comm->Init(&zmq_ctx); CHECK_EQ(0, suc); petuum::ServerProxy<int> *proxy = new petuum::ServerProxy<int>(comm); bool is_namenode = (myid == serverinfos[0].id_); if (is_namenode) { suc = proxy->InitNameNode(serverinfos.size() - 1, num_clients); CHECK_EQ(0, suc) << "InitNameNode Failed"; } else { // Supply namenode ip and port # to initialize non-namenode. suc = proxy->InitNonNameNode(serverinfos[0].id_, serverinfos[0].ip_, serverinfos[0].port_, num_clients); CHECK_EQ(0, suc); } proxy->StartProxy(); VLOG(0) << "calling comm->ShutDown()"; comm->ShutDown(); //TODO(jinliang): where to shut down comm? delete comm; delete proxy; };
int main(int argc, char *argv[]) { zmq::context_t zmq_ctx(1); zmq::socket_t zmq_sub(zmq_ctx, ZMQ_SUB); zmq::message_t event; DAQ::FzEvent ev; std::string hostname, port; std::string url; // handling of command line parameters po::options_description desc("\nFazia Spy - allowed options"); desc.add_options() ("help", "produce help message") ("host", po::value<std::string>(), "FzDAQ publisher hostname to contact") ("port", po::value<std::string>(), "[optional] FzDAQ publisher port (default: 5563)") ("single", "[optional] single shot, dump first acquired event to screen") ("adc", "[optional] acquire only ADC from BLOCK") ("block", po::value<unsigned int>(), "BLOCK card number to spy (default: 0) (if specify only BLOCK a total view will be plotted)") ("fee", po::value<unsigned int>(), "FRONTEND card number to spy [0...7] (default: 0)") ("tel", po::value<unsigned int>(), "TELESCOPE to spy [0 => A , 1 => B] (default: 0)") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << "\n"; return 0; } if (vm.count("host")) { hostname = vm["host"].as<std::string>(); } else { std::cout << "ERROR: host missing" << std::endl; return -1; } if (vm.count("port")) { port = vm["port"].as<std::string>(); } else { port = "5563"; } url = "tcp://" + hostname + ":" + port; std::cout << "connect to: " << url << std::endl; try { zmq_sub.connect(url.c_str()); } catch(zmq::error_t zmq_err) { std::cout << "ERROR: connection failed - " << zmq_err.what() << std::endl; return -1; } zmq_sub.setsockopt(ZMQ_SUBSCRIBE, "", 0); if (vm.count("single")) { zmq_sub.recv(&event); if(ev.ParseFromArray(event.data(), event.size()) == true) { std::cout << "parsing succedeed ! - event size = " << event.size() << std::endl; dumpOnScreen(&ev); } return 0; } if(vm.count("block")) { nblk = vm["block"].as<unsigned int>(); } else { nblk = 0; } total = true; if(vm.count("adc")) { adc = true; total = false; } if(vm.count("fee")) { nfee = vm["fee"].as<unsigned int>(); if( (nfee > 7) || (nfee < 0) ) { std::cout << "ERROR: FRONTEND card number out of range" << std::endl; return -1; } total = false; } if(vm.count("tel")) { ntel = vm["tel"].as<unsigned int>(); if( (ntel > 1) || (ntel < 0) ) { std::cout << "ERROR: TELESCOPE number out of range" << std::endl; return -1; } total = false; } if(adc) std::cout << "starting FAZIA SPY GUI interface for: " << "B" << nblk << " ADC channel" << std::endl; else std::cout << "starting FAZIA SPY GUI interface for: " << "B" << nblk << "-" << "F" << nfee << "-" << "T" << ntel << std::endl; TApplication* rootapp = new TApplication("FAZIA SPY GUI", &argc, argv); //theCanvas is allocated for the life of the program: Double_t w = 1024; Double_t h = 768; theCanvas = new TCanvas("theCanvas", "the Canvas", w, h); theCanvas->SetWindowSize(w + (w - theCanvas->GetWw()), h + (h - theCanvas->GetWh())); if(total) { for(int i=0; i<8; i++) { // for each Frontend Card for(int j=0; j<2; j++) { // for each Telescope totHistograms[(i*12)+DAQ::FzData::QH1+(j*6)] = new TH1F("QH1", "QH1", 1024, 0, 1024); totHistograms[(i*12)+DAQ::FzData::I1+(j*6)] = new TH1F("I1", "I1", 500, 0, 500); totHistograms[(i*12)+DAQ::FzData::QL1+(j*6)] = new TH1F("QL1", "QL1", 1024, 0, 1024); totHistograms[(i*12)+DAQ::FzData::Q2+(j*6)] = new TH1F("Q2", "Q2", 1024, 0, 1024); totHistograms[(i*12)+DAQ::FzData::I2+(j*6)] = new TH1F("I2", "I2", 500, 0, 500); totHistograms[(i*12)+DAQ::FzData::Q3+(j*6)] = new TH1F("Q3", "Q3", 1024, 0, 1024); } } // create the sub pads: theCanvas->Divide(12, 8); // draw the histograms once for(int i=0; i<8; i++) { // for each Frontend Card for(int j=0; j<2; j++) { // for each Telescope for(int k=0; k<6; k++) { std::cout << "index = " << (i*12)+k+(j*6) << std::endl; theCanvas->cd((i*12)+k+(j*6)+1); totHistograms[(i*12)+k+(j*6)]->SetMaximum(8500); totHistograms[(i*12)+k+(j*6)]->SetMinimum(-8500); totHistograms[(i*12)+k+(j*6)]->Draw(); } } } } else { if(adc) { adcHistogram = new TH1F("ADC", "ADC", 150, 0, 150); adcHistogram->SetMaximum(8000); adcHistogram->SetMinimum(8000); phHistogram = new TH1F("Phase", "Phase", 100000, 0, 360); // create the sub pads: theCanvas->Divide(2, 1); theCanvas->cd(1); adcHistogram->Draw(); theCanvas->cd(2); phHistogram->Draw(); } else { // telHistograms[i] are allocated for the life of the program: telHistograms[DAQ::FzData::QH1] = new TH1F("QH1", "QH1", 1024, 0, 1024); telHistograms[DAQ::FzData::I1] = new TH1F("I1", "I1", 500, 0, 500); telHistograms[DAQ::FzData::QL1] = new TH1F("QL1", "QL1", 1024, 0, 1024); telHistograms[DAQ::FzData::Q2] = new TH1F("Q2", "Q2", 1024, 0, 1024); telHistograms[DAQ::FzData::I2] = new TH1F("I2", "I2", 500, 0, 500); telHistograms[DAQ::FzData::Q3] = new TH1F("Q3", "Q3", 1024, 0, 1024); // create the sub pads: theCanvas->Divide(2, 3); // draw the histograms once for (int i=0; i<TEL_HIST_NUM; i++) { theCanvas->cd(i+1); telHistograms[i]->SetMaximum(8500); telHistograms[i]->SetMinimum(-8500); telHistograms[i]->Draw(); } telHistograms[DAQ::FzData::QH1]->SetMaximum(8000); telHistograms[DAQ::FzData::QH1]->SetMinimum(-8000); telHistograms[DAQ::FzData::QL1]->SetMaximum(8000); telHistograms[DAQ::FzData::QL1]->SetMinimum(-8000); } } theCanvas->cd(0); theCanvas->Update(); // create two threads for filling and displsaying telHistograms[i]. if ((connecthistogramfill(&zmq_sub)) == 0) printf("successfully started thread to fill histogram..\n"); if ((connectdisplayupdate(NULL)) == 0) printf("successfully started thread to update the display..\n"); rootapp->Run(); return 0; // never reached }
static error forward_server_control_command( const std::string& _name, const std::string& _host, const std::string& _port_keyword, std::string& _output ) { if ( EMPTY_RESC_HOST == _host ) { return SUCCESS(); } int time_out = 0; error ret = get_server_property < int > ( CFG_SERVER_CONTROL_PLANE_TIMEOUT, time_out ); if ( !ret.ok() ) { return PASS( ret ); } int port = 0, num_hash_rounds = 0; std::string encryption_algorithm; buffer_crypt::array_t shared_secret; ret = get_server_properties( _port_keyword, port, num_hash_rounds, shared_secret, encryption_algorithm ); if ( !ret.ok() ) { return PASS( ret ); } // stringify the port std::stringstream port_sstr; port_sstr << port; // standard zmq rep-req communication pattern zmq::context_t zmq_ctx( 1 ); zmq::socket_t zmq_skt( zmq_ctx, ZMQ_REQ ); zmq_skt.setsockopt( ZMQ_RCVTIMEO, &time_out, sizeof( time_out ) ); zmq_skt.setsockopt( ZMQ_SNDTIMEO, &time_out, sizeof( time_out ) ); // this is the client so we connect rather than bind std::string conn_str( "tcp://" ); conn_str += _host; conn_str += ":"; conn_str += port_sstr.str(); try { zmq_skt.connect( conn_str.c_str() ); } catch ( zmq::error_t& e_ ) { std::string msg( "failed to connect to [" ); msg + conn_str + "]"; return ERROR( SYS_INVALID_INPUT_PARAM, msg ); } // build the command to forward control_plane_command cmd; cmd.command = _name; cmd.options[ SERVER_CONTROL_OPTION_KW ] = SERVER_CONTROL_HOSTS_OPT; cmd.options[ SERVER_CONTROL_HOST_KW ] = _host; // serialize using the generated avro class std::auto_ptr< avro::OutputStream > out = avro::memoryOutputStream(); avro::EncoderPtr e = avro::binaryEncoder(); e->init( *out ); avro::encode( *e, cmd ); boost::shared_ptr< std::vector< uint8_t > > data = avro::snapshot( *out ); buffer_crypt crypt( shared_secret.size(), // key size 0, // salt size ( we dont send a salt ) num_hash_rounds, // num hash rounds encryption_algorithm.c_str() ); buffer_crypt::array_t iv; buffer_crypt::array_t data_to_send; buffer_crypt::array_t data_to_encrypt( data->data(), data->data() + data->size() ); ret = crypt.encrypt( shared_secret, iv, data_to_encrypt, data_to_send ); if ( !ret.ok() ) { return PASS( ret ); } // copy binary encoding into a zmq message for transport zmq::message_t rep( data_to_send.size() ); memcpy( rep.data(), data_to_send.data(), data_to_send.size() ); zmq_skt.send( rep ); // wait for the server response zmq::message_t req; zmq_skt.recv( &req ); if ( 0 == req.size() ) { return ERROR( SYS_INVALID_INPUT_PARAM, "empty response string" ); } // decrypt the message before passing to avro buffer_crypt::array_t data_to_process; const uint8_t* data_ptr = static_cast< const uint8_t* >( req.data() ); buffer_crypt::array_t data_to_decrypt( data_ptr, data_ptr + req.size() ); ret = crypt.decrypt( shared_secret, iv, data_to_decrypt, data_to_process ); if ( !ret.ok() ) { irods::log( PASS( ret ) ); rodsLog( LOG_ERROR, "Failed to decrpyt [%s]", req.data() ); return PASS( ret ); } std::string rep_str( reinterpret_cast< char* >( data_to_process.data() ), data_to_process.size() ); if ( SERVER_CONTROL_SUCCESS != rep_str ) { // check if the result is really an error or a status if ( std::string::npos == rep_str.find( "[-]" ) ) { _output += rep_str; } else { return ERROR( CONTROL_PLANE_MESSAGE_ERROR, rep_str ); } } return SUCCESS(); } // forward_server_control_command
void server_control_executor::operator()() { int port = 0, num_hash_rounds = 0; buffer_crypt::array_t shared_secret; std::string encryption_algorithm; error ret = get_server_properties( port_prop_, port, num_hash_rounds, shared_secret, encryption_algorithm ); if ( !ret.ok() ) { irods::log( PASS( ret ) ); return; } if ( shared_secret.empty() || encryption_algorithm.empty() || 0 == port || 0 == num_hash_rounds ) { rodsLog( LOG_NOTICE, "control plane is not configured properly" ); return; } zmq::context_t zmq_ctx( 1 ); zmq::socket_t zmq_skt( zmq_ctx, ZMQ_REP ); int time_out = SERVER_CONTROL_POLLING_TIME_MILLI_SEC; zmq_skt.setsockopt( ZMQ_RCVTIMEO, &time_out, sizeof( time_out ) ); zmq_skt.setsockopt( ZMQ_SNDTIMEO, &time_out, sizeof( time_out ) ); std::stringstream port_sstr; port_sstr << port; std::string conn_str( "tcp://*:" ); conn_str += port_sstr.str(); zmq_skt.bind( conn_str.c_str() ); rodsLog( LOG_NOTICE, ">>> control plane :: listening on port %d\n", port ); server_state& s = server_state::instance(); while ( server_state::STOPPED != s() ) { zmq::message_t req; zmq_skt.recv( &req ); if ( 0 == req.size() ) { continue; } // process the message std::string output; std::string rep_msg( SERVER_CONTROL_SUCCESS ); error ret = process_operation( req, output ); if ( !ret.ok() ) { log( PASS( ret ) ); rep_msg = ret.result(); } else if ( !output.empty() ) { rep_msg = output; } buffer_crypt crypt( shared_secret.size(), // key size 0, // salt size ( we dont send a salt ) num_hash_rounds, // num hash rounds encryption_algorithm.c_str() ); buffer_crypt::array_t iv; buffer_crypt::array_t data_to_send; buffer_crypt::array_t data_to_encrypt( rep_msg.begin(), rep_msg.end() ); ret = crypt.encrypt( shared_secret, iv, data_to_encrypt, data_to_send ); if ( !ret.ok() ) { irods::log( PASS( ret ) ); } zmq::message_t rep( data_to_send.size() ); memcpy( rep.data(), data_to_send.data(), data_to_send.size() ); zmq_skt.send( rep ); } // while } // control operation