Viam C++ SDK current
Loading...
Searching...
No Matches
server.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <grpcpp/impl/service_type.h>
7#include <grpcpp/security/server_credentials.h>
8#include <grpcpp/server_builder.h>
9
10#include <viam/sdk/resource/resource.hpp>
11#include <viam/sdk/resource/resource_api.hpp>
12#include <viam/sdk/resource/resource_server_base.hpp>
13
14namespace viam {
15
16// needed for later `friend` declaration.
17namespace sdktests {
18class TestServer;
19}
20
21namespace sdk {
22
25class Server {
26 public:
27 Server();
28 ~Server();
29
33 void start();
34
38 void register_service(grpc::Service* service);
39
43 std::shared_ptr<ResourceServer> lookup_resource_server(const API& api);
44
48 void add_resource(std::shared_ptr<Resource> resource);
49
54 void add_listening_port(const std::string& address,
55 std::shared_ptr<grpc::ServerCredentials> creds = nullptr);
56
58 void wait();
59
61 void shutdown();
62
63 // friend declaration to allow TestServer to access the private server_ field.
64 friend class ::viam::sdktests::TestServer;
65
66 private:
67 std::unordered_map<API, std::shared_ptr<ResourceServer>> managed_servers_;
68 std::unique_ptr<grpc::ServerBuilder> builder_;
69 std::unique_ptr<grpc::Server> server_;
70};
71
72} // namespace sdk
73} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Defines gRPC Server functionality.
Definition server.hpp:25
std::shared_ptr< ResourceServer > lookup_resource_server(const API &api)
Returns reference to managed resource server.
void start()
Starts the grpc server. Can only be called once.
void register_service(grpc::Service *service)
Registers a gRPC service.
void wait()
waits on server close, only returning when the server is closed.
void add_listening_port(const std::string &address, std::shared_ptr< grpc::ServerCredentials > creds=nullptr)
Adds a listening port to the server.
void add_resource(std::shared_ptr< Resource > resource)
Adds a specific managed resource to the associated resource server.
void shutdown()
Shutdown the gRPC server.