using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Hive.Contracts.BusinessObjects;
using HeuristicLab.Hive.Contracts.Interfaces;
using HeuristicLab.Hive.Contracts;
namespace HeuristicLab.Hive.Server.Core {
///
/// The ClientCommunicator manages the whole communication with the client
///
public class ClientCommunicator: IClientCommunicator {
List clients;
LinkedList jobs;
int nrOfJobs = 3;
public ClientCommunicator() {
jobs = new LinkedList();
for (long i = 0; i < nrOfJobs; i++) {
jobs.AddFirst(i);
}
}
#region IClientCommunicator Members
public Response Login(ClientInfo clientInfo) {
if (clients == null)
clients = new List();
clients.Add(clientInfo);
Response response = new Response();
response.Success = true;
response.StatusMessage = "Client with GUID " + clientInfo.ClientId + " successuflly logged in";
return response;
}
public ResponseHB SendHeartBeat(HeartBeatData hbData) {
ResponseHB response = new ResponseHB();
response.Success = true;
response.StatusMessage = "HeartBeat received";
response.ActionRequest = new List();
if (jobs.Count > 0)
response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.FetchJob));
else
response.ActionRequest.Add(new MessageContainer(MessageContainer.MessageType.NoMessage));
return response;
}
public ResponseJob PullJob(Guid clientId) {
ResponseJob response = new ResponseJob();
lock (this) {
response.JobId = jobs.Last.Value;
jobs.RemoveLast();
}
response.Success = true;
response.StatusMessage = "Job with id " + jobs.Count + " sent";
return response;
}
public Response SendJobResult(JobResult Result, bool finished) {
Response response = new Response();
response.Success = true;
response.StatusMessage = "Thanks for calculating";
return response;
}
public Response Logout(Guid clientId) {
bool clientRemoved = false;
foreach (ClientInfo client in clients) {
if (client.ClientId.Equals(clientId)) {
clients.Remove(client);
clientRemoved = true;
}
}
Response response = new Response();
if (clientRemoved) {
response.Success = true;
response.StatusMessage = "Successfully logged out. Good bye";
} else {
response.Success = false;
response.StatusMessage = "Sorry, but you weren't logged in";
}
return response;
}
#endregion
}
}