示例#1
0
        public static void Start(
            string url,
            string identityUrl         = null,
            int threads                = 4,
            string staticFiles         = null,
            int nodes                  = 0,
            IEnumerable <Type> classes = null,
            IEnumerable <KeyValuePair <Guid, Type> > instances = null)
        {
            using (WebApp.Start(url, (builder) =>
            {
                if (staticFiles != null)
                {
                    if (!Directory.Exists(staticFiles))
                    {
                        throw new ArgumentException(staticFiles);
                    }

                    staticFiles = Path.GetFullPath(staticFiles);

                    var physicalFileSystem = new PhysicalFileSystem(staticFiles);
                    var options = new FileServerOptions
                    {
                        EnableDefaultFiles = true,
                        FileSystem = physicalFileSystem
                    };
                    options.StaticFileOptions.FileSystem = physicalFileSystem;
                    options.StaticFileOptions.ServeUnknownFileTypes = true;
                    options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };

                    builder.UseFileServer(options);
                }

                var distributed = null as IDistributedApp;
                builder.UseExcess(initializeApp: server =>
                {
                    distributed = server;

                    //setup
                    if (classes != null)
                    {
                        foreach (var @class in classes)
                        {
                            server.ConcurrentApp.RegisterClass(@class);
                        }
                    }

                    if (instances != null)
                    {
                        foreach (var instance in instances)
                        {
                            server.RegisterInstance(instance.Key, (IConcurrentObject)Activator.CreateInstance(instance.Value));
                        }
                    }

                    if (nodes > 0)
                    {
                        //start the identity server if we have any nodes
                        var error = null as Exception;
                        var waiter = new ManualResetEvent(false);

                        NetMQFunctions.StartServer(server, identityUrl,
                                                   expectedClients: nodes,
                                                   connected: ex =>
                        {
                            error = ex;
                            waiter.Set();
                        });

                        waiter.WaitOne(); //td: timeout
                        if (error != null)
                        {
                            throw error;
                        }
                    }
                });
            }))
            {
                Console.ReadKey();
            }
        }
示例#2
0
        public static void Start(
            string localServer,
            string remoteServer,
            int threads = 2,
            IEnumerable <Type> classes = null,
            IDictionary <Guid, IConcurrentObject> managedInstances = null)
        {
            var concurrentApp = new ThreadedConcurrentApp(new Dictionary <string, Func <IConcurrentApp, object[], IConcurrentObject> >());
            var app           = new DistributedApp(concurrentApp, localServer);

            if (classes != null)
            {
                foreach (var @class in classes)
                {
                    Guid id;
                    IConcurrentObject @object;

                    app.RegisterClass(@class);
                    if (isConcurrentSingleton(@class, out id, out @object))
                    {
                        if (managedInstances != null)
                        {
                            managedInstances[id] = @object;
                        }

                        app.RegisterInstance(id, @object);
                    }
                }
            }

            app.Connect = _ =>
            {
                var waiter  = new ManualResetEvent(false);
                var errors  = new List <Exception>();
                int waitFor = 2;

                NetMQFunctions.StartServer(app, localServer,
                                           connected: ex =>
                {
                    if (ex != null)
                    {
                        errors.Add(ex);
                    }
                    if (--waitFor == 0)
                    {
                        waiter.Set();
                    }
                });

                NetMQFunctions.StartClient(app, localServer, remoteServer, ex =>
                {
                    if (ex != null)
                    {
                        errors.Add(ex);
                    }
                    if (--waitFor == 0)
                    {
                        waiter.Set();
                    }
                });

                waiter.WaitOne();
                return(errors.Any()
                    ? new AggregateException(errors)
                    : null);
            };

            app.Start();
        }