Free cookie consent management tool by TermsFeed Policy Generator

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

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

Faked jobs replaced with real jobs (#396)

File size: 2.9 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 Response SendJobResult(JobResult Result, bool finished) {
69      Response response = new Response();
70      response.Success = true;
71      response.StatusMessage = "Thanks for calculating";
72
73      return response;
74    }
75                           
76    public Response Logout(Guid clientId) {
77      bool clientRemoved = false;
78      foreach (ClientInfo client in clients) {
79        if (client.ClientId.Equals(clientId)) {
80          clients.Remove(client);
81          clientRemoved = true;
82        }
83      }
84      Response response = new Response();
85     
86      if (clientRemoved) {
87        response.Success = true;
88        response.StatusMessage = "Successfully logged out. Good bye";
89      } else {
90        response.Success = false;
91        response.StatusMessage = "Sorry, but you weren't logged in";
92      }
93      return response;
94    }
95
96    #endregion
97  }
98}
Note: See TracBrowser for help on using the repository browser.