Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1103 was 1103, checked in by svonolfe, 15 years ago

Changed SendJobResult Interface (#351)

File size: 8.0 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;
13using System.Runtime.CompilerServices;
14
15namespace HeuristicLab.Hive.Server.Core {
16  /// <summary>
17  /// The ClientCommunicator manages the whole communication with the client
18  /// </summary>
19  public class ClientCommunicator: IClientCommunicator {
20    int nrOfJobs = 0;
21    Dictionary<Guid, DateTime> lastHeartbeats =
22      new Dictionary<Guid,DateTime>();
23
24    IClientAdapter clientAdapter;
25    IJobAdapter jobAdapter;
26    IJobResultsAdapter jobResultAdapter;
27    ILifecycleManager lifecycleManager;
28
29    public ClientCommunicator() {
30      clientAdapter = ServiceLocator.GetClientAdapter();
31      jobAdapter = ServiceLocator.GetJobAdapter();
32      jobResultAdapter = ServiceLocator.GetJobResultsAdapter();
33      lifecycleManager = ServiceLocator.GetLifecycleManager();
34
35      lifecycleManager.OnServerHeartbeat +=
36        new EventHandler(lifecycleManager_OnServerHeartbeat);
37
38      for (int i = 0; i < nrOfJobs; i++) {
39        Job job = new Job();
40        job.Id = i;
41        job.State = State.offline;
42        jobAdapter.Update(job);
43      }
44      lastHeartbeats = new Dictionary<Guid, DateTime>();
45
46    }
47
48    [MethodImpl(MethodImplOptions.Synchronized)]
49    void lifecycleManager_OnServerHeartbeat(object sender, EventArgs e) {
50      List<ClientInfo> allClients = new List<ClientInfo>(clientAdapter.GetAll());
51      List<Job> allJobs = new List<Job>(jobAdapter.GetAll());
52
53      foreach (ClientInfo client in allClients) {
54        if (client.State != State.offline && client.State != State.nullState) {
55          if (!lastHeartbeats.ContainsKey(client.ClientId)) {
56            client.State = State.offline;
57            clientAdapter.Update(client);
58          } else {
59            DateTime lastHbOfClient = lastHeartbeats[client.ClientId];
60            TimeSpan dif = DateTime.Now.Subtract(lastHbOfClient);
61            Console.WriteLine(dif);
62          }
63        } else {
64          if (lastHeartbeats.ContainsKey(client.ClientId))
65            lastHeartbeats.Remove(client.ClientId);
66        }
67      }
68    }
69
70    #region IClientCommunicator Members
71
72    [MethodImpl(MethodImplOptions.Synchronized)]
73    public Response Login(ClientInfo clientInfo) {
74      Response response = new Response();
75
76      if (lastHeartbeats.ContainsKey(clientInfo.ClientId)) {
77        lastHeartbeats[clientInfo.ClientId] = DateTime.Now;
78      } else {
79        lastHeartbeats.Add(clientInfo.ClientId, DateTime.Now);
80      }
81
82      ICollection<ClientInfo> allClients = clientAdapter.GetAll();
83      ClientInfo client = clientAdapter.GetById(clientInfo.ClientId);
84      if (client != null && client.State != State.offline && client.State != State.nullState) {
85        response.Success = false;
86        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_USER_ALLREADY_ONLINE;
87        return response;
88      }
89      clientInfo.State = State.idle;
90      clientAdapter.Update(clientInfo);
91      response.Success = true;
92      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_SUCCESS;
93
94      return response;
95    }
96
97    [MethodImpl(MethodImplOptions.Synchronized)]
98    public ResponseHB SendHeartBeat(HeartBeatData hbData) {
99      ResponseHB response = new ResponseHB();
100
101      response.ActionRequest = new List<MessageContainer>();
102      if (clientAdapter.GetById(hbData.ClientId).State == State.offline ||
103          clientAdapter.GetById(hbData.ClientId).State == State.nullState) {
104        response.Success = false;
105        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_USER_NOT_LOGGED_IN;
106        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
107        return response;
108      }
109
110      if (lastHeartbeats.ContainsKey(hbData.ClientId)) {
111        lastHeartbeats[hbData.ClientId] = DateTime.Now;
112      } else {
113        lastHeartbeats.Add(hbData.ClientId, DateTime.Now);
114      }
115
116      response.Success = true;
117      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_HARDBEAT_RECEIVED;
118      List<Job> allOfflineJobs = new List<Job>(jobAdapter.GetJobsByState(State.offline));
119      if (allOfflineJobs.Count > 0 && hbData.freeCores > 0)
120        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
121      else
122        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
123
124      return response;
125    }
126
127    [MethodImpl(MethodImplOptions.Synchronized)]
128    public ResponseJob PullJob(Guid clientId) {
129      ResponseJob response = new ResponseJob();
130      lock (this) {
131        LinkedList<Job> allOfflineJobs = new LinkedList<Job>(jobAdapter.GetJobsByState(State.offline));
132        if (allOfflineJobs != null && allOfflineJobs.Count > 0) {
133          Job job2Calculate = allOfflineJobs.First.Value;
134          job2Calculate.State = State.calculating;
135          response.Job = job2Calculate;
136          jobAdapter.Update(job2Calculate);         
137          response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
138          response.Success = true;
139          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOB_PULLED;
140          return response;
141        }
142      }
143      response.Success = true;
144      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT;
145      return response;
146    }
147
148    [MethodImpl(MethodImplOptions.Synchronized)]
149    public ResponseResultReceived SendJobResult(Guid clientId,
150      long jobId,
151      byte[] result,
152      Exception exception, 
153      bool finished) {
154      ResponseResultReceived response = new ResponseResultReceived();
155      ClientInfo client =
156        clientAdapter.GetById(clientId);
157
158      Job job =
159        jobAdapter.GetById(jobId);
160      if (job == null) {
161        response.Success = false;
162        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JO_WITH_THIS_ID;
163        return response;
164      }
165      if (job.State != State.calculating) {
166        response.Success = false;
167        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_WRONG_JOB_STATE;
168        return response;
169      }
170      if (finished) {
171        job.State = State.finished;
172        jobAdapter.Update(job);
173
174        List<JobResult> jobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
175        foreach (JobResult currentResult in jobResults)
176          jobResultAdapter.Delete(currentResult);
177      }
178
179      JobResult jobResult =
180        new JobResult();
181      jobResult.Client = client;
182      jobResult.Job = job;
183      jobResult.Result = result;
184      jobResult.Exception = exception;
185
186      jobResultAdapter.Update(jobResult);   
187
188      response.Success = true;
189      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOBRESULT_RECEIVED;
190      response.JobId = jobId;
191
192      return response;
193    }
194
195    [MethodImpl(MethodImplOptions.Synchronized)]                       
196    public Response Logout(Guid clientId) {
197      Response response = new Response();
198
199      if (lastHeartbeats.ContainsKey(clientId))
200        lastHeartbeats.Remove(clientId);
201
202      ClientInfo client = clientAdapter.GetById(clientId);
203      if (client == null) {
204        response.Success = false;
205        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_CLIENT_NOT_REGISTERED;
206        return response;
207      }
208      client.State = State.offline;
209      clientAdapter.Update(client);
210
211      response.Success = true;
212      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_SUCCESS;
213     
214      return response;
215    }
216
217    #endregion
218  }
219}
Note: See TracBrowser for help on using the repository browser.