1 /*----------------------------------------------------------------------------
2 Name: XbPinger.cs
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Provides abstraction to xmlBlaster access from C#
6 Author: "Marcel Ruff" <xmlBlaster@marcelruff.info>
7 Date: 05/2008
8 See: http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.html
9 -----------------------------------------------------------------------------*/
10 using System;
11 using System.Text;
12 using System.Threading;
13 using System.Collections;
14 using System.Collections.Generic;
15
16 //using org.xmlBlaster.util;
17
18 namespace org.xmlBlaster.client
19 {
20 public class XbPinger
21 {
22
23 private readonly string ME = "XbPinger";
24 private I_LoggingCallback logger;
25 private volatile bool running;
26 private XmlBlasterAccess xbAccess;
27 private int sleepMillis;
28 private Thread thread;
29 private object locker = new object();
30 public static readonly int MIN_PING_MILLIS = 5000;
31
32 public XbPinger(XmlBlasterAccess xbAccess, long sleepMillis, I_LoggingCallback listener)
33 {
34 this.xbAccess = xbAccess;
35 this.sleepMillis = (int)sleepMillis;
36 if (this.sleepMillis < MIN_PING_MILLIS) this.sleepMillis = MIN_PING_MILLIS;
37 this.logger = listener;
38 this.running = false;
39 }
40
41 public bool IsConfiguredToWork()
42 {
43 return this.sleepMillis > 0;
44 }
45
46 public bool IsStarted()
47 {
48 return this.running;
49 }
50
51 public bool Start()
52 {
53 if (this.sleepMillis < 1)
54 return false;
55 lock (locker)
56 {
57 if (this.running) return false;
58 this.running = true;
59 this.thread = new Thread(this.Run);
60 this.thread.Start();
61 }
62 logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "Start pingInterval=" + sleepMillis);
63 return true;
64 }
65
66 public bool Stop()
67 {
68 lock (locker)
69 {
70 if (!this.running) return false;
71 this.running = false;
72 }
73 logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "Stop");
74 return true;
75 }
76
77 private void Run()
78 {
79 try
80 {
81 //logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "working...");
82 while (this.running)
83 {
84 try
85 {
86 logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "Ping ...");
87 string pong = this.xbAccess.Ping("");
88 logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "Ping sucess: " + pong);
89 }
90 catch (Exception e)
91 {
92 logger.OnLogging(XmlBlasterLogLevel.WARN, ME, "Ping: " + e.ToString());
93 Stop();
94 this.xbAccess.OnPingFailed(e);
95 }
96
97 if (!this.running) break;
98
99 Thread.Sleep(sleepMillis);
100 }
101 logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "terminating gracefully.");
102 }
103 catch (Exception e2)
104 {
105 logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "terminating abort: " + e2.ToString());
106 }
107 }
108
109 public long GetSleepMillis()
110 {
111 return this.sleepMillis;
112 }
113
114 public void Shutdown()
115 {
116 Stop();
117 if (this.thread != null)
118 this.thread.Abort();
119 }
120 }
121 }
syntax highlighted by Code2HTML, v. 0.9.1