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