std::string Collector::serve_request(std::string req) { Json::Value root; Json::Reader reader; if (reader.parse(req, root, false) == false) { return generate_error("bad_json"); } if (root["status"] == "have_work") { std::cout << "work: " << root["work"] << std::endl; /* Add the pixels to the pixmap. */ process_work(root["work"], root["pixels"]); Json::Value root; root["success"] = true; return json_to_string(root); } else if (root["status"] == "need_work") { /* We want to reduce network slowdown by only sending the scene when necessary. */ return generate_work(root["have_scene"].asBool()); } else { return generate_error("bad_request"); } }
int main() { int back_router = nn_socket(AF_SP_RAW, NN_REP); nn_bind(back_router, BACKROUTER); chan jobs = chmake(char*, 128); chan workers = chmake(char*, 64); go(collect(workers, back_router)); go(generate_work(jobs)); go(route_work(workers, back_router, jobs)); msleep(now() + 5435345); int front_router = nn_socket(AF_SP_RAW, NN_REP); nn_bind(front_router, FRONTROUTER); char ctrl [256]; char body [256]; char ctrl2 [256]; char body2 [256]; struct nn_msghdr hdr; struct nn_msghdr hdr2; struct nn_iovec iovec; iovec.iov_base = body; iovec.iov_len = sizeof(body); memset(&hdr, 0, sizeof (hdr)); hdr.msg_iov = &iovec; hdr.msg_iovlen = 1; hdr.msg_control = ctrl; hdr.msg_controllen = sizeof (ctrl); int rc = nn_recvmsg(back_router, &hdr, 0); printf("got rc: %d, got ctrl: %d\n", rc, hdr.msg_controllen); struct nn_iovec iovec2; iovec2.iov_base = body2; iovec2.iov_len = sizeof(body2); memset(&hdr2, 0, sizeof (hdr)); hdr2.msg_iov = &iovec2; hdr2.msg_iovlen = 1; hdr2.msg_control = ctrl2; hdr2.msg_controllen = sizeof (ctrl2); rc = nn_recvmsg(back_router, &hdr2, 0); /* struct nn_cmsghdr *cmsg; cmsg = NN_CMSG_FIRSTHDR (&hdr); while (cmsg != NULL) { size_t len = cmsg->cmsg_len - sizeof (struct nn_cmsghdr); printf ("level: %d property: %d length: %dB data: ", (int) cmsg->cmsg_level, (int) cmsg->cmsg_type, (int) len); unsigned char *data = NN_CMSG_DATA(cmsg); while (len) { printf ("%02X", *data); ++data; --len; } printf ("\n"); cmsg = NN_CMSG_NXTHDR (&hdr.msg_control, cmsg); } */ hdr.msg_iov = &iovec2; hdr.msg_iovlen = 1; nn_sendmsg(back_router, &hdr, 0); /* while(1){ sleep(10); buf = NULL; printf("waiting for worker\n"); int n = nn_recv(response, &buf, NN_MSG, 0); printf("server got msg: %.*s\n", n, buf); sprintf(tevs, "you are number %d served", ++c); printf("responding to %d\n",response); nn_send(response, tevs, strlen(tevs), 0); nn_freemsg(buf); } nn_shutdown(response, 0); */ return 0; }
int run_wallet (QApplication & application, int argc, char * const * argv, boost::filesystem::path const & data_path) { rai_qt::eventloop_processor processor; boost::filesystem::create_directories (data_path); QPixmap pixmap (":/logo.png"); QSplashScreen * splash = new QSplashScreen (pixmap); splash->show (); application.processEvents (); splash->showMessage (QSplashScreen::tr ("Remember - Back Up Your Wallet Seed"), Qt::AlignBottom | Qt::AlignHCenter, Qt::darkGray); application.processEvents (); qt_wallet_config config (data_path); auto config_path ((data_path / "config.json")); int result (0); std::fstream config_file; auto error (rai::fetch_object (config, config_path, config_file)); config_file.close (); if (!error) { boost::asio::io_service service; config.node.logging.init (data_path); std::shared_ptr<rai::node> node; std::shared_ptr<rai_qt::wallet> gui; rai::set_application_icon (application); auto opencl (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging)); rai::work_pool work (config.node.work_threads, opencl ? [&opencl](rai::uint256_union const & root_a) { return opencl->generate_work (root_a); } : std::function<boost::optional<uint64_t> (rai::uint256_union const &)> (nullptr)); rai::alarm alarm (service); rai::node_init init; node = std::make_shared<rai::node> (init, service, data_path, alarm, config.node, work); if (!init.error ()) { auto wallet (node->wallets.open (config.wallet)); if (wallet == nullptr) { auto existing (node->wallets.items.begin ()); if (existing != node->wallets.items.end ()) { wallet = existing->second; config.wallet = existing->first; } else { wallet = node->wallets.create (config.wallet); } } if (config.account.is_zero () || !wallet->exists (config.account)) { rai::transaction transaction (wallet->store.environment, nullptr, true); auto existing (wallet->store.begin (transaction)); if (existing != wallet->store.end ()) { rai::uint256_union account (existing->first.uint256 ()); config.account = account; } else { config.account = wallet->deterministic_insert (transaction); } } assert (wallet->exists (config.account)); update_config (config, config_path, config_file); node->start (); std::unique_ptr<rai::rpc> rpc = get_rpc (service, *node, config.rpc); if (rpc && config.rpc_enable) { rpc->start (); } rai::thread_runner runner (service, node->config.io_threads); QObject::connect (&application, &QApplication::aboutToQuit, [&]() { rpc->stop (); node->stop (); }); application.postEvent (&processor, new rai_qt::eventloop_event ([&]() { gui = std::make_shared<rai_qt::wallet> (application, processor, *node, wallet, config.account); splash->close (); gui->start (); gui->client_window->show (); })); result = application.exec (); runner.join (); } else { show_error ("Error initializing node"); } update_config (config, config_path, config_file); } else { show_error ("Error deserializing config"); } return result; }