Ejemplo n.º 1
0
        public static TestServer CreateServer(string code, string configuration, IDictionary <string, Guid> instances)
        {
            var errors         = new List <Diagnostic>();
            var configurations = new List <Type>();
            var assembly       = buildServer(code,
                                             errors: errors,
                                             configurations: configurations);

            if (errors.Any())
            {
                return(null);
            }

            var config = configurations
                         .Single(cfg => cfg.Name == configuration);

            var remoteTypes = null as IEnumerable <Type>;
            var clients     = 0;
            var startNodes  = false;

            if (config != null)
            {
                remoteTypes = (IEnumerable <Type>)config
                              .GetMethod("RemoteTypes")
                              .Invoke(null, new object[] { });

                clients = (int)config
                          .GetMethod("NodeCount")
                          .Invoke(null, new object[] { });

                startNodes = clients > 0;
            }

            var classes = new List <Type>();
            var app     = appFromAssembly(assembly,
                                          allInstances: instances,
                                          remoteTypes: remoteTypes,
                                          commonClasses: classes);

            app.Connect = _ =>
            {
                if (startNodes)
                {
                    Task.Run(() => config
                             .GetMethod("StartNodes")
                             .Invoke(null, new object[] { classes }));
                }

                var waiter = new ManualResetEvent(false);
                var error  = null as Exception;
                NetMQFunctions.StartServer(app, "tcp://localhost:5000", clients,
                                           connected: ex =>
                {
                    error = ex;
                    waiter.Set();
                });

                waiter.WaitOne();
                return(error);
            };

            return(TestServer.Create(builder => builder.UseExcess(app)));
        }