Пример #1
0
        public Server(IPAddress serverIp)
        {
            Workers     = new List <WorkerInfo>();
            Clients     = new List <Uri>();
            TaskService = new ServerTaskService(this);
            ClientRegistrationService = new ClientRegistrationService(this);
            WorkerRegistrationService = new WorkerRegistrationService(this);
            ConnectionControl         = new ServerConnectionControl(this);
            TaskLibraryManager        = new TaskLibraryManager();
            TaskLibraryManager.Load();
            bool hostCreated = false;

            while (!hostCreated)
            {
                try
                {
                    ServerUri           = RemoteConnection.CreateUri(serverIp, Port++);
                    CheckConnectionHost = RemoteConnection.CreateHost(UriTemplates.CreateCheckConnectionUri(ServerUri), typeof(ICheckConnection), typeof(CheckConnection));
                    hostCreated         = true;
                }
                catch
                {
                }
            }
            TaskServiceHost        = RemoteConnection.CreateHost(UriTemplates.CreateTasksUri(ServerUri), typeof(IServerTaskService), TaskService);
            ClientRegistrationHost = RemoteConnection.CreateHost(UriTemplates.CreateClientRegistrationUri(ServerUri), typeof(IClientRegistrationService), ClientRegistrationService);
            WorkerRegistrationHost = RemoteConnection.CreateHost(UriTemplates.CreateWorkerRegistrationUri(ServerUri), typeof(IWorkerRegistrationService), WorkerRegistrationService);
            TaskLibraryHost        = RemoteConnection.CreateHost(UriTemplates.CreateLibrariesUri(ServerUri), typeof(IDistributedTaskLibraryService), typeof(DistributedTaskLibraryService));
            CheckConnectionHost    = RemoteConnection.CreateHost(UriTemplates.CreateCheckConnectionUri(ServerUri), typeof(ICheckConnection), typeof(CheckConnection));
            CheckConnectionHost.Open();
            WorkerRegistrationHost.Open();
            TaskLibraryHost.Open();
        }
 private async Task <DistributedResource> ExecuteAsync(Uri clientUri, Uri workerUri, DistributedResource resource, int taskId)
 {
     return(await Task.Run(() =>
     {
         DistributedResource result = null;
         try
         {
             var service = RemoteConnection.CreateProxy <IWorkerTaskService>(UriTemplates.CreateTasksUri(workerUri));
             result = service.ExecuteTask(taskId, resource);
         }
         catch
         {
             result = null;
         }
         result = Execute(clientUri, taskId, resource);
         return result;
     }));
 }
 public ClientTask CreateTask(String typename, List <string> filenames)
 {
     if (Disposed)
     {
         throw new NoServerConnectionException();
     }
     else
     {
         var libraries = new DistributedTaskLibrary(LocalUri, filenames, typename);
         var service   = RemoteConnection.CreateProxy <IServerTaskService>(UriTemplates.CreateTasksUri(ServerUri));
         int taskid    = 0;
         try
         {
             taskid = service.CreateTask(libraries);
         }
         catch
         {
             throw new ServerUnavailableException();
         }
         return(new ClientTask(taskid, ServerUri, LocalUri));
     }
 }
        public void Connect(Uri workerUri)
        {
            var workerInfo = new WorkerInfo(workerUri);
            var service    = RemoteConnection.CreateProxy <IWorkerTaskService>(UriTemplates.CreateTasksUri(workerUri));

            foreach (Uri client in Server.Clients)
            {
                workerInfo.Connections.Add(new ConnectionInfo(client, service.CheckConnection(client)));
            }
            for (int i = 0; i < Server.TaskLibraryManager.Tasks.Count; i++)
            {
                workerInfo.Tasks.Add(i, service.CreateTask(Server.TaskLibraryManager.Tasks[i].CreateDistributedTaskLibrary(Server.ServerUri)));
            }
            lock (Server.Workers)
            {
                if (Server.Workers.Count == 0)
                {
                    Server.OpenClientConnection();
                }
                Server.Workers.Add(workerInfo);
            }
            Server.ConnectionControl.AddWorkerConnection(workerUri);
            Logging.WriteLog("Worker on " + workerUri + " connected");
        }
        public WorkerServerConnection(IPAddress localIp, IPAddress serverIp, int port) : base(localIp, serverIp, port)
        {
            Logging.WriteLog("Connecting to server on " + ServerUri);
            PerformanceService     = new WorkerPerformanceService();
            PerformanceServiceHost = RemoteConnection.CreateHost(UriTemplates.CreatePerformanceUri(LocalUri), typeof(IWorkerPerformanceService), PerformanceService);
            PerformanceServiceHost.Open();
            TasksService     = new WorkerTaskService(LocalUri);
            TasksServiceHost = RemoteConnection.CreateHost(UriTemplates.CreateTasksUri(LocalUri), typeof(IWorkerTaskService), TasksService);
            TasksServiceHost.Open();
            var registation = RemoteConnection.CreateProxy <IWorkerRegistrationService>(UriTemplates.CreateWorkerRegistrationUri(ServerUri));

            try
            {
                registation.Connect(LocalUri);
            }
            catch (Exception e)
            {
                TasksServiceHost.Close();
                PerformanceServiceHost.Close();
                Dispose();
                throw new NoServerConnectionException();
            }
            ConnectionControl = new ConnectionControl(this, ServerUri);
        }
 public ClientTask(int id, Uri server, Uri client)
 {
     Id     = id;
     Client = client;
     Server = UriTemplates.CreateTasksUri(server);
 }
 public void Connect(Uri localuri)
 {
     lock (Server.Clients)
     {
         Server.Clients.Add(localuri);
     }
     lock (Server.Workers)
     {
         foreach (WorkerInfo workerinfo in Server.Workers)
         {
             if (!workerinfo.Connections.Select <ConnectionInfo, Uri>(connection => connection.ClientUri).Contains(localuri))
             {
                 var service = RemoteConnection.CreateProxy <IWorkerTaskService>(UriTemplates.CreateTasksUri(workerinfo.WorkerUri));
                 workerinfo.Connections.Add(new ConnectionInfo(localuri, service.CheckConnection(localuri)));
             }
         }
     }
     Server.ConnectionControl.AddClientConnection(localuri);
     Logging.WriteLog("Client on " + localuri + " connected");
 }
        public int CreateTask(DistributedTaskLibrary libraries)
        {
            Logging.WriteLog("Request for creating new library was received");
            int taskId = Server.TaskLibraryManager.CreateTask(libraries);
            DistributedTaskLibrary distributedLibrary = Server.TaskLibraryManager.Tasks[taskId].CreateDistributedTaskLibrary(Server.ServerUri);
            List <int>             list = new List <int>();

            lock (Server.Workers)
            {
                foreach (WorkerInfo workerInfo in Server.Workers)
                {
                    if (!workerInfo.Tasks.Keys.ToList().Contains(taskId))
                    {
                        var service = RemoteConnection.CreateProxy <IWorkerTaskService>(UriTemplates.CreateTasksUri(workerInfo.WorkerUri));
                        workerInfo.Tasks.Add(taskId, service.CreateTask(distributedLibrary));
                    }
                }
            }
            return(taskId);
        }
        public DistributedResource Execute(Uri clientUri, int taskId, DistributedResource resource)
        {
            Logging.WriteLog("Request for executing task on single resource was received");
            DistributedResource result = null;
            int trycount = TryCount;

            do
            {
                WorkerInfo workerInfo = null;
                lock (Server.Workers)
                {
                    List <int> performance = Server.Workers.Select <WorkerInfo, int>(worker => worker.GetPerformance(clientUri)).ToList();
                    if (performance.Count != 0)
                    {
                        workerInfo = Server.Workers[performance.FindIndex(perform => perform == performance.Max())];
                        var service = RemoteConnection.CreateProxy <IWorkerTaskService>(UriTemplates.CreateTasksUri(workerInfo.WorkerUri));
                        try
                        {
                            result = service.ExecuteTask(workerInfo.Tasks[taskId], resource);
                            trycount--;
                        }
                        catch
                        { }
                    }
                }
            }while (result == null && Server.Workers.Count != 0 && trycount > 0);
            return(result);
        }