1 /*----------------------------------------------------------------------------
  2 Name:      XbPoller.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 XbPoller
 21    {
 22       private readonly string ME = "XbPoller";
 23       private I_LoggingCallback logger;
 24       private volatile bool running;
 25       private XmlBlasterAccess xbAccess;
 26       private int sleepMillis;
 27       private Thread thread;
 28       private object locker = new object();
 29       public static readonly int MIN_POLL_MILLIS = 4000;
 30 
 31       public XbPoller(XmlBlasterAccess xbAccess, long sleepMillis, I_LoggingCallback listener)
 32       {
 33          this.xbAccess = xbAccess;
 34          this.sleepMillis = (int)sleepMillis;
 35          if (this.sleepMillis < MIN_POLL_MILLIS) this.sleepMillis = MIN_POLL_MILLIS;
 36          this.logger = listener;
 37          this.running = false;
 38       }
 39 
 40       public bool IsConfiguredToWork()
 41       {
 42          return this.sleepMillis > 0;
 43       }
 44 
 45       public bool IsStarted()
 46       {
 47          return this.running;
 48       }
 49 
 50       public bool Start()
 51       {
 52          if (this.sleepMillis < 1)
 53             return false;
 54          lock (locker)
 55          {
 56             if (this.running) return false;
 57             this.running = true;
 58             this.thread = new Thread(this.Run);
 59             this.thread.Start();
 60          }
 61          logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "Start pollInterval" + sleepMillis);
 62          return true;
 63       }
 64 
 65       public bool Stop()
 66       {
 67          lock (locker)
 68          {
 69             if (!this.running) return false;
 70             this.running = false;
 71          }
 72          logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "Stop");
 73          return true;
 74       }
 75 
 76       private void Run()
 77       {
 78          try
 79          {
 80             //logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "working...");

 81             while (this.running)
 82             {
 83                try
 84                {
 85                   logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "Poll ...");
 86                   this.xbAccess.OnReconnectTry();
 87                   logger.OnLogging(XmlBlasterLogLevel.INFO, ME, "Poll sucess, reconnected");
 88                   break;
 89                }
 90                catch (Exception e)
 91                {
 92                   logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, e.ToString());
 93                }
 94 
 95                if (!this.running)
 96                   break;
 97 
 98                Thread.Sleep(sleepMillis);
 99             }
100             logger.OnLogging(XmlBlasterLogLevel.TRACE, ME, "terminating gracefully.");
101          }
102          catch (Exception e2) {
103             logger.OnLogging(XmlBlasterLogLevel.WARN, ME, "terminating abort: " + e2.ToString());
104          }
105       }
106 
107       public long GetSleepMillis()
108       {
109          return this.sleepMillis;
110       }
111 
112       public void Shutdown()
113       {
114          Stop();
115          if (this.thread != null)
116             this.thread.Abort();
117       }
118    }
119 }


syntax highlighted by Code2HTML, v. 0.9.1