Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/ClientCommunicator.cs @ 1022

Last change on this file since 1022 was 1022, checked in by msteinbi, 15 years ago

Implementation of ClientCommunicator (#399)

File size: 5.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using HeuristicLab.Hive.Contracts.Interfaces;
7using HeuristicLab.Hive.Contracts;
8using HeuristicLab.Core;
9using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
10using System.Resources;
11using System.Reflection;
12using HeuristicLab.Hive.JobBase;
13
14namespace HeuristicLab.Hive.Server.Core {
15  /// <summary>
16  /// The ClientCommunicator manages the whole communication with the client
17  /// </summary>
18  public class ClientCommunicator: IClientCommunicator {
19    int nrOfJobs = 0;
20
21    IClientAdapter clientAdapter;
22    IJobAdapter jobAdapter;
23    IJobResultsAdapter jobResultAdapter;
24
25    public ClientCommunicator() {
26      clientAdapter = ServiceLocator.GetClientAdapter();
27      jobAdapter = ServiceLocator.GetJobAdapter();
28      jobResultAdapter = ServiceLocator.GetJobResultsAdapter();
29
30      for (int i = 0; i < nrOfJobs; i++) {
31        Job job = new Job();
32        job.Id = i;
33        job.State = State.offline;
34        jobAdapter.Update(job);
35      }
36
37    }
38
39    #region IClientCommunicator Members
40
41    public Response Login(ClientInfo clientInfo) {
42      Response response = new Response();
43
44      ICollection<ClientInfo> allClients = clientAdapter.GetAll();
45      ClientInfo client = clientAdapter.GetById(clientInfo.ClientId);
46      if (client != null && client.State != State.offline) {
47        response.Success = false;
48        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_USER_ALLREADY_ONLINE;
49        return response;
50      }
51      clientAdapter.Update(clientInfo);
52      response.Success = true;
53      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_SUCCESS;
54
55      return response;
56    }
57
58    public ResponseHB SendHeartBeat(HeartBeatData hbData) {
59      ResponseHB response = new ResponseHB();
60
61      response.Success = true;
62      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_HARDBEAT_RECEIVED;
63      response.ActionRequest = new List<MessageContainer>();
64      List<Job> allOfflineJobs = new List<Job>(jobAdapter.GetJobsByState(State.offline));
65      if (allOfflineJobs.Count > 0 && hbData.freeCores > 0)
66        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
67      else
68        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
69
70      return response;
71    }
72
73    public ResponseJob PullJob(Guid clientId) {
74      ResponseJob response = new ResponseJob();
75      lock (this) {
76        LinkedList<Job> allOfflineJobs = new LinkedList<Job>(jobAdapter.GetJobsByState(State.offline));
77        if (allOfflineJobs != null && allOfflineJobs.Count > 0) {
78          Job job2Calculate = allOfflineJobs.First.Value;
79          job2Calculate.State = State.calculating;
80          response.Job = job2Calculate;
81          jobAdapter.Update(job2Calculate);         
82          response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
83          response.Success = true;
84          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOB_PULLED;
85          return response;
86        }
87      }
88      response.Success = true;
89      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT;
90      return response;
91    }
92
93    public ResponseResultReceived SendJobResult(JobResult result, bool finished) {
94      ResponseResultReceived response = new ResponseResultReceived();
95      if (result.Id != 0) {
96        response.Success = false;
97        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_ID_MUST_NOT_BE_SET;
98        return response;
99      }
100      Job job = result.Job;
101      if (job == null) {
102        response.Success = false;
103        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JO_WITH_THIS_ID;
104        return response;
105      }
106      if (job.State != State.calculating) {
107        response.Success = false;
108        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_WRONG_JOB_STATE;
109        return response;
110      }
111      if (finished) {
112        job.State = State.finished;
113        jobAdapter.Update(job);
114
115        List<JobResult> jobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
116        foreach (JobResult currentResult in jobResults)
117          jobResultAdapter.Delete(currentResult);
118      }
119      jobResultAdapter.Update(result);   
120
121      response.Success = true;
122      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOBRESULT_RECEIVED;
123      response.Job = result.Job;
124
125      return response;
126    }
127                           
128    public Response Logout(Guid clientId) {
129      Response response = new Response();
130     
131      ClientInfo client = clientAdapter.GetById(clientId);
132      if (client == null) {
133        response.Success = false;
134        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_CLIENT_NOT_REGISTERED;
135        return response;
136      }
137      client.State = State.offline;
138      clientAdapter.Update(client);
139
140      response.Success = true;
141      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_SUCCESS;
142     
143      return response;
144    }
145
146    #endregion
147  }
148}
Note: See TracBrowser for help on using the repository browser.