Skip to content

Commit

Permalink
First commit so as to save work
Browse files Browse the repository at this point in the history
  • Loading branch information
samdealy committed Jun 22, 2022
1 parent c184c0c commit 0b33f46
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 178 deletions.
52 changes: 51 additions & 1 deletion fog/view/server/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
// Copyright (c) 2018-2022 The MobileCoin Foundation

use displaydoc::Display;
use grpcio::RpcStatus;
use mc_common::logger::Logger;
use mc_fog_view_enclave::Error as ViewEnclaveError;
use mc_sgx_report_cache_untrusted::Error as ReportCacheError;
use mc_util_grpc::{rpc_internal_error, rpc_permissions_error};

#[derive(Debug, Display)]
pub enum RouterServerError {
/// Error related to contacting Fog View Store: {0}
ViewStoreError(String),
/// View Enclave error: {0}
Enclave(ViewEnclaveError),
}

impl From<grpcio::Error> for RouterServerError {
fn from(src: grpcio::Error) -> Self {
RouterServerError::ViewStoreError(format!("{}", src))
}
}

impl From<mc_common::ResponderIdParseError> for RouterServerError {
fn from(src: mc_common::ResponderIdParseError) -> Self {
RouterServerError::ViewStoreError(format!("{}", src))
}
}

impl From<mc_util_uri::UriParseError> for RouterServerError {
fn from(src: mc_util_uri::UriParseError) -> Self {
RouterServerError::ViewStoreError(format!("{}", src))
}
}

pub fn router_server_err_to_rpc_status(
context: &str,
src: RouterServerError,
logger: Logger,
) -> RpcStatus {
match src {
RouterServerError::ViewStoreError(_) => {
rpc_internal_error(context, format!("{}", src), &logger)
}
RouterServerError::Enclave(_) => {
rpc_permissions_error(context, format!("{}", src), &logger)
}
}
}

impl From<ViewEnclaveError> for RouterServerError {
fn from(src: ViewEnclaveError) -> Self {
RouterServerError::Enclave(src)
}
}

#[derive(Debug, Display)]
pub enum ViewServerError {
Expand All @@ -26,4 +76,4 @@ impl From<ReportCacheError> for ViewServerError {
fn from(src: ReportCacheError) -> Self {
Self::ReportCache(src)
}
}
}
95 changes: 0 additions & 95 deletions fog/view/server/src/fog_view_router_server.rs
Original file line number Diff line number Diff line change
@@ -1,95 +0,0 @@
// Copyright (c) 2018-2022 The MobileCoin Foundation

//! Server object containing a view node
//! Constructible from config (for testability) and with a mechanism for
//! stopping it

use crate::{config::FogViewRouterConfig, fog_view_router_service::FogViewRouterService};
use futures::executor::block_on;
use mc_common::logger::{log, Logger};
use mc_fog_api::view_grpc;
use mc_fog_uri::{ConnectionUri, FogViewStoreUri};
use mc_fog_view_enclave::ViewEnclaveProxy;
use mc_util_grpc::{ConnectionUriGrpcioServer, ReadinessIndicator};
use std::sync::Arc;

pub struct FogViewRouterServer<E>
where
E: ViewEnclaveProxy,
{
server: grpcio::Server,
_enclave: E,
logger: Logger,
}

impl<E> FogViewRouterServer<E>
where
E: ViewEnclaveProxy,
{
/// Creates a new view router server instance
pub fn new(
config: FogViewRouterConfig,
enclave: E,
shards: Vec<FogViewStoreUri>,
logger: Logger,
) -> FogViewRouterServer<E> {
let readiness_indicator = ReadinessIndicator::default();

let env = Arc::new(
grpcio::EnvBuilder::new()
.name_prefix("Main-RPC".to_string())
.build(),
);

let fog_view_router_service = view_grpc::create_fog_view_router_api(
FogViewRouterService::new(enclave.clone(), shards, logger.clone()),
);
log::debug!(logger, "Constructed Fog View Router GRPC Service");

// Health check service
let health_service =
mc_util_grpc::HealthService::new(Some(readiness_indicator.into()), logger.clone())
.into_service();

// Package service into grpc server
log::info!(
logger,
"Starting Fog View Router server on {}",
config.client_listen_uri.addr(),
);
let server_builder = grpcio::ServerBuilder::new(env)
.register_service(fog_view_router_service)
.register_service(health_service)
.bind_using_uri(&config.client_listen_uri, logger.clone());

let server = server_builder.build().unwrap();

Self {
server,
_enclave: enclave,
logger,
}
}

/// Starts the server
pub fn start(&mut self) {
self.server.start();
for (host, port) in self.server.bind_addrs() {
log::info!(self.logger, "API listening on {}:{}", host, port);
}
}

/// Stops the server
pub fn stop(&mut self) {
block_on(self.server.shutdown()).expect("Could not stop grpc server");
}
}

impl<E> Drop for FogViewRouterServer<E>
where
E: ViewEnclaveProxy,
{
fn drop(&mut self) {
self.stop();
}
}
Loading

0 comments on commit 0b33f46

Please sign in to comment.