Ejemplo n.º 1
0
        public void Register(string[] productFamilies, IStockManager sm, PropagateToken token)
        {
            logger.Info("Registering families {0} for stock manager {1}", productFamilies, sm.GetUID());
            lock (familiesMap)
            {
                foreach (string family in productFamilies)
                {
                    if (!familiesMap.ContainsKey(family))
                    {
                        familiesMap.Add(family, new Dictionary <string, IStockManager>());
                    }

                    var dic = familiesMap[family];
                    if (!dic.ContainsKey(sm.GetUID()))
                    {
                        dic.Add(sm.GetUID(), sm);
                    }
                    else
                    {
                        logger.Warn("Stock manager {0} already registered, discarding", sm.GetUID());
                    }
                }
            }
            SendServerAction(token);
        }
Ejemplo n.º 2
0
        public void SendServerAction(PropagateToken token)
        {
            if (token == null)
            {
                return; //Do not propagate
            }
            token.AddHop(this);
            var servCopy = nextRingServer;

            ThreadPool.QueueUserWorkItem((result) =>
            {
                try
                {
                    logger.Debug("Propagating action {0} to server {1} on index.", token.Action.ToString(), servCopy.RingIndex);
                    servCopy.ServerProxy.ReceiveServerAction(token);
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    logger.Debug(ex, "Exception on propagate to index {0}", servCopy.RingIndex);
                    logger.Error("Error propagating action {0} to server {1} on index, removing server.",
                                 token.Action.ToString(), servCopy.RingIndex);

                    RemoveZoneServer(servCopy.UID);
                    if (servCopy.UID != nextRingServer.UID && nextRingServer.UID != this.serverUid)
                    {
                        SendServerAction(token);
                    }
                }
            });
        }
Ejemplo n.º 3
0
        public Server()
        {
            var key = System.Configuration.ConfigurationManager.AppSettings[ConfigurationFactory.SERVER_RING_INDEX_KEY];

            ringIndex     = Int32.Parse(key);
            discoverToken = new PropagateToken(PropagateAction.AddServer, this);
            myServer      = new CachedServer(this);
        }
Ejemplo n.º 4
0
 public void Unregister(IStockManager sm, PropagateToken token)
 {
     logger.Info("Unregistering stock manager {0}", sm.GetUID());
     lock (familiesMap)
     {
         foreach (var entry in familiesMap)
         {
             if (entry.Value.ContainsKey(sm.GetUID()))
             {
                 entry.Value.Remove(sm.GetUID());
             }
         }
     }
     SendServerAction(token);
 }
Ejemplo n.º 5
0
        public void AddZoneServer(IServerExtended server, PropagateToken token)
        {
            logger.Info("Adding zone server with index {0}", server.RingIndex);
            lock (knownServers)
            {
                checkRingData(server);
                if (!knownServers.ContainsKey(server.GetUID()))
                {
                    CachedServer cached = new CachedServer(server);
                    knownServers.Add(cached.UID, cached);
                }

                nextRingServer = getNextServerOnRing();
            }
            SendServerAction(token);
        }
Ejemplo n.º 6
0
        private bool checkDiscovery(PropagateToken token)
        {
            if (discoveryComplete)
            {
                return(false);
            }

            lock (knownServers)
            {
                if (!discoveryComplete && (discoverToken.GetUID() == token.GetUID()))
                {
                    discoveryComplete = true;
                    logger.Info("Received discovery token. Server is on the ring.");
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 7
0
        public void RemoveZoneServer(string serverUid, PropagateToken token)
        {
            CachedServer cachedServer;

            if (!knownServers.TryGetValue(serverUid, out cachedServer))
            {
                logger.Info("Cannot remove unknown server with uid {0}", serverUid);
                return;
            }

            logger.Info("Removing zone server with index {0}", cachedServer.RingIndex);
            lock (knownServers)
            {
                knownServers.Remove(cachedServer.UID);

                if (cachedServer.UID == nextRingServer.UID)
                {
                    nextRingServer = getNextServerOnRing();
                }
            }

            SendServerAction(token);
        }
Ejemplo n.º 8
0
        public void ReceiveServerAction(PropagateToken token)
        {
            if (checkDiscovery(token))
            {
                return;
            }

            logger.Debug("Token {0} received for action {1}", token.GetUID(), token.Action.ToString());
            if (token.HasHopedOnObject(this))
            {
                logger.Debug("Token {0} already processed, discarding", token.GetUID());
                return;
            }


            object[] pars;
            switch (token.Action)
            {
            case PropagateAction.AddServer:
                AddZoneServer((IServerExtended)token.Data, token);
                break;

            case PropagateAction.RemoveServer:
                RemoveZoneServer((string)token.Data, token);
                break;

            case PropagateAction.Register:
                pars = (object[])token.Data;
                Register((string[])pars[0], (IStockManager)pars[1], token);
                break;

            case PropagateAction.Unregister:
                Unregister((IStockManager)token.Data, token);
                break;
            }
        }