Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Slave.Console/3.3/LogServiceReader.cs @ 5093

Last change on this file since 5093 was 5093, checked in by cneumuel, 14 years ago

#1260

  • moved all state-information into lifecycleManager
  • changed isolation level for transactions to ReadCommited
  • made currentlyFetching-status on slave more rubust
  • made LogServiceReader more rubust
File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Core;
7using HeuristicLab.Hive.Slave.Console.SlaveConsoleService;
8using HeuristicLab.Clients.Common;
9
10namespace HeuristicLab.Hive.Slave.Console {
11  internal class LogServiceReader : ILogReader {
12
13    private Timer timer;
14    private int messageCount = 0;
15
16    public LogServiceReader() {
17    }
18
19    public void Start() {
20      timer = new Timer();
21      timer.Interval = 2000;
22      timer.Tick += new EventHandler(timer_Tick);
23      timer.Start();
24    }
25
26    void timer_Tick(object sender, EventArgs e) {
27      try {
28        using (var slaveClient = ClientFactory.CreateClient<ISlaveConsoleCommunicator>("SlaveConsoleTcpEndpointClient")) {
29          try {
30            var messages = slaveClient.Obj.GetLogMessages();
31            if (messages.Count < messageCount) {
32              messageCount = 0; // log got cleared
33            }
34            var newMessages = messages.GetRange(messageCount, messages.Count - messageCount);
35            messageCount = messages.Count;
36            OnMoreData(newMessages);
37
38          }
39          catch (Exception ex) {
40            OnMoreData(new List<string> { "Error fetching log: " + ex.ToString() });
41          }
42        }
43      }
44      catch (Exception ex) {
45        OnMoreData(new List<string> { "Error in slave-communication: " + ex.ToString() });
46      }
47    }
48
49    public void Stop() {
50      timer.Stop();
51    }
52
53    public event MoreDataHandler MoreData;
54    private void OnMoreData(IEnumerable<string> newMessages) {
55      var handler = MoreData;
56      var newData = string.Join(Environment.NewLine, newMessages.ToArray());
57      if (newMessages.Count() > 0) newData += Environment.NewLine;
58      if (handler != null) MoreData(this, newData);
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.