Skip to content

Commit

Permalink
Rename genesis_block_service.hpp/cpp -> genesis_block_server.hpp/cpp.…
Browse files Browse the repository at this point in the history
… Add GenesisBlockServerRunner
  • Loading branch information
motxx authored and muratovv committed Jul 30, 2017
1 parent 3835993 commit 1ebe7c7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

#include "genesis_block_service.hpp"
#include "genesis_block_server.hpp"
#include "model/converters/pb_block_factory.hpp"
#include "timer/timer.hpp"

namespace iroha {
grpc::Status GenesisBlockService::SendGenesisBlock(
Expand All @@ -27,6 +28,33 @@ namespace iroha {
auto success = processor_.genesis_block_handle(iroha_block);
response->set_applied(success ? iroha::protocol::APPLY_SUCCESS
: iroha::protocol::APPLY_FAILURE);

// GenesisBlockServer shuts down itself after sending response to iroha_cli.
timer::setAwkTimer(2000, [this] { server_runner_->shutdown(); });
return grpc::Status::OK;
}

/**
* runs genesis block server
* @param ip
* @param port
*/
void GenesisBlockServerRunner::run(const std::string& ip, const int port) {
std::string server_address(ip + ":" + std::to_string(port));
iroha::GenesisBlockService service(processor_, this);

grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server_ = builder.BuildAndStart();
std::cout << "GenesisBlockServer listening on " << server_address
<< std::endl;
server_->Wait();
}

/**
* shuts down server if received genesis block.
*/
void GenesisBlockServerRunner::shutdown() { server_->Shutdown(); }

} // namespace iroha
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,40 @@ namespace iroha {

constexpr int GenesisBlockServicePort = 50090;

class GenesisBlockServerRunner;

class GenesisBlockService final : public iroha::protocol::GenesisBlockService::Service {
public:
GenesisBlockService(GenesisBlockProcessor &processor)
: processor_(processor)
GenesisBlockService(GenesisBlockProcessor &processor, GenesisBlockServerRunner* server_runner)
: processor_(processor), server_runner_(server_runner)
{}

grpc::Status SendGenesisBlock(grpc::ServerContext* context,
const iroha::protocol::Block* request,
iroha::protocol::ApplyGenesisBlockResponse* response) override;
private:
GenesisBlockProcessor &processor_;
GenesisBlockServerRunner *server_runner_;
};

class GenesisBlockServerRunner {
public:
GenesisBlockServerRunner(iroha::GenesisBlockProcessor &processor)
: processor_(processor) {}
/**
* runs genesis block server
* @param ip
* @param port
*/
void run(const std::string &ip, const int port);

/**
* shuts down server if received genesis block.
*/
void shutdown();
private:
iroha::GenesisBlockProcessor &processor_;
std::unique_ptr<grpc::Server> server_;
};

}
Expand Down

0 comments on commit 1ebe7c7

Please sign in to comment.