Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Hive.Server.Core/ClientCommunicator.cs @ 1184

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

Login implemented with db (#399)

File size: 3.8 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;
9using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
10using System.Resources;
11using System.Reflection;
12
13namespace 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 = rm.GetString("UserAllreadyOnline");
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 = rm.GetString("LoginSuccess");
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      bool clientRemoved = false;
100      foreach (ClientInfo client in clients) {
101        if (client.ClientId.Equals(clientId)) {
102          clients.Remove(client);
103          clientRemoved = true;
104        }
105      }
106      Response response = new Response();
107     
108      if (clientRemoved) {
109        response.Success = true;
110        response.StatusMessage = "Successfully logged out. Good bye";
111      } else {
112        response.Success = false;
113        response.StatusMessage = "Sorry, but you weren't logged in";
114      }
115      return response;
116    }
117
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.