Ejemplo n.º 1
0
        private static void HandshakeMessageReceived(string message)
        {
            //if (!_handshaking) return;
            ServiceSyncMasterSlaveHandshakeMessage m = null;

            try
            {
                m = Deserialize <ServiceSyncMasterSlaveHandshakeMessage>(message);
            }
            catch (Exception e)
            {
                WriteTrace(string.Format("Exception: {0}{2} Trace: {1}", e.Message, e.StackTrace, Environment.NewLine));
            }
            if (m == null)
            {
                return;
            }
            if (m.ServiceName != ServiceName)
            {
                return;
            }
            if (m.ServiceName == ServiceName && m.MachineName != MachineName)
            {
                lock (_handshakeLock)
                {
                    m.ReceivedAt = DateTime.Now;
                    _lastReceivedHandshakeMessage = m;
                }
            }
        }
Ejemplo n.º 2
0
 public static void Start()
 {
     if (_running)
     {
         return;
     }
     _lastReceivedHandshakeMessage = null;
     _lastReceivedSyncMessage      = null;
     IsMaster                 = false;
     _handshaking             = false;
     _handshakeRetryCount     = 0;
     _failoverIntervalCounter = 0;
     _running                 = true;
     //var task = new Task(() => _receiver.Start());
     //task.Start();
     _receiver.Start();
     _syncTimer.Start();
 }
Ejemplo n.º 3
0
 private static void Handshake()
 {
     if (_handshaking)
     {
         return;
     }
     try
     {
         _abortHandshake = false;
         lock (_handshakeLock)
         {
             _lastReceivedHandshakeMessage = null;
         }
         _handshaking = true;
         var diffMiliseconds = HeartBeatInverval * MaxHandshakeRetry * 10;
         _luck = 0;
         _handshakeRetryCount = 0;
         WriteTrace("Handshake started...");
         WishLuck();
         Thread.Sleep(HeartBeatInverval * 5);
         while (!_abortHandshake)
         {
             WriteTrace(string.Format("Handshake retry: {0}", _handshakeRetryCount));
             var message = new ServiceSyncMasterSlaveHandshakeMessage()
             {
                 Luck        = _luck,
                 MachineName = MachineName,
                 ServiceName = ServiceName,
                 SentOn      = DateTime.Now
             };
             var msg = Serialize(message);
             _sender.SendMessage(msg);
             Thread.Sleep(HeartBeatInverval * 5);
             if (_lastReceivedHandshakeMessage != null && _lastReceivedHandshakeMessage.SentOn > DateTime.Now.AddMilliseconds(-diffMiliseconds))
             {
                 if (_lastReceivedHandshakeMessage.Luck == _luck)
                 {
                     WriteTrace(string.Format("CAN NOT Decide we are EQUAL (Received: {0} == Sended: {1})", _lastReceivedHandshakeMessage.Luck, _luck));
                     WishLuck();
                     continue;
                 }
                 if (_lastReceivedHandshakeMessage.Luck < _luck)
                 {
                     WriteTrace(string.Format("Decided as MASTER by Luck (Received: {0} < Sended: {1})", _lastReceivedHandshakeMessage.Luck, _luck));
                     IsMaster = true;
                     break;
                 }
                 if (_lastReceivedHandshakeMessage.Luck > _luck)
                 {
                     WriteTrace(string.Format("Decided as SLAVE by Luck (Received: {0} > Sended: {1})", _lastReceivedHandshakeMessage.Luck, _luck));
                     IsMaster = false;
                     break;
                 }
             }
             if (_handshakeRetryCount > _maxHandshakeRetry)
             {
                 WriteTrace("Decided as MASTER by RETRY");
                 IsMaster = true;
                 break;
             }
             _handshakeRetryCount++;
         }
     }
     finally
     {
         _handshaking = false;
     }
     WriteTrace("Handshake ended...");
 }