Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implementation of ClientCommunicator (#399)

File size: 4.1 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;
12
13namespace HeuristicLab.Hive.Server.Core {
14  /// <summary>
15  /// The ClientCommunicator manages the whole communication with the client
16  /// </summary>
17  public class ClientCommunicator: IClientCommunicator {
18    int nrOfJobs = 1;
19
20    IClientAdapter clientAdapter;
21    IJobAdapter jobAdapter;
22
23    public ClientCommunicator() {
24      clientAdapter = ServiceLocator.GetClientAdapter();
25      jobAdapter = ServiceLocator.GetJobAdapter();
26
27      for (int i = 0; i < 10; i++) {
28        Job job = new Job();
29        job.JobId = i;
30        job.State = State.offline;
31        jobAdapter.UpdateJob(job);
32      }
33
34    }
35
36    #region IClientCommunicator Members
37
38    public Response Login(ClientInfo clientInfo) {
39      Response response = new Response();
40      response.Success = true;
41
42      ICollection<ClientInfo> allClients = clientAdapter.GetAllClients();
43      ClientInfo client = clientAdapter.GetClientById(clientInfo.ClientId);
44      if (client != null) {
45        if (client.State != State.offline) {
46          response.Success = false;
47          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_USER_ALLREADY_ONLINE;
48        }
49      }
50
51      if (response.Success) {
52        clientAdapter.UpdateClient(clientInfo);
53        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_SUCCESS;
54      }
55
56      return response;
57    }
58
59    public ResponseHB SendHeartBeat(HeartBeatData hbData) {
60      ResponseHB response = new ResponseHB();
61
62      response.Success = true;
63      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_HARDBEAT_RECEIVED;
64      response.ActionRequest = new List<MessageContainer>();
65      List<Job> allJobs = new List<Job>(jobAdapter.GetAllJobs());
66      if (allJobs.Count > 0 && hbData.freeCores > 0)
67        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
68      else
69        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
70
71      return response;
72    }
73
74    public ResponseJob PullJob(Guid clientId) {
75      ResponseJob response = new ResponseJob();
76      lock (this) {
77        LinkedList<Job> allJobs = new LinkedList<Job>(jobAdapter.GetAllJobs());
78        if (allJobs.Last != null) {
79          response.JobId = allJobs.Last.Value.JobId;
80          jobAdapter.DeleteJob(allJobs.Last.Value);   
81          response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
82          response.Success = true;
83          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOB_PULLED;
84          return response;
85        }
86      }
87      response.Success = true;
88      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT;
89      return response;
90    }
91
92    public ResponseResultReceived SendJobResult(JobResult Result, bool finished) {
93      ResponseResultReceived response = new ResponseResultReceived();
94      response.Success = true;
95      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOBRESULT_RECEIVED;
96      response.JobId = Result.JobId;
97
98      return response;
99    }
100                           
101    public Response Logout(Guid clientId) {
102      Response response = new Response();
103     
104      ClientInfo client = clientAdapter.GetClientById(clientId);
105      if (client == null) {
106        response.Success = false;
107        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_CLIENT_NOT_REGISTERED;
108        return response;
109      }
110      client.State = State.offline;
111      clientAdapter.UpdateClient(client);
112
113      response.Success = true;
114      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_SUCCESS;
115     
116      return response;
117    }
118
119    #endregion
120  }
121}
Note: See TracBrowser for help on using the repository browser.