Exemplo n.º 1
0
        private bool Connect(Domain.Entities.Network net, DetectedNode n)
        {
            try
            {
                LogManager.GetLogger("faplog").Info("Client connecting to {0}", n.Address);
                net.State = ConnectionState.Connecting;

                var verb = new ConnectVerb();
                verb.ClientType = ClientType.Client;
                verb.Address    = model.LocalNode.Location;
                verb.Secret     = IDService.CreateID();
                var client = new Client(model.LocalNode);

                transmitted.Data.Clear();
                foreach (var info in model.LocalNode.Data.ToList())
                {
                    transmitted.SetData(info.Key, info.Value);
                }

                net.Overlord          = new Node();
                net.Overlord.Location = n.Address;
                net.Overlord.Secret   = verb.Secret;
                LogManager.GetLogger("faplog").Debug("Client using secret {0}", verb.Secret);
                if (client.Execute(verb, n.Address))
                {
                    net.State       = ConnectionState.Connected;
                    net.Overlord.ID = verb.OverlordID;
                    LogManager.GetLogger("faplog").Info("Client connected");
                    return(true);
                }
                else
                {
                    net.Overlord = new Node();
                }
            }
            catch
            {
                net.State = ConnectionState.Disconnected;
            }
            return(false);
        }
Exemplo n.º 2
0
        private void ProcessLanConnection(object o)
        {
            mserver.SendMessage(WhoVerb.CreateRequest());
            Domain.Entities.Network network = model.Network;
            network.PropertyChanged += network_PropertyChanged;
            while (run)
            {
                if (network.State != ConnectionState.Connected)
                {
                    //Not connected so connect automatically..

                    //Regenerate local secret to stop any updates if we reconnecting..
                    network.Overlord        = new Node();
                    network.Overlord.Secret = IDService.CreateID();
                    //Clear old peers
                    network.Nodes.Clear();

                    //Build up a prioritised server list
                    var availibleNodes = new List <DetectedNode>();

                    List <DetectedNode> detectedPeers = peerFinder.Peers.ToList();

                    //Prioritise a server we havent connected to already
                    foreach (DetectedNode peer in detectedPeers)
                    {
                        if (attemptedPeers.Where(s => s.Node == peer).Count() == 0)
                        {
                            availibleNodes.Add(peer);
                        }
                    }
                    foreach (LanPeer peer in attemptedPeers.OrderByDescending(x => x.LastConnectionTime))
                    {
                        availibleNodes.Add(peer.Node);
                    }

                    while (network.State != ConnectionState.Connected && availibleNodes.Count > 0)
                    {
                        DetectedNode node = availibleNodes[0];
                        availibleNodes.RemoveAt(0);
                        if (!Connect(network, node))
                        {
                            peerFinder.RemovePeer(node);
                        }
                    }
                }
                if (network.State == ConnectionState.Connected)
                {
                    CheckModelChanges();
                    //Check for network timeout

                    if ((Environment.TickCount - model.Network.Overlord.LastUpdate) > Model.UPLINK_TIMEOUT)
                    {
                        //We havent recently sent/recieved so went a noop so check we are still connected.
                        var req = new NetworkRequest
                        {
                            Verb     = "NOOP",
                            SourceID = model.LocalNode.ID,
                            AuthKey  = model.Network.Overlord.Secret
                        };
                        var client = new Client(model.LocalNode);
                        if (!client.Execute(req, model.Network.Overlord, 4000))
                        {
                            if (network.State == ConnectionState.Connected)
                            {
                                Disconnect();
                            }
                        }
                    }

                    workerEvent.WaitOne(10000);
                }
                else
                {
                    workerEvent.WaitOne(100);
                }
            }
        }