[741] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
[751] | 5 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
[780] | 6 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 7 | using HeuristicLab.Hive.Contracts;
|
---|
[823] | 8 | using HeuristicLab.Core;
|
---|
[842] | 9 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
| 10 | using System.Resources;
|
---|
| 11 | using System.Reflection;
|
---|
[741] | 12 |
|
---|
| 13 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[780] | 14 | /// <summary>
|
---|
| 15 | /// The ClientCommunicator manages the whole communication with the client
|
---|
| 16 | /// </summary>
|
---|
| 17 | public class ClientCommunicator: IClientCommunicator {
|
---|
[791] | 18 | List<ClientInfo> clients;
|
---|
[805] | 19 | LinkedList<long> jobs;
|
---|
[816] | 20 | int nrOfJobs = 1;
|
---|
[783] | 21 |
|
---|
[842] | 22 | IClientAdapter clientAdapter;
|
---|
| 23 | ResourceManager rm;
|
---|
| 24 |
|
---|
[783] | 25 | public ClientCommunicator() {
|
---|
[842] | 26 | clientAdapter = ServiceLocator.GetClientAdapter();
|
---|
| 27 | rm = new ResourceManager("HiveServerMessages.resx", Assembly.GetExecutingAssembly());
|
---|
| 28 |
|
---|
[805] | 29 | jobs = new LinkedList<long>();
|
---|
[811] | 30 | for (long i = 0; i < nrOfJobs; i++) {
|
---|
[805] | 31 | jobs.AddFirst(i);
|
---|
[783] | 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[741] | 35 | #region IClientCommunicator Members
|
---|
| 36 |
|
---|
[791] | 37 | public Response Login(ClientInfo clientInfo) {
|
---|
[741] | 38 | Response response = new Response();
|
---|
| 39 | response.Success = true;
|
---|
| 40 |
|
---|
[842] | 41 | ICollection<ClientInfo> allClients = clientAdapter.GetAllClients();
|
---|
| 42 | foreach (ClientInfo client in allClients) {
|
---|
| 43 | if (client.ClientId.Equals(clientInfo.ClientId)) {
|
---|
| 44 | if (client.State != State.offline) {
|
---|
| 45 | response.Success = false;
|
---|
[907] | 46 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGIN_USER_ALLREADY_ONLINE;
|
---|
[842] | 47 | break;
|
---|
| 48 | } else
|
---|
| 49 | break; // searching for clients can be stopped, because it was found and it's state is offline
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if (response.Success) {
|
---|
| 54 | clientAdapter.UpdateClient(clientInfo);
|
---|
| 55 | response.Success = true;
|
---|
[907] | 56 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGIN_SUCCESS;
|
---|
[842] | 57 | }
|
---|
| 58 |
|
---|
[741] | 59 | return response;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[780] | 62 | public ResponseHB SendHeartBeat(HeartBeatData hbData) {
|
---|
[783] | 63 | ResponseHB response = new ResponseHB();
|
---|
| 64 |
|
---|
| 65 | response.Success = true;
|
---|
| 66 | response.StatusMessage = "HeartBeat received";
|
---|
[797] | 67 | response.ActionRequest = new List<MessageContainer>();
|
---|
[783] | 68 | if (jobs.Count > 0)
|
---|
| 69 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
|
---|
| 70 | else
|
---|
| 71 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
|
---|
| 72 |
|
---|
| 73 | return response;
|
---|
[780] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public ResponseJob PullJob(Guid clientId) {
|
---|
[783] | 77 | ResponseJob response = new ResponseJob();
|
---|
[805] | 78 | lock (this) {
|
---|
| 79 | response.JobId = jobs.Last.Value;
|
---|
[823] | 80 | jobs.RemoveLast();
|
---|
| 81 | response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
|
---|
[805] | 82 | }
|
---|
| 83 |
|
---|
[783] | 84 | response.Success = true;
|
---|
[805] | 85 | response.StatusMessage = "Job with id " + jobs.Count + " sent";
|
---|
[783] | 86 | return response;
|
---|
[780] | 87 | }
|
---|
| 88 |
|
---|
[838] | 89 | public ResponseResultReceived SendJobResult(JobResult Result, bool finished) {
|
---|
| 90 | ResponseResultReceived response = new ResponseResultReceived();
|
---|
[783] | 91 | response.Success = true;
|
---|
| 92 | response.StatusMessage = "Thanks for calculating";
|
---|
[838] | 93 | response.JobId = Result.JobId;
|
---|
[783] | 94 |
|
---|
| 95 | return response;
|
---|
[780] | 96 | }
|
---|
[805] | 97 |
|
---|
[780] | 98 | public Response Logout(Guid clientId) {
|
---|
[786] | 99 | Response response = new Response();
|
---|
[783] | 100 |
|
---|
[902] | 101 | ClientInfo client = clientAdapter.GetClientById(clientId);
|
---|
| 102 | if (client == null) {
|
---|
[783] | 103 | response.Success = false;
|
---|
[907] | 104 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGOUT_CLIENT_NOT_REGISTERED;
|
---|
[902] | 105 | return response;
|
---|
[783] | 106 | }
|
---|
[902] | 107 | client.State = State.offline;
|
---|
| 108 | clientAdapter.UpdateClient(client);
|
---|
| 109 |
|
---|
| 110 | response.Success = true;
|
---|
[907] | 111 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGOUT_SUCCESS;
|
---|
[902] | 112 |
|
---|
[783] | 113 | return response;
|
---|
[780] | 114 | }
|
---|
| 115 |
|
---|
[741] | 116 | #endregion
|
---|
| 117 | }
|
---|
| 118 | }
|
---|