int NodeDispatcherTask::run(INodeConnectionMgr* man,IClientConnFactory* fact , int node, const std::string &n, boost::shared_ptr<TTransport> buffer) { TMemoryBuffer *mb = (TMemoryBuffer *)buffer.get(); uint8_t *b; uint32_t size; mb->getBuffer(&b, &size); return man->send(node, n, -1, (char *)b, size); }
static std::string ThriftToString(const ThriftStruct& ts) { using namespace apache::thrift::transport; using namespace apache::thrift::protocol; TMemoryBuffer* buffer = new TMemoryBuffer; boost::shared_ptr<TTransport> trans(buffer); TBinaryProtocol protocol(trans); ts.write(&protocol); uint8_t* buf; uint32_t size; buffer->getBuffer(&buf, &size); return std::string((char*)buf, (unsigned int)size); }
virtual int process(const std::string &name, int seq, boost::shared_ptr<apache::thrift::protocol::TProtocol> iprop, boost::shared_ptr<apache::thrift::protocol::TProtocol> oprop){ int32_t wuliao = 0; TMemoryBuffer *mb = new TMemoryBuffer(); boost::shared_ptr<TTransport> wr_trans(mb); boost::shared_ptr<TProtocol> rp(new TCustomProtocol(wr_trans)); rp->writeMessageBegin(name, T_CALL, seq); boost::shared_ptr<TProtocol> tp(new TProtocolTap(iprop, rp)); process_fn(tp.get(), oprop.get(), name, seq, wuliao); uint8_t *wbuf; uint32_t length; mb->getBuffer(&wbuf, &length); std::string wv((char *)wbuf, length); updated.push_back(wv); return 0; }
int main(int argc, char* argv[]) { AsyncInit init; init.asyncStageSize = 2; std::string sip = argv[1]; int port = atoi(argv[2]); signal(SIGINT, handle_sigint); // Catch SIGINT to clean up signal(SIGQUIT, handle_sigint); // Catch SIGQUIT to clean up rl_readline_name = "dao_tool"; rl_attempted_completion_function= (rl_completion_func_t*)&new_mysql_completion; rl_completion_entry_function= (rl_compentry_func_t*)&no_completion; if (getenv("HOME")) { sprintf(histfile,"%s/.daotool_history",getenv("HOME")); sprintf(histfile_tmp,"%s/.daotool_history.tmp",getenv("HOME")); read_history(histfile); } DummyClient dc; ReqProcessor rp(&dc); grp = &rp; rl_event_hook = rl_event; IAsyncCenter *center = initAsyncCenter(init); IAsyncContextMgr *ctxMgr = center->createAsyncContextMgr(); sleep(1); TRowTransportFactory *to = new TRowTransportFactory(); to->init(center->getClientConnFactory(), ctxMgr->getHandler(), sip, port); to->setMainThreadQueue(center->getMainQueue()); AsyncDaoFactory adf; adf.setAsyncContextMgr(ctxMgr); adf.setProtocolFactory(boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory)); adf.setTransportFactory(boost::shared_ptr<TTransportFactory>(to)); adf.init(); DaoConsoleFactory daoFac(&adf); if (!daoFac.startup()) { return 1; } ctxMgr->startAsync(&rp, -1); daoFac.getCommands(command_list); command_list.push_back("aton"); command_list.push_back("bson2String"); command_list.push_back("help"); std::sort(command_list.begin(), command_list.end()); SysConsoleAsync *pp = (SysConsoleAsync *)adf.getByName("SysConsoleAsync"); string service; int res = pp->queryServiceName(NULL, service); if(res != 0){ printf("---------Warning:load service name error---------\n"); fflush(stdout); } char *buffer; Driver parser; while((buffer = readline(">"))){ for(int i = 0; i < (int)rp.updated.size(); ++i){ printf("--------------update begin %d----------------\n", i); printf("%s\n", rp.updated[i].c_str()); printf("--------------update end %d----------------\n", i); } rp.updated.clear(); if(strlen(buffer) == 0){ continue; } if(!parser.parse_string(buffer)){ printf("parse error:%s\n>", parser.lasterr.c_str()); continue; } if(not_in_history(buffer)){ add_history(buffer); } ICommandLine *command = parser.ret; std::string str = command->getCommand(); if (str == "help") { TParam *p = command->getArg(0); std::string s; if(p){ s = ((TString *)p)->value; } std::string ret = daoFac.dumpUsage(service); printf("service interfaces:\n"); printf("%s", ret.c_str()); printf("utility interfaces:\n"); printf("\tstd::string bson2String(const std::string &input)\n"); printf("\tint aton(std::string &ip)\n"); printf("usage:\n"); printf("\tService.Function args\n"); printf("arg syntax: \n"); printf("\targs: args arg \n"); printf("\targ: value or list or map or object\n"); printf("\tvalue: int or \"string\" \n"); printf("\tlist: [value, value...] \n"); printf("\tmap: ((value, value), (value, value)...) \n"); printf("\tobject:{value, value....} \n"); continue; } if(str == "bson2string") { TParam *p = command->getArg(0); std::string input; if(p){ input = ((TString *)p)->value; std::string out = bson2String(input); printf("real string is :%s\n",out.c_str()); } continue; } if(str == "aton"){ TParam *p = command->getArg(0); if(p){ int ip = sox::aton_addr(((TString *)p)->value); printf("nip is:%d\n>", ip); } continue; } size_t s = str.find("."); str[s] = '_'; IDaoConsole* pDao = daoFac.getDaoConsole(str); if (pDao == NULL) { printf("interface<%s> is not found\n",dt_srvName.c_str()); continue; } boost::shared_ptr<TTransport> wr_trans(new TMemoryBuffer()); TCustomProtocol wr_prot(wr_trans); TProtocol *oprot = &wr_prot; int ret = 0; try{ ret = pDao->execute(command, oprot); }catch(std::exception &ex){ printf("exception:%s\n", ex.what()); continue; } TMemoryBuffer *wmb = (TMemoryBuffer *)wr_trans.get(); uint8_t *wbuf; uint32_t length; wmb->getBuffer(&wbuf, &length); std::string wv((char *)wbuf, length); printf("%s \n", wv.c_str()); printf("resCode = %d\n", ret); } printf("end------------\n"); if (!write_history(histfile_tmp)){ rename(histfile_tmp, histfile); } return 0; }