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 | using HeuristicLab.Hive.JobBase;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Hive.Server.Core {
|
---|
15 | /// <summary>
|
---|
16 | /// The ClientCommunicator manages the whole communication with the client
|
---|
17 | /// </summary>
|
---|
18 | public class ClientCommunicator: IClientCommunicator {
|
---|
19 | int nrOfJobs = 10;
|
---|
20 |
|
---|
21 | IClientAdapter clientAdapter;
|
---|
22 | IJobAdapter jobAdapter;
|
---|
23 | IJobResultsAdapter jobResultAdapter;
|
---|
24 |
|
---|
25 | public ClientCommunicator() {
|
---|
26 | clientAdapter = ServiceLocator.GetClientAdapter();
|
---|
27 | jobAdapter = ServiceLocator.GetJobAdapter();
|
---|
28 |
|
---|
29 | for (int i = 0; i < nrOfJobs; i++) {
|
---|
30 | Job job = new Job();
|
---|
31 | job.Id = i;
|
---|
32 | job.State = State.offline;
|
---|
33 | jobAdapter.Update(job);
|
---|
34 | }
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | #region IClientCommunicator Members
|
---|
39 |
|
---|
40 | public Response Login(ClientInfo clientInfo) {
|
---|
41 | Response response = new Response();
|
---|
42 |
|
---|
43 | ICollection<ClientInfo> allClients = clientAdapter.GetAll();
|
---|
44 | ClientInfo client = clientAdapter.GetById(clientInfo.ClientId);
|
---|
45 | if (client != null && client.State != State.offline) {
|
---|
46 | response.Success = false;
|
---|
47 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_USER_ALLREADY_ONLINE;
|
---|
48 | return response;
|
---|
49 | }
|
---|
50 | clientAdapter.Update(clientInfo);
|
---|
51 | response.Success = true;
|
---|
52 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGIN_SUCCESS;
|
---|
53 |
|
---|
54 | return response;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ResponseHB SendHeartBeat(HeartBeatData hbData) {
|
---|
58 | ResponseHB response = new ResponseHB();
|
---|
59 |
|
---|
60 | response.Success = true;
|
---|
61 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_HARDBEAT_RECEIVED;
|
---|
62 | response.ActionRequest = new List<MessageContainer>();
|
---|
63 | List<Job> allOfflineJobs = new List<Job>(jobAdapter.GetJobsByState(State.offline));
|
---|
64 | if (allOfflineJobs.Count > 0 && hbData.freeCores > 0)
|
---|
65 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
|
---|
66 | else
|
---|
67 | response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
|
---|
68 |
|
---|
69 | return response;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public ResponseJob PullJob(Guid clientId) {
|
---|
73 | ResponseJob response = new ResponseJob();
|
---|
74 | lock (this) {
|
---|
75 | LinkedList<Job> allOfflineJobs = new LinkedList<Job>(jobAdapter.GetJobsByState(State.offline));
|
---|
76 | if (allOfflineJobs != null && allOfflineJobs.Count > 0) {
|
---|
77 | Job job2Calculate = allOfflineJobs.First.Value;
|
---|
78 | job2Calculate.State = State.calculating;
|
---|
79 | response.JobId = job2Calculate.Id;
|
---|
80 | jobAdapter.Update(job2Calculate);
|
---|
81 | response.SerializedJob = PersistenceManager.SaveToGZip(new TestJob());
|
---|
82 | response.Success = true;
|
---|
83 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOB_PULLED;
|
---|
84 | return response;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | response.Success = true;
|
---|
88 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JOBS_LEFT;
|
---|
89 | return response;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public ResponseResultReceived SendJobResult(JobResult result, bool finished) {
|
---|
93 | ResponseResultReceived response = new ResponseResultReceived();
|
---|
94 | if (result.Id != 0) {
|
---|
95 | response.Success = false;
|
---|
96 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_ID_MUST_NOT_BE_SET;
|
---|
97 | return response;
|
---|
98 | }
|
---|
99 | Job job = jobAdapter.GetById(result.JobId);
|
---|
100 | if (job == null) {
|
---|
101 | response.Success = false;
|
---|
102 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_NO_JO_WITH_THIS_ID;
|
---|
103 | return response;
|
---|
104 | }
|
---|
105 | if (job.State != State.calculating) {
|
---|
106 | response.Success = false;
|
---|
107 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_WRONG_JOB_STATE;
|
---|
108 | return response;
|
---|
109 | }
|
---|
110 | if (finished) {
|
---|
111 | job.State = State.finished;
|
---|
112 | jobAdapter.Update(job);
|
---|
113 |
|
---|
114 | List<JobResult> jobResults = new List<JobResult>(jobResultAdapter.GetResultsOf(job));
|
---|
115 | foreach (JobResult currentResult in jobResults)
|
---|
116 | jobResultAdapter.Delete(currentResult);
|
---|
117 | }
|
---|
118 | jobResultAdapter.Update(result);
|
---|
119 |
|
---|
120 | response.Success = true;
|
---|
121 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_JOBRESULT_RECEIVED;
|
---|
122 | response.JobId = result.JobId;
|
---|
123 |
|
---|
124 | return response;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public Response Logout(Guid clientId) {
|
---|
128 | Response response = new Response();
|
---|
129 |
|
---|
130 | ClientInfo client = clientAdapter.GetById(clientId);
|
---|
131 | if (client == null) {
|
---|
132 | response.Success = false;
|
---|
133 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_CLIENT_NOT_REGISTERED;
|
---|
134 | return response;
|
---|
135 | }
|
---|
136 | client.State = State.offline;
|
---|
137 | clientAdapter.Update(client);
|
---|
138 |
|
---|
139 | response.Success = true;
|
---|
140 | response.StatusMessage = ApplicationConstants.RESPONSE_COMMUNICATOR_LOGOUT_SUCCESS;
|
---|
141 |
|
---|
142 | return response;
|
---|
143 | }
|
---|
144 |
|
---|
145 | #endregion
|
---|
146 | }
|
---|
147 | }
|
---|