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 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
|
---|
10 | using System.Resources;
|
---|
11 | using System.Reflection;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Server.Core {
|
---|
14 | /// <summary>
|
---|
15 | /// The ClientCommunicator manages the whole communication with the client
|
---|
16 | /// </summary>
|
---|
17 | public class ClientCommunicator: IClientCommunicator {
|
---|
18 | List<ClientInfo> clients;
|
---|
19 | LinkedList<long> jobs;
|
---|
20 | int nrOfJobs = 1;
|
---|
21 |
|
---|
22 | IClientAdapter clientAdapter;
|
---|
23 | ResourceManager rm;
|
---|
24 |
|
---|
25 | public ClientCommunicator() {
|
---|
26 | clientAdapter = ServiceLocator.GetClientAdapter();
|
---|
27 | rm = new ResourceManager("HiveServerMessages.resx", Assembly.GetExecutingAssembly());
|
---|
28 |
|
---|
29 | jobs = new LinkedList<long>();
|
---|
30 | for (long i = 0; i < nrOfJobs; i++) {
|
---|
31 | jobs.AddFirst(i);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | #region IClientCommunicator Members
|
---|
36 |
|
---|
37 | public Response Login(ClientInfo clientInfo) {
|
---|
38 | Response response = new Response();
|
---|
39 | response.Success = true;
|
---|
40 |
|
---|
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;
|
---|
46 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGIN_USER_ALLREADY_ONLINE;
|
---|
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;
|
---|
56 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGIN_SUCCESS;
|
---|
57 | }
|
---|
58 |
|
---|
59 | return response;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public ResponseHB SendHeartBeat(HeartBeatData hbData) {
|
---|
63 | ResponseHB response = new ResponseHB();
|
---|
64 |
|
---|
65 | response.Success = true;
|
---|
66 | response.StatusMessage = "HeartBeat received";
|
---|
67 | response.ActionRequest = new List<MessageContainer>();
|
---|
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;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public ResponseJob PullJob(Guid clientId) {
|
---|
77 | ResponseJob response = new ResponseJob();
|
---|
78 | lock (this) {
|
---|
79 | response.JobId = jobs.Last.Value;
|
---|
80 | jobs.RemoveLast();
|
---|
81 | response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
|
---|
82 | }
|
---|
83 |
|
---|
84 | response.Success = true;
|
---|
85 | response.StatusMessage = "Job with id " + jobs.Count + " sent";
|
---|
86 | return response;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public ResponseResultReceived SendJobResult(JobResult Result, bool finished) {
|
---|
90 | ResponseResultReceived response = new ResponseResultReceived();
|
---|
91 | response.Success = true;
|
---|
92 | response.StatusMessage = "Thanks for calculating";
|
---|
93 | response.JobId = Result.JobId;
|
---|
94 |
|
---|
95 | return response;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public Response Logout(Guid clientId) {
|
---|
99 | Response response = new Response();
|
---|
100 |
|
---|
101 | ClientInfo client = clientAdapter.GetClientById(clientId);
|
---|
102 | if (client == null) {
|
---|
103 | response.Success = false;
|
---|
104 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGOUT_CLIENT_NOT_REGISTERED;
|
---|
105 | return response;
|
---|
106 | }
|
---|
107 | client.State = State.offline;
|
---|
108 | clientAdapter.UpdateClient(client);
|
---|
109 |
|
---|
110 | response.Success = true;
|
---|
111 | response.StatusMessage = ApplicationConstants.RESPONSE_LOGOUT_SUCCESS;
|
---|
112 |
|
---|
113 | return response;
|
---|
114 | }
|
---|
115 |
|
---|
116 | #endregion
|
---|
117 | }
|
---|
118 | }
|
---|