Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implementing ClientCommunicator (#399)

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