Exemplo n.º 1
0
        public static void StartServer(IDistributedApp app, string url, int expectedClients = 0, Action <Exception> connected = null)
        {
            Task.Run(() =>
            {
                using (var context = NetMQContext.Create())
                    using (var input = context.CreateDealerSocket())
                    {
                        Dictionary <string, DealerSocket> clients = null;
                        try
                        {
                            input.Bind(url);

                            if (expectedClients > 0)
                            {
                                clients = new Dictionary <string, DealerSocket>();
                                awaitClients(input, expectedClients, clients, context, app);

                                app.SendToClient = (client, msg) => writeMessage(clients[client], msg);
                            }

                            connected(null);
                        }
                        catch (Exception ex)
                        {
                            connected(ex);
                            return;
                        }

                        //loop
                        var message    = new NetMQMessage(expectedFrameCount: 7);
                        var appMessage = new DistributedAppMessage();
                        for (;;)
                        {
                            message = input.ReceiveMultipartMessage(expectedFrameCount: 7);

                            try
                            {
                                if (!readMessage(message, appMessage))
                                {
                                    continue;
                                }

                                app.Receive(appMessage);
                            }
                            catch
                            {
                                //td: log?
                            }
                            finally
                            {
                                message.Clear();
                            }
                        }
                    }
            });
        }
Exemplo n.º 2
0
        private static void handcheck(DealerSocket output, string url, IDistributedApp app)
        {
            var json = JObject.FromObject(new
            {
                Url       = url,
                Instances = app.Instances()
            });

            output.SendFrame(json.ToString());
        }
Exemplo n.º 3
0
 public ExcessOwinMiddleware(
     OwinMiddleware next,
     IDistributedApp server,
     __Scope scope,
     IEnumerable <Type> functions,
     IEnumerable <FilterFunction> filters) : base(next)
 {
     _server    = server;
     _scope     = scope;
     _functions = BuildFunctions(_scope, functions, filters);
 }
Exemplo n.º 4
0
        public static void FromAssemblies(
            IDistributedApp app,
            IEnumerable <Assembly> assemblies,
            IInstantiator instantiator,
            IEnumerable <string> except = null,
            IEnumerable <string> only   = null)
        {
            var classes   = new List <Type>();
            var instances = new Dictionary <Guid, Type>();

            foreach (var assembly in assemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (except != null && except.Contains(type.Name))
                    {
                        continue;
                    }

                    if (only != null && !only.Contains(type.Name))
                    {
                        continue;
                    }

                    var id = default(Guid);
                    var isConcurrentType = isConcurrent(type);
                    var isServiceType    = isService(type, out id);
                    if (isConcurrentType)
                    {
                        app.RegisterClass(type);
                    }

                    if (isServiceType)
                    {
                        app.RegisterInstance(id, (IConcurrentObject)instantiator.Create(type));
                    }

                    if (isServiceType || isConcurrentType)
                    {
                        continue;
                    }
                }
            }
        }
Exemplo n.º 5
0
        public static void StartClient(IDistributedApp app, string localServer, string remoteServer, Action <Exception> connected)
        {
            var writeQueue = new BlockingCollection <DistributedAppMessage>();

            app.Send = msg => writeQueue.Add(msg);

            Task.Run(() =>
            {
                using (var context = NetMQContext.Create())
                    using (var output = context.CreateDealerSocket())
                    {
                        try
                        {
                            output.Connect(remoteServer);
                        }
                        catch (Exception ex)
                        {
                            connected(ex);
                            return;
                        }

                        handcheck(output, localServer, app);

                        //notify
                        connected(null);

                        //loop
                        for (;;)
                        {
                            var toSend = writeQueue.Take();
                            writeMessage(output, toSend);

                            Debug.WriteLine(
                                $"Client.Send: {toSend.Id} ==> {toSend.Method}, {toSend.Data} => {toSend.RequestId}");
                        }
                    }
            });
        }
Exemplo n.º 6
0
 public static void UseExcess(this IAppBuilder builder, IDistributedApp server)
 {
     builder.Use <ExcessOwinMiddleware>(server);
     server.Start();
 }
Exemplo n.º 7
0
        private static void awaitClients(DealerSocket input, int clients, Dictionary <string, DealerSocket> result, NetMQContext context, IDistributedApp app)
        {
            while (result.Count < clients)
            {
                var clientCmd = input.ReceiveFrameString();
                var json      = JObject.Parse(clientCmd) as dynamic;

                string clientUrl = json.Url;

                //connect
                var socket = context.CreateDealerSocket();
                socket.Connect(clientUrl);
                result[clientUrl] = socket;

                //register instances
                var instances = (json.Instances as JArray)
                                .Select(val => Guid.Parse(val.ToString()));

                foreach (var id in instances)
                {
                    app.RemoteInstance(id, msg => writeMessage(socket, msg));
                }
            }
        }
Exemplo n.º 8
0
 public ExcessOwinMiddleware(OwinMiddleware next, IDistributedApp server) : base(next)
 {
     _server = server;
 }