Exemplo n.º 1
0
        static void Main(string[] args)
        {
            AuthMethods    authMethods = new AuthMethods();
            IChannelServer host        = ChannelServerBuilder.CreateServer();

            host.LoadAssemblies(AppDomain.CurrentDomain, null);
            host.AddTokenAuthentication(authMethods.AuthenticateToken);
            host.StartHosting(null);

            Console.ReadLine();
        }
Exemplo n.º 2
0
        public ServerManager(IServiceLocator services, IChannelServer server)
        {
            _services = services ?? throw new ArgumentNullException(nameof(services));
            _server   = server ?? throw new ArgumentNullException(nameof(server));

            _writer         = _services.Get <IConsoleWriter>();
            _reader         = _services.Get <IConsoleReader>();
            _commandFactory = _services.Get <IServerCommandFactory>();
            _commandResults = _services.Get <ICommandExecutionResults>();

            InitCommands();
        }
        static void Main(string[] args)
        {
            AuthMethods    authMethods = new AuthMethods();
            IChannelServer host        = ChannelServerBuilder.CreateServer();
            //host.LoadAssemblies(AppDomain.CurrentDomain, null);
            List <string> asm = new List <string>
            {
                "Nuclear.Channels.Monolithic.Tests"
            };

            host.RegisterChannels(asm);
            host.AddTokenAuthentication(authMethods.AuthenticateToken);
            //host.ConfigureCacheCleaner(TimeSpan.FromSeconds(30));
            host.StartHosting(null);

            Console.ReadLine();
        }
Exemplo n.º 4
0
        public static IApplicationBuilder UseChannels(this IApplicationBuilder app)
        {
            IChannelServer server = ChannelServerBuilder.CreateServer();

            //If you have channels or services in another project add them into your current AppDomain
            //Same goes for services if you use IServiceLocator from Nuclear.ExportLocator package
            server.LoadAssemblies(AppDomain.CurrentDomain);

            //Add authentication if you have for basic or for token authentication
            //server.AddTokenAuthentication( // your delegate goes here );
            //server.AddBasicAuthentication( // your delegate goes here );

            //or setup server.AuthenticationOption

            //Change the route for your web url
            server.StartHosting("https://localhost:44386");

            return(app);
        }
 public InitServerCommand(IChannelServer server)
 {
     _server = server;
 }
Exemplo n.º 6
0
 public ChannelWebServer(IServiceLocator services, IChannelServer server)
 {
     _services   = services;
     _server     = server;
     _serverCopy = _server;
 }
Exemplo n.º 7
0
        public async Task <MazeChannel> InitializeChannel(ActionContext actionContext, IChannelServer channelServer)
        {
            var channel = (MazeChannel)ObjectFactory.Invoke(actionContext.Context.RequestServices, new object[0]);

            channel.MazeContext = actionContext.Context;
            channel.ChannelId   = channelServer.RegisterChannel(channel);

            await _executor.Execute(_objectMethodExecutor, channel, new object[0], Metadata);

            return(channel);
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public async Task Execute(MazeContext context, IChannelServer channelServer)
        {
            _logger.LogDebug($"Resolve Maze path {context.Request.Path}");
            var result = _routeResolver.Resolve(context);

            if (!result.Success)
            {
                _logger.LogDebug("Path not found");
                await WriteError(context, BusinessErrors.Commander.RouteNotFound(context.Request.Path),
                                 StatusCodes.Status404NotFound);

                return;
            }

            _logger.LogDebug(
                $"Route resolved (package: {result.RouteDescription.PackageIdentity}). Get cached route info.");
            var route         = _routeCache.Routes[result.RouteDescription];
            var actionContext = new DefaultActionContext(context, route, result.Parameters.ToImmutableDictionary());

            _logger.LogDebug($"Invoke method {route.RouteMethod}");

            IActionResult actionResult;

            try
            {
                switch (route.RouteType)
                {
                case RouteType.Http:
                    actionResult = await route.ActionInvoker.Value.Invoke(actionContext);

                    break;

                case RouteType.ChannelInit:
                    _logger.LogDebug("Create channel {channelName}", actionContext.Route.ControllerType.FullName);
                    var channel = await route.ActionInvoker.Value.InitializeChannel(actionContext, channelServer);

                    context.Response.Headers.Add(HeaderNames.Location, "ws://channels/" + channel.ChannelId);
                    actionResult = new StatusCodeResult(StatusCodes.Status201Created);
                    break;

                case RouteType.Channel:
                    var channelId = int.Parse(actionContext.Context.Request.Headers["ChannelId"]);
                    _logger.LogDebug("Request channel with id {channelId}", channelId);

                    var foundChannel = channelServer.GetChannel(channelId);
                    actionResult =
                        await route.ActionInvoker.Value.InvokeChannel(actionContext, (MazeChannel)foundChannel);

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }

                _logger.LogError(e,
                                 $"Error occurred when invoking method {route.RouteMethod} of package {result.RouteDescription.PackageIdentity} (path: {context.Request.Path})");
                await WriteError(context,
                                 BusinessErrors.Commander.ActionError(e.GetType().Name, route.RouteMethod.Name, e.Message),
                                 StatusCodes.Status500InternalServerError);

                return;
            }

            if (context.RequestAborted.IsCancellationRequested)
            {
                return;
            }

            try
            {
                await actionResult.ExecuteResultAsync(actionContext);
            }
            catch (Exception e)
            {
                if (context.RequestAborted.IsCancellationRequested)
                {
                    return;
                }

                _logger.LogError(e,
                                 $"Error occurred when executing action result {route.RouteMethod} of package {result.RouteDescription.PackageIdentity} (path: {context.Request.Path})");
                await WriteError(context,
                                 BusinessErrors.Commander.ResultExecutionError(e.GetType().Name, actionResult?.GetType().Name,
                                                                               e.Message), StatusCodes.Status500InternalServerError);

                return;
            }

            _logger.LogDebug("Request successfully executed.");
        }