1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
6 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
7 | using HeuristicLab.Hive.Contracts;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Hive.Server.Core {
|
---|
10 | /// <summary>
|
---|
11 | /// The ClientCommunicator manages the whole communication with the client
|
---|
12 | /// </summary>
|
---|
13 | public class ClientCommunicator: IClientCommunicator {
|
---|
14 | List<ClientInfo> clients;
|
---|
15 | LinkedList<long> jobs;
|
---|
16 | int nrOfJobs = 10;
|
---|
17 |
|
---|
18 | public ClientCommunicator() {
|
---|
19 | jobs = new LinkedList<long>();
|
---|
20 | for (long i = 1; i < nrOfJobs; i++) {
|
---|
21 | jobs.AddFirst(i);
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | #region IClientCommunicator Members
|
---|
26 |
|
---|
27 | public Response Login(ClientInfo clientInfo) {
|
---|
28 | if (clients == null)
|
---|
29 | clients = new List<ClientInfo>();
|
---|
30 |
|
---|
31 | clients.Add(clientInfo);
|
---|
32 |
|
---|
33 | Response response = new Response();
|
---|
34 | response.Success = true;
|
---|
35 | response.StatusMessage = "Client with GUID " + clientInfo.ClientId + " successuflly logged in";
|
---|
36 |
|
---|
37 | return response;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ResponseHB SendHeartBeat(HeartBeatData hbData) {
|
---|
41 | ResponseHB response = new ResponseHB();
|
---|
42 |
|
---|
43 | response.Success = true;
|
---|
44 | response.StatusMessage = "HeartBeat received";
|
---|
45 | response.ActionRequest = new List<MessageContainer>();
|
---|
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;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ResponseJob PullJob(Guid clientId) {
|
---|
55 | ResponseJob response = new ResponseJob();
|
---|
56 | lock (this) {
|
---|
57 | response.JobId = jobs.Last.Value;
|
---|
58 | jobs.RemoveLast();
|
---|
59 | }
|
---|
60 |
|
---|
61 | response.Success = true;
|
---|
62 | response.StatusMessage = "Job with id " + jobs.Count + " sent";
|
---|
63 | return response;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Response SendJobResult(JobResult Result, bool finished) {
|
---|
67 | Response response = new Response();
|
---|
68 | response.Success = true;
|
---|
69 | response.StatusMessage = "Thanks for calculating";
|
---|
70 |
|
---|
71 | return response;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public Response Logout(Guid clientId) {
|
---|
75 | bool clientRemoved = false;
|
---|
76 | foreach (ClientInfo client in clients) {
|
---|
77 | if (client.ClientId.Equals(clientId)) {
|
---|
78 | clients.Remove(client);
|
---|
79 | clientRemoved = true;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | Response response = new Response();
|
---|
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;
|
---|
92 | }
|
---|
93 |
|
---|
94 | #endregion
|
---|
95 | }
|
---|
96 | }
|
---|