Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/ClientCommunicator.cs @ 838

Last change on this file since 838 was 838, checked in by msteinbi, 16 years ago

Changed response class for SendJobResult (#371)

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