static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console(new RenderedCompactJsonFormatter()) .Enrich.FromLogContext() .CreateLogger(); // See https://support.microsoft.com/en-gb/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s // Experimentation shows we need the ThreadPool to always spin up threads for good performance under load ThreadPool.GetMaxThreads(out var workerThreads, out var ioThreads); ThreadPool.SetMinThreads(workerThreads, ioThreads); Parser.Default.ParseArguments <GatewayArgs>(args) .WithParsed(parsedArgs => { var spatialRefreshToken = Secrets.GetEnvSecret(SpatialRefreshTokenEnvironmentVariable); var memoryStoreClientManager = new RedisClientManager(parsedArgs.RedisConnectionString); var playerAuthClient = PlayerAuthServiceClient.Create( credentials: new PlatformRefreshTokenCredential(spatialRefreshToken)); IAnalyticsSender analyticsSender = new AnalyticsSenderBuilder("gateway_gateway") .WithCommandLineArgs(parsedArgs) .With(new LogExceptionStrategy(Log.Logger)) .Build(); var server = GrpcBaseServer.Build(parsedArgs); server.AddInterceptor(new PlayerIdentityTokenValidatingInterceptor( playerAuthClient, memoryStoreClientManager.GetRawClient(Database.CACHE))); server.AddService( GatewayService.BindService(new GatewayServiceImpl(memoryStoreClientManager, playerAuthClient, analyticsSender))); var serverTask = Task.Run(() => server.Start()); var signalTask = Task.Run(() => UnixSignal.WaitAny(new[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM) })); Task.WaitAny(serverTask, signalTask); if (signalTask.IsCompleted) { Log.Information($"Received UNIX signal {signalTask.Result}"); Log.Information("Server shutting down..."); server.Shutdown(); serverTask.Wait(); Log.Information("Server stopped cleanly"); } else { /* The server task has completed; we can just exit. */ Log.Information("The Gateway server has stopped itself or encountered an unhandled exception."); } }); }
private static void Main() { try { // initialize logger log4net.Config.XmlConfigurator.Configure(); // create a RestfulDeviceService used to communicate with the DeviceHive cloud // insert your assigned DeviceHive service URL here using (var deviceService = new RestfulDeviceService("http://localhost/DeviceHive.API")) { // create a DeviceHive network where our gateway will reside var network = new Network("Gateway Sample Network", "A DeviceHive network for Gateway sample"); // create gateway service var gatewayService = new GatewayService(deviceService, network); // create connection to device through COM port and add it to the gateway // insert your COM port name here var serialPort = new SerialPort("COM3") { ReadTimeout = 1000, WriteTimeout = 1000 }; var serialPortConnection = new SerialPortBinaryConnection(serialPort); gatewayService.DeviceConnectionList.Add(serialPortConnection); // start gateway gatewayService.Start(); // wait for console key press and then dispose gateway service Console.WriteLine("Gateway is now running, press any key to stop..."); Console.ReadKey(); gatewayService.Stop(); } } catch (Exception ex) { // handle the error Console.WriteLine("Error: " + ex); Console.ReadKey(); } }
private static void Main() { try { // initialize logger log4net.Config.XmlConfigurator.Configure(); // create a RestfulDeviceService used to communicate with the DeviceHive cloud // insert your assigned DeviceHive service URL here using (var deviceService = new RestfulDeviceService("http://localhost/DeviceHive.API")) { // create a DeviceHive network where our gateway will reside var network = new Network("Gateway Sample Network", "A DeviceHive network for Gateway sample"); // create gateway service var gatewayService = new GatewayService(deviceService, network); // create connection to device through COM port and add it to the gateway // insert your COM port name here var serialPort = new SerialPort("COM3") {ReadTimeout = 1000, WriteTimeout = 1000, BaudRate = 9600}; var serialPortConnection = new SerialPortBinaryConnection(serialPort); gatewayService.DeviceConnectionList.Add(serialPortConnection); // start gateway gatewayService.Start(); // wait for console key press and then dispose gateway service Console.WriteLine("Gateway is now running, press any key to stop..."); Console.ReadKey(); gatewayService.Stop(); } } catch (Exception ex) { // handle the error Console.WriteLine("Error: " + ex); Console.ReadKey(); } }