[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;
|
---|
[741] | 9 |
|
---|
| 10 | namespace HeuristicLab.Hive.Server.Core {
|
---|
[780] | 11 | /// <summary>
|
---|
| 12 | /// The ClientCommunicator manages the whole communication with the client
|
---|
| 13 | /// </summary>
|
---|
| 14 | public class ClientCommunicator: IClientCommunicator {
|
---|
[791] | 15 | List<ClientInfo> clients;
|
---|
[805] | 16 | LinkedList<long> jobs;
|
---|
[816] | 17 | int nrOfJobs = 1;
|
---|
[783] | 18 |
|
---|
| 19 | public ClientCommunicator() {
|
---|
[805] | 20 | jobs = new LinkedList<long>();
|
---|
[811] | 21 | for (long i = 0; i < nrOfJobs; i++) {
|
---|
[805] | 22 | jobs.AddFirst(i);
|
---|
[783] | 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[741] | 26 | #region IClientCommunicator Members
|
---|
| 27 |
|
---|
[791] | 28 | public Response Login(ClientInfo clientInfo) {
|
---|
[783] | 29 | if (clients == null)
|
---|
[791] | 30 | clients = new List<ClientInfo>();
|
---|
[783] | 31 |
|
---|
| 32 | clients.Add(clientInfo);
|
---|
| 33 |
|
---|
[741] | 34 | Response response = new Response();
|
---|
| 35 | response.Success = true;
|
---|
[783] | 36 | response.StatusMessage = "Client with GUID " + clientInfo.ClientId + " successuflly logged in";
|
---|
[741] | 37 |
|
---|
| 38 | return response;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[780] | 41 | public ResponseHB SendHeartBeat(HeartBeatData hbData) {
|
---|
[783] | 42 | ResponseHB response = new ResponseHB();
|
---|
| 43 |
|
---|
| 44 | response.Success = true;
|
---|
| 45 | response.StatusMessage = "HeartBeat received";
|
---|
[797] | 46 | response.ActionRequest = new List<MessageContainer>();
|
---|
[783] | 47 | if (jobs.Count > 0)
|
---|
| 48 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
|
---|
| 49 | else
|
---|
| 50 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
|
---|
| 51 |
|
---|
| 52 | return response;
|
---|
[780] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public ResponseJob PullJob(Guid clientId) {
|
---|
[783] | 56 | ResponseJob response = new ResponseJob();
|
---|
[805] | 57 | lock (this) {
|
---|
| 58 | response.JobId = jobs.Last.Value;
|
---|
[823] | 59 | jobs.RemoveLast();
|
---|
| 60 | response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
|
---|
[805] | 61 | }
|
---|
| 62 |
|
---|
[783] | 63 | response.Success = true;
|
---|
[805] | 64 | response.StatusMessage = "Job with id " + jobs.Count + " sent";
|
---|
[783] | 65 | return response;
|
---|
[780] | 66 | }
|
---|
| 67 |
|
---|
| 68 | public Response SendJobResult(JobResult Result, bool finished) {
|
---|
[783] | 69 | Response response = new Response();
|
---|
| 70 | response.Success = true;
|
---|
| 71 | response.StatusMessage = "Thanks for calculating";
|
---|
| 72 |
|
---|
| 73 | return response;
|
---|
[780] | 74 | }
|
---|
[805] | 75 |
|
---|
[780] | 76 | public Response Logout(Guid clientId) {
|
---|
[783] | 77 | bool clientRemoved = false;
|
---|
[791] | 78 | foreach (ClientInfo client in clients) {
|
---|
[783] | 79 | if (client.ClientId.Equals(clientId)) {
|
---|
| 80 | clients.Remove(client);
|
---|
| 81 | clientRemoved = true;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[786] | 84 | Response response = new Response();
|
---|
[783] | 85 |
|
---|
| 86 | if (clientRemoved) {
|
---|
| 87 | response.Success = true;
|
---|
| 88 | response.StatusMessage = "Successfully logged out. Good bye";
|
---|
| 89 | } else {
|
---|
| 90 | response.Success = false;
|
---|
| 91 | response.StatusMessage = "Sorry, but you weren't logged in";
|
---|
| 92 | }
|
---|
| 93 | return response;
|
---|
[780] | 94 | }
|
---|
| 95 |
|
---|
[741] | 96 | #endregion
|
---|
| 97 | }
|
---|
| 98 | }
|
---|