Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 973 was 970, checked in by msteinbi, 16 years ago

Implementation of ClientCommunicator, heartbet and pulljob (#399)

File size: 3.9 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    }
28
29    #region IClientCommunicator Members
30
31    public Response Login(ClientInfo clientInfo) {
32      Response response = new Response();
33      response.Success = true;
34
35      ICollection<ClientInfo> allClients = clientAdapter.GetAllClients();
36      ClientInfo client = clientAdapter.GetClientById(clientInfo.ClientId);
37      if (client != null) {
38        if (client.State != State.offline) {
39          response.Success = false;
40          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_USER_ALLREADY_ONLINE;
41        }
42      }
43
44      if (response.Success) {
45        clientAdapter.UpdateClient(clientInfo);
46        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_SUCCESS;
47      }
48
49      return response;
50    }
51
52    public ResponseHB SendHeartBeat(HeartBeatData hbData) {
53      ResponseHB response = new ResponseHB();
54
55      response.Success = true;
56      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_HARDBEAT_RECEIVED;
57      response.ActionRequest = new List<MessageContainer>();
58      List<Job> allJobs = new List<Job>(jobAdapter.GetAllJobs());
59      if (allJobs.Count > 0)
60        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
61      else
62        response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
63
64      return response;
65    }
66
67    public ResponseJob PullJob(Guid clientId) {
68      ResponseJob response = new ResponseJob();
69      lock (this) {
70        LinkedList<Job> allJobs = new LinkedList<Job>(jobAdapter.GetAllJobs());
71        if (allJobs.Last != null) {
72          response.JobId = allJobs.Last.Value.JobId;
73          jobAdapter.DeleteJob(allJobs.Last.Value);   
74          response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
75          response.Success = true;
76          response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOB_PULLED;
77          return response;
78        }
79      }
80      response.Success = true;
81      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT;
82      return response;
83    }
84
85    public ResponseResultReceived SendJobResult(JobResult Result, bool finished) {
86      ResponseResultReceived response = new ResponseResultReceived();
87      response.Success = true;
88      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOBRESULT_RECEIVED;
89      response.JobId = Result.JobId;
90
91      return response;
92    }
93                           
94    public Response Logout(Guid clientId) {
95      Response response = new Response();
96     
97      ClientInfo client = clientAdapter.GetClientById(clientId);
98      if (client == null) {
99        response.Success = false;
100        response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_CLIENT_NOT_REGISTERED;
101        return response;
102      }
103      client.State = State.offline;
104      clientAdapter.UpdateClient(client);
105
106      response.Success = true;
107      response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_SUCCESS;
108     
109      return response;
110    }
111
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.