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 @ 5707

Last change on this file since 5707 was 5707, checked in by cneumuel, 13 years ago

#1260

  • some changes due to the removal of Disposable (r5706)
  • copy PluginInfrastructure files into PluginCache folder in slaves (needed due to r5703)
File size: 2.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Windows.Forms;
5using HeuristicLab.Clients.Common;
6using HeuristicLab.Hive.Contracts;
7using HeuristicLab.Hive.Slave.Console.SlaveConsoleService;
8using System.ServiceModel;
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    private void timer_Tick(object sender, EventArgs e) {
27      try {
28        using (var factory = new DisposableWrapper<ChannelFactory<ISlaveConsoleCommunicator>>(ClientFactory.CreateChannelFactory<ISlaveConsoleCommunicator>("SlaveConsoleTcpEndpointClient"))) {
29          var slaveClient = factory.Obj.CreateChannel();
30          try {
31            var messages = slaveClient.GetLogMessages();
32            if (messages.Count < messageCount) {
33              messageCount = 0; // log got cleared
34            }
35            var newMessages = messages.GetRange(messageCount, messages.Count - messageCount);
36            messageCount = messages.Count;
37            OnMoreData(newMessages);
38
39          }
40          catch (Exception ex) {
41            OnMoreData(new List<string> { "Error fetching log: " + ex.ToString() });
42          }
43        }
44      }
45      catch (Exception ex) {
46        OnMoreData(new List<string> { "Error in slave-communication: " + ex.ToString() });
47      }
48    }
49
50    public void Stop() {
51      timer.Stop();
52    }
53
54    public event MoreDataHandler MoreData;
55    private void OnMoreData(IEnumerable<string> newMessages) {
56      var handler = MoreData;
57      var newData = string.Join(Environment.NewLine, newMessages.ToArray());
58      if (newMessages.Count() > 0) newData += Environment.NewLine;
59      if (handler != null) MoreData(this, newData);
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.