Пример #1
0
    /// <summary>Returns the 4 oldest connections.</summary>
    protected List<Connection> GetClosest(ConnectionList cons)
    {
      List<Connection> lcons = new List<Connection>(cons.Count);
      foreach(Connection con in cons) {
        lcons.Add(con);
      }

      return GetClosest(lcons);
    }
Пример #2
0
 /// <summary>
 /// Constructs with the provided input/output node count, activation function library, 
 /// node and connection lists.
 /// </summary>
 public NetworkDefinition(int inputNodeCount, int outputNodeCount,
                          IActivationFunctionLibrary activationFnLib,
                          NodeList nodeList, ConnectionList connectionList)
 {
     _inputNodeCount = inputNodeCount;
     _outputNodeCount = outputNodeCount;
     _activationFnLib = activationFnLib;
     _nodeList = nodeList;
     _connectionList = connectionList;
     _isAcyclic = !CyclicNetworkTest.IsNetworkCyclic(this);
 }
Пример #3
0
		public Neuron(IActivationFunction activationFn, NeuronType neuronType, uint id)
		{
			this.activationFn = activationFn;
			this.neuronType = neuronType;
			this.id = id;
			connectionList = new ConnectionList();

			if(neuronType == NeuronType.Bias)
				this.outputValue = 1.0D;
			else
				this.outputValue = 0.0D;
		}
Пример #4
0
        public static ConnectionList Build(int leftPerceptrons, int rightPerceptrons, ConnectionProperties connectionProperties)
        {
            var connectionList = new ConnectionList();

            for (int i = 0; i < leftPerceptrons; i++)
            {
                for (int j = 0; j < rightPerceptrons; j++)
                {
                    connectionList.Add(new Connection(i + 1, j + 1, connectionProperties));
                }
            }

            return connectionList;
        }
Пример #5
0
 public static void Dispose()
 {
     ConnectionList?.Dispose();
 }
Пример #6
0
 public ConnectionEventArgs(Connection c, int idx, ConnectionList cl) {
   Connection = c;
   Index = idx;
   CList = cl;
 }
Пример #7
0
        /// <summary>
        /// 处理 ClientEnterNetwork 消息。
        /// </summary>
        /// <param name="args">处理时需要的信息。</param>
        private void HandleClientEnterNetwork(HandleMessageArgs args)
        {
            Logger.Log("收到消息: 客户端加入分布网络。");
            KEndPoint remoteEndPoint   = args.Message.Header.SourceEndPoint;
            int       isMessageHandled = 0;
            ushort    realPort         = 0;

            if (args.Message.Content.Data.Length > 0)
            {
                BEncodedDictionary dict;
                dict             = BEncodedValue.Decode <BEncodedDictionary>(args.Message.Content.Data);
                isMessageHandled = (int)((dict["message handled"] as BEncodedNumber).Number);
                realPort         = (ushort)((dict["real port"] as BEncodedNumber).Number);
            }
            string enterArgsRecord = "ClientEnterNetwork Args:";

            enterArgsRecord += Environment.NewLine + "isMessageHandled: " + isMessageHandled.ToString();
            enterArgsRecord += Environment.NewLine + "realPort: " + realPort.ToString();
            Logger.Log(enterArgsRecord);
            args.RealPort = realPort;
            if (!ConnectionList.Contains(remoteEndPoint))
            {
                if (isMessageHandled == 0)
                {
                    Logger.Log("本客户端是第一个收到这条进入消息的客户端。" + Environment.NewLine + "本机的连接列表如下。");
                    StringBuilder sb = new StringBuilder(100);
                    lock (ConnectionList)
                    {
                        foreach (var item in ConnectionList)
                        {
                            sb.AppendLine(item.ToString());
                        }
                    }
                    Logger.Log(sb.ToString());

                    Logger.Log("将当前连接信息编码并发送。我眼中的对方: " + args.EndPoint.ToString());
                    // 将自己的连接列表和用户列表编码,准备发送到连接来的客户端
                    var data = EncodeTargetInformation(args.EndPoint);
                    // 然后要修正 remoteEndPoint,因为加入消息必定是 127.0.0.1:ep 形式,所以要修正为实际的 ep
                    remoteEndPoint = KEndPoint.FromEndPoint(args.EndPoint);
                    try
                    {
                        // 先返回接下来的字节大小
                        args.Stream.WriteInt32(data.Length);
                        // 然后一次性发送
                        args.Stream.Write(data, 0, data.Length);
                        args.Stream.Flush();
                    }
                    catch (Exception ex)
                    {
                        // 首次通信就失败了……
                        Logger.Log(ex.Message);
                        Logger.Log(ex.StackTrace);
                    }
                }
            }

            KMessage message = args.Message;

            if (isMessageHandled == 0)
            {
                // 注意:虽然 KMessage 是一个值类型,但是其中含有引用类型(数组),这里修改了这个引用类型
                Logger.Log("设置该消息为处理过。");
                message.Content.Data = BitConverter.GetBytes((int)1);
            }
            else
            {
                // 本客户端不是第一个处理的,那就报告存活吧
                SendMessage(remoteEndPoint, MessageFactory.ReportAlive(LocalKEndPoint));
            }
            Logger.Log("转发消息。");
            // 转发
            BroadcastMessage(message);
        }
Пример #8
0
 public AHState UpdateLeafs(ConnectionList l) {
   return new AHState(this, l);
 }
Пример #9
0
    protected AHState(AHState old_state, ConnectionList leafs) {
      Leafs = leafs;
      Structs = old_state.Structs;
      Local = old_state.Local;

      _directional = old_state._directional;
      _greedy = old_state._greedy;
      _annealing = old_state._annealing;
    }
Пример #10
0
 public AnnealingRouting(AHAddress local, ConnectionList structured_cons) {
   //Fake connection to ourselves:
   _local_con = new Connection(null, local, "structured.self", null, null);
   int local_idx;
   _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
 }
Пример #11
0
    /// <summary>Attempt to find the overlap in a remote RelayTransportAddress
    /// and our local node.  This will be used to help communicate with a new
    /// tunneled peer.</summary>
    public virtual List<Connection> FindOverlap(RelayTransportAddress ta, ConnectionList cons)
    {
      List<Connection> overlap = new List<Connection>();
      foreach(Connection con in cons) {
        if(ta.ContainsForwarder(con.Address)) {
          overlap.Add(con);
        }
      }

      return GetOldest(overlap);
    }
Пример #12
0
        /// <summary>
        /// Create a network definition by querying the provided IBlackBox (typically a CPPN) with the 
        /// substrate connection endpoints.
        /// </summary>
        /// <param name="blackbox">The HyperNEAT CPPN that defines the strength of connections between nodes on the substrate.</param>
        /// <param name="lengthCppnInput">Optionally we provide a connection length input to the CPPN.</param>
        public INetworkDefinition CreateNetworkDefinition(IBlackBox blackbox, bool lengthCppnInput)
        {
            // Get the sequence of substrate connections. Either a pre-built list or a dynamically
            // generated sequence.
            IEnumerable<SubstrateConnection> connectionSequence = _connectionList ?? GetConnectionSequence();

            // Iterate over substrate connections. Determine each connection's weight and create a list
            // of network definition connections.
            ISignalArray inputSignalArr = blackbox.InputSignalArray;
            ISignalArray outputSignalArr = blackbox.OutputSignalArray;
            ConnectionList networkConnList = new ConnectionList(_connectionCountHint);
            int lengthInputIdx = inputSignalArr.Length - 1;

            foreach(SubstrateConnection substrateConn in connectionSequence)
            {
                int layerToLayerAdder = ((int)substrateConn._tgtNode._position[2] * 3);
                // Assign the connection's endpoint position coords to the CPPN/blackbox inputs. Note that position dimensionality is not fixed.
                for(int i=0; i<_dimensionality - 1; i++)
                {
                    inputSignalArr[i] = substrateConn._srcNode._position[i];
                    inputSignalArr[i + _dimensionality - 1] = substrateConn._tgtNode._position[i];
                    inputSignalArr[i + 2 * (_dimensionality - 1)] = Math.Abs(substrateConn._srcNode._position[i] - substrateConn._tgtNode._position[i]);
                }

                // Optional connection length input.
                if(lengthCppnInput) {
                    inputSignalArr[lengthInputIdx] = CalculateConnectionLength(substrateConn._srcNode._position, substrateConn._tgtNode._position);
                }

                // Reset blackbox state and activate it.
                blackbox.ResetState();
                blackbox.Activate();

                // Read connection weight from output 0.
                double weight = outputSignalArr[0 + layerToLayerAdder];

                // Skip connections with a weight magnitude less than _weightThreshold.
                double weightAbs = Math.Abs(weight);
                if (outputSignalArr[2 + layerToLayerAdder] >= 0)
                {
                    // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight],
                    // assuming IBlackBox outputs are in the range [-1,1].
                    weight = (weightAbs) * _weightRescalingCoeff * Math.Sign(weight);

                    // Create network definition connection and add to list.
                    networkConnList.Add(new NetworkConnection(substrateConn._srcNode._id,
                                                              substrateConn._tgtNode._id, weight));
                }
            }

            // Additionally we create connections from each hidden and output node to a bias node that is not defined at any
            // position on the substrate. The motivation here is that a each node's input bias is independent of any source
            // node (and associated source node position on the substrate). That we refer to a bias 'node' is a consequence of how input
            // biases are handled in NEAT - with a specific bias node that other nodes can be connected to.
            int setCount = _nodeSetList.Count;
            for(int i=1; i<setCount; i++)
            {
                SubstrateNodeSet nodeSet = _nodeSetList[i];
                foreach(SubstrateNode node in nodeSet.NodeList)
                {
                    // Assign the node's position coords to the blackbox inputs. The CPPN inputs for source node coords are set to zero when obtaining bias values.
                    for(int j=0; j<_dimensionality - 1; j++)
                    {
                        inputSignalArr[j] = 0.0;
                        inputSignalArr[j + _dimensionality - 1] = node._position[j];
                    }

                    // Optional connection length input.
                    if(lengthCppnInput) {
                        inputSignalArr[lengthInputIdx] = CalculateConnectionLength(node._position);
                    }

                    // Reset blackbox state and activate it.
                    blackbox.ResetState();
                    blackbox.Activate();

                    // Read bias connection weight from output 1.
                    double weight = outputSignalArr[1+ (i- 1) * 3];

                    // Skip connections with a weight magnitude less than _weightThreshold.
                    double weightAbs = Math.Abs(weight);
                    if(weightAbs > _weightThreshold)
                    {
                        // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight],
                        // assuming IBlackBox outputs are in the range [-1,1].
                        weight = (weightAbs - _weightThreshold) * _weightRescalingCoeff * Math.Sign(weight);

                        // Create network definition connection and add to list. Bias node is always ID 0.
                        networkConnList.Add(new NetworkConnection(0, node._id, weight));
                    }
                }
            }

            // Check for no connections.
            // If no connections were generated then there is no point in further evaulating the network.
            // However, null is a valid response when decoding genomes to phenomes, therefore we do that here.
            if(networkConnList.Count == 0) {
                return null;
            }

            // Construct and return a network definition.
            NetworkDefinition networkDef = new NetworkDefinition(_inputNodeCount, _outputNodeCount,
                                                                 _activationFnLibrary, _netNodeList, networkConnList);

            // Check that the definition is valid and return it.
            Debug.Assert(networkDef.PerformIntegrityCheck());
            return networkDef;
        }
Пример #13
0
 public AHState UpdateLeafs(ConnectionList l)
 {
     return(new AHState(this, l));
 }
Пример #14
0
        public static void LoadConnections(bool withDialog = false, bool update = false)
        {
            var connectionsLoader = new ConnectionsLoader();

            try
            {
                // disable sql update checking while we are loading updates
                SQLConnProvider?.Disable();

                if (ConnectionList != null && ContainerList != null)
                {
                    PreviousConnectionList = ConnectionList.Copy();
                    PreviousContainerList  = ContainerList.Copy();
                }

                ConnectionList = new ConnectionList();
                ContainerList  = new ContainerList();

                if (!Settings.Default.UseSQLServer)
                {
                    if (withDialog)
                    {
                        var loadDialog = Tools.Controls.ConnectionsLoadDialog();
                        if (loadDialog.ShowDialog() != DialogResult.OK)
                        {
                            return;
                        }
                        connectionsLoader.ConnectionFileName = loadDialog.FileName;
                    }
                    else
                    {
                        connectionsLoader.ConnectionFileName = GetStartupConnectionFileName();
                    }

                    CreateBackupFile(Convert.ToString(connectionsLoader.ConnectionFileName));
                }

                connectionsLoader.ConnectionList = ConnectionList;
                connectionsLoader.ContainerList  = ContainerList;

                if (PreviousConnectionList != null && PreviousContainerList != null)
                {
                    connectionsLoader.PreviousConnectionList = PreviousConnectionList;
                    connectionsLoader.PreviousContainerList  = PreviousContainerList;
                }

                if (update)
                {
                    connectionsLoader.PreviousSelected = LastSelected;
                }

                ConnectionTree.ResetTree();

                connectionsLoader.RootTreeNode     = Windows.treeForm.tvConnections.Nodes[0];
                connectionsLoader.UseDatabase      = Settings.Default.UseSQLServer;
                connectionsLoader.DatabaseHost     = Settings.Default.SQLHost;
                connectionsLoader.DatabaseName     = Settings.Default.SQLDatabaseName;
                connectionsLoader.DatabaseUsername = Settings.Default.SQLUser;
                var cryptographyProvider = new LegacyRijndaelCryptographyProvider();
                connectionsLoader.DatabasePassword = cryptographyProvider.Decrypt(Convert.ToString(Settings.Default.SQLPass), GeneralAppInfo.EncryptionKey);
                connectionsLoader.DatabaseUpdate   = update;
                connectionsLoader.LoadConnections(false);

                if (Settings.Default.UseSQLServer)
                {
                    LastSqlUpdate = DateTime.Now;
                }
                else
                {
                    if (connectionsLoader.ConnectionFileName == GetDefaultStartupConnectionFileName())
                    {
                        Settings.Default.LoadConsFromCustomLocation = false;
                    }
                    else
                    {
                        Settings.Default.LoadConsFromCustomLocation = true;
                        Settings.Default.CustomConsPath             = connectionsLoader.ConnectionFileName;
                    }
                }

                // re-enable sql update checking after updates are loaded
                SQLConnProvider?.Enable();
            }
            catch (Exception ex)
            {
                if (Settings.Default.UseSQLServer)
                {
                    MessageCollector.AddExceptionMessage(Language.strLoadFromSqlFailed, ex);
                    var commandButtons = string.Join("|", Language.strCommandTryAgain, Language.strCommandOpenConnectionFile, string.Format(Language.strCommandExitProgram, Application.ProductName));
                    CTaskDialog.ShowCommandBox(Application.ProductName, Language.strLoadFromSqlFailed, Language.strLoadFromSqlFailedContent, MiscTools.GetExceptionMessageRecursive(ex), "", "", commandButtons, false, ESysIcons.Error, ESysIcons.Error);
                    switch (CTaskDialog.CommandButtonResult)
                    {
                    case 0:
                        LoadConnections(withDialog, update);
                        return;

                    case 1:
                        Settings.Default.UseSQLServer = false;
                        LoadConnections(true, update);
                        return;

                    default:
                        Application.Exit();
                        return;
                    }
                }
                if (ex is FileNotFoundException && !withDialog)
                {
                    MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoadedNew, connectionsLoader.ConnectionFileName), ex, MessageClass.InformationMsg);
                    NewConnections(Convert.ToString(connectionsLoader.ConnectionFileName));
                    return;
                }

                MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoaded, connectionsLoader.ConnectionFileName), ex);
                if (connectionsLoader.ConnectionFileName != GetStartupConnectionFileName())
                {
                    LoadConnections(withDialog, update);
                }
                else
                {
                    MessageBox.Show(frmMain.Default,
                                    string.Format(Language.strErrorStartupConnectionFileLoad, Environment.NewLine, Application.ProductName, GetStartupConnectionFileName(), MiscTools.GetExceptionMessageRecursive(ex)),
                                    "Could not load startup file.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }
        }
Пример #15
0
        /// <summary>
        /// Create a network definition by querying the provided IBlackBox (typically a CPPN) with the
        /// substrate connection endpoints.
        /// </summary>
        /// <param name="blackbox">The HyperNEAT CPPN that defines the strength of connections between nodes on the substrate.</param>
        /// <param name="lengthCppnInput">Optionally we provide a connection length input to the CPPN.</param>
        public INetworkDefinition CreateNetworkDefinition(IBlackBox blackbox, bool lengthCppnInput)
        {
            // Iterate over substrate connections. Determine each connection's weight and create a list
            // of network definition connections.
            ISignalArray   inputSignalArr  = blackbox.InputSignalArray;
            ISignalArray   outputSignalArr = blackbox.OutputSignalArray;
            ConnectionList networkConnList = new ConnectionList(_connectionCountHint);
            int            lengthInputIdx  = Dimensionality + Dimensionality;

            for (int i = 0; i < N; i++)
            {
                foreach (var substrateConnection in _connectionList[i])
                {
                    for (int j = 0; j < Dimensionality; j++)
                    {
                        inputSignalArr[j] = substrateConnection._srcNode._position[j];
                        inputSignalArr[j + Dimensionality] = substrateConnection._tgtNode._position[j];
                    }
                    // Optional connection length input.
                    if (lengthCppnInput)
                    {
                        inputSignalArr[lengthInputIdx] = CalculateConnectionLength(substrateConnection._srcNode._position, substrateConnection._tgtNode._position);
                    }
                    blackbox.ResetState();
                    blackbox.Activate();
                    double weight = outputSignalArr[i];

                    //if LEO is toggled query for expression
                    double expressionWeight = -0.1;
                    if (Leo)
                    {
                        expressionWeight = outputSignalArr[i + M + N];
                    }
                    // Skip connections with a weight magnitude less than _weightThreshold.
                    double weightAbs = Math.Abs(weight);
                    if (!Leo && weightAbs > _weightThreshold || Leo && expressionWeight >= 0.0)
                    {
                        // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight],
                        // assuming IBlackBox outputs are in the range [-1,1].
                        weight = (weightAbs - _weightThreshold) * _weightRescalingCoeff * Math.Sign(weight);

                        // Create network definition connection and add to list.
                        networkConnList.Add(new NetworkConnection(substrateConnection._srcNode._id,
                                                                  substrateConnection._tgtNode._id, weight));
                    }
                }
            }
            var biasOutputIdx = N;

            foreach (var nodeSet in _outputLayers.Concat(_hiddenLayers))
            {
                foreach (var node in nodeSet.NodeList)
                {
                    // Assign the node's position coords to the blackbox inputs. The CPPN inputs for source node coords are set to zero when obtaining bias values.
                    for (int j = 0; j < Dimensionality; j++)
                    {
                        inputSignalArr[j] = 0.0;
                        inputSignalArr[j + Dimensionality] = node._position[j];
                    }

                    // Optional connection length input.
                    if (lengthCppnInput)
                    {
                        inputSignalArr[lengthInputIdx] = CalculateConnectionLength(node._position);
                    }

                    // Reset blackbox state and activate it.
                    blackbox.ResetState();
                    blackbox.Activate();

                    // Read bias connection weight from output 1.
                    double weight = outputSignalArr[biasOutputIdx];
                    // Skip connections with a weight magnitude less than _weightThreshold.
                    double weightAbs = Math.Abs(weight);
                    if (weightAbs > _weightThreshold)
                    {
                        // For weights over the threshold we re-scale into the range [-_maxWeight,_maxWeight],
                        // assuming IBlackBox outputs are in the range [-1,1].
                        weight = (weightAbs - _weightThreshold) * _weightRescalingCoeff * Math.Sign(weight);

                        // Create network definition connection and add to list. Bias node is always ID 0.
                        networkConnList.Add(new NetworkConnection(0, node._id, weight));
                    }
                }
                biasOutputIdx++;
            }

            // Check for no connections.
            // If no connections were generated then there is no point in further evaulating the network.
            // However, null is a valid response when decoding genomes to phenomes, therefore we do that here.
            if (networkConnList.Count == 0)
            {
                return(null);
            }

            // Construct and return a network definition.
            NetworkDefinition networkDef = new NetworkDefinition(_inputLayers.Sum(x => x.NodeList.Count), _outputLayers.Sum(x => x.NodeList.Count),
                                                                 _activationFnLibrary, _netNodeList, networkConnList);

            // Check that the definition is valid and return it.
            Debug.Assert(networkDef.PerformIntegrityCheck());
            return(networkDef);
        }
Пример #16
0
        /// <summary>Make sure there are no entries in the Dht, who we should be
        /// connected to, but aren't.</summary>
        protected void CheckAndUpdateRemoteTAs(List <TransportAddress> tas)
        {
            AHAddress  right = null, left = null;
            BigInteger right_dist = null, left_dist = null;
            AHAddress  addr = _node.Address as AHAddress;

            // Find the closest left and right nodes
            foreach (TransportAddress ta in tas)
            {
                AHAddress target = (ta as SubringTransportAddress).Target;
                if (target.Equals(addr))
                {
                    continue;
                }
                BigInteger ldist = addr.LeftDistanceTo(target);
                BigInteger rdist = addr.RightDistanceTo(target);

                if (left_dist == null || ldist < left_dist)
                {
                    left_dist = ldist;
                    left      = target;
                }

                if (right_dist == null || rdist < right_dist)
                {
                    right_dist = rdist;
                    right      = target;
                }
            }

            ConnectionList cl        = _node.ConnectionTable.GetConnections(ConnectionType.Structured);
            int            local_idx = ~cl.IndexOf(_node.Address);

            if (left != null)
            {
                int remote_idx = ~cl.IndexOf(left);
                // If we're not connected to the left closest and its closer than any
                // of our current peers, let's connect to it
                if (remote_idx > 0 && Math.Abs(local_idx - remote_idx) < 2)
                {
                    List <TransportAddress> tmp_tas = new List <TransportAddress>(1);
                    tmp_tas.Add(new SubringTransportAddress(left, _shared_namespace));
                    Linker linker = new Linker(_node, null, tmp_tas, "leaf", addr.ToString());
                    linker.Start();
                }
            }

            if (right != null && right != left)
            {
                int remote_idx = ~cl.IndexOf(right);
                // If we're not connected to the right closest and its closer than any
                // of our current peers, let's connect to it
                if (remote_idx > 0 && Math.Abs(local_idx - remote_idx) < 2)
                {
                    List <TransportAddress> tmp_tas = new List <TransportAddress>(1);
                    tas.Add(new SubringTransportAddress(right, _shared_namespace));
                    Linker linker = new Linker(_node, null, tmp_tas, "leaf", addr.ToString());
                    linker.Start();
                }
            }

            UpdateRemoteTAs(tas);
        }
Пример #17
0
        /**
         * This is a recursive function over the network
         * It helps do a link-reliable procedure call on the
         * on the overlay network.
         */
        protected void RecursiveCall(IList margs, object req_state)
        {
            //first argument is the target node.
            AHAddress a = (AHAddress)AddressParser.Parse((string)margs[0]);

            /*
             * First find the Connection pointing to the node closest to dest, if
             * there is one closer than us
             */

            ConnectionTable tab          = _node.ConnectionTable;
            ConnectionList  structs      = tab.GetConnections(ConnectionType.Structured);
            Connection      next_closest = structs.GetNearestTo((AHAddress)_node.Address, a);

            //Okay, we have the next closest:
            if (next_closest != null)
            {
                Channel result = new Channel();
                //We only want one result, so close the queue after we get the first
                result.CloseAfterEnqueue();
                result.CloseEvent += delegate(object o, EventArgs args) {
                    Channel q = (Channel)o;
                    if (q.Count > 0)
                    {
                        try {
                            RpcResult rres = (RpcResult)q.Dequeue();
                            _rpc.SendResult(req_state, rres.Result);
                        }
                        catch (Exception x) {
                            string    m  = String.Format("<node>{0}</node> trying <connection>{1}</connection> got <exception>{2}</exception>", _node.Address, next_closest, x);
                            Exception nx = new Exception(m);
                            _rpc.SendResult(req_state, nx);
                        }
                    }
                    else
                    {
                        //We got no results.
                        _rpc.SendResult(req_state, null);
                    }
                };
                object [] new_args = new object[margs.Count];
                margs.CopyTo(new_args, 0);
                _rpc.Invoke(next_closest.Edge, result, "trace.RecursiveCall", new_args);
            }
            else
            {
                //We are the end of the line, send the result:
                //Console.Error.WriteLine("Doing a local invocation");
                Channel result = new Channel();
                result.CloseAfterEnqueue();
                result.CloseEvent += delegate(object o, EventArgs args) {
                    Channel q = (Channel)o;
                    if (q.Count > 0)
                    {
                        try {
                            //Console.Error.WriteLine("Got result.");
                            RpcResult rres = (RpcResult)q.Dequeue();
                            _rpc.SendResult(req_state, rres.Result);
                        }
                        catch (Exception x) {
                            string    m  = String.Format("<node>{0}</node> local invocation got <exception>{1}</exception>", _node.Address, x);
                            Exception nx = new Exception(m);
                            _rpc.SendResult(req_state, nx);
                        }
                    }
                    else
                    {
                        //We got no results.
                        _rpc.SendResult(req_state, null);
                    }
                };

                string    method_name = (string)margs[1];
                object [] new_args    = new object[margs.Count - 2];
                margs.RemoveAt(0); //extract destination address
                margs.RemoveAt(0); //extract method name
                margs.CopyTo(new_args, 0);
                //Console.Error.WriteLine("Calling method: {0}, args_count: {1}", method_name, new_args.Length);
                //for (int i = 0; i < new_args.Length; i++) {
                //Console.Error.WriteLine(new_args[i]);
                //}
                _rpc.Invoke(_node, result, method_name, new_args);
            }
        }
Пример #18
0
 /// <summary>Returns our nearest neighbors to the specified address, which
 /// is in turn used to help communicate with tunnel peer.</summary>
 public static List<Address> GetNearest(Address addr, ConnectionList cons)
 {
   ConnectionList cons_near = cons.GetNearestTo(addr, 16);
   List<Address> addrs = new List<Address>();
   foreach(Connection con in cons_near) {
     addrs.Add(con.Address);
   }
   return addrs;
 }
Пример #19
0
    /// <summary>Returns a Relay Sync message containing up to 40 addresses
    /// first starting with previous overlap followed by new potential
    /// connections for overlap.</summary>
    public virtual IDictionary GetSyncMessage(IList<Connection> current_overlap,
        Address local_addr, ConnectionList cons)
    {
      Hashtable ht = new Hashtable();
      DateTime now = DateTime.UtcNow;
      if(current_overlap != null) {
        foreach(Connection con in current_overlap) {
          Hashtable info = new Hashtable(2);
          info["ta"] = TransportAddress.TATypeToString(con.State.Edge.TAType);
          info["ct"] = (int) (now - con.CreationTime).TotalMilliseconds;
          ht[con.Address.ToMemBlock()] = info;
        }
      }

      int idx = cons.IndexOf(local_addr);
      if(idx < 0) {
        idx = ~idx;
      }
      int max = cons.Count < 16 ? cons.Count : 16;
      int start = idx - max / 2;
      int end = idx + max / 2;

      for(int i = start; i < end; i++) {
        Connection con = cons[i];
        MemBlock key = con.Address.ToMemBlock();
        if(ht.Contains(key)) {
          continue;
        }
        Hashtable info = new Hashtable();
        info["ta"] = TransportAddress.TATypeToString(con.State.Edge.TAType);
        info["ct"] = (int) (now - con.CreationTime).TotalMilliseconds;
        ht[key] = info;
      }

      return ht;
    }
Пример #20
0
        /// <param name="persistantObjectName">Will be used as CustomObject name to get the object instance</param>
        public static PageFile GetInstance(ConnectionList connectionList, string connectionID, string persistantObjectName=null, HttpRequest request=null, Func<PageFile> createCallback=null)
        {
            if( persistantObjectName == null )
            {
                if( request == null )
                    throw new ArgumentException( "Either 'fileID' parameter or 'request' must be specified" );
                persistantObjectName = request.QueryString[ RqstFileID ];
            }
            if( string.IsNullOrEmpty(persistantObjectName) )
                throw new ArgumentException( "The connection's custom object name could not be determined. Either the 'fileID' parameter must be set or the 'request' must contain the HTTP parameter '" + RqstFileID + "'" );

            PageFile instance;
            if( createCallback == null )
            {
                instance = (PageFile)connectionList.GetConnectionCustomObject( connectionID, persistantObjectName );
            }
            else
            {
             				Func<object> createPageFileCallback = ()=>{ return createCallback(); };
                instance = (PageFile)connectionList.GetConnectionCustomObject( connectionID, persistantObjectName, createPageFileCallback );
            }
            return instance;
        }
Пример #21
0
 public GreedyRouting(AHAddress local, ConnectionList structured_cons) {
   //Fake connection to ourselves:
   _local_con = new Connection(null, local, "structured.self", null, null);
   int local_idx;
   _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
   _NO_ONE = new Pair<Connection, bool>(null, false);
   _LOCAL = new Pair<Connection, bool>(null, true);
 }
    /*
     * Check to see if any of this node's neighbors
     * should be neighbors of us.  If they should, connect
     * to the closest such nodes on each side.
     * 
     * @param structs a ConnectionList of ConnectionType.Structured
     * @param neighbors an IEnumerable of NodeInfo objects
     * @param nltarget an address of a node that should be on our left
     * @param nrtarget an address of a node that should be on our right
     */
    protected void CheckForNearerNeighbors(ConnectionList structs,
        IEnumerable neighbors, out Address nltarget, out Address nrtarget)
    {

      BigInteger ldist = null;
      BigInteger rdist = null;
      nltarget = null;
      nrtarget = null;
      AHAddress local = (AHAddress)_node.Address;
      foreach(NodeInfo ni in neighbors) {
        if( !( _node.Address.Equals(ni.Address) ||
               structs.Contains(ni.Address) ) ) {
          AHAddress adr = (AHAddress)ni.Address;
          int n_left = structs.LeftInclusiveCount(_node.Address, adr);
          int n_right = structs.RightInclusiveCount(_node.Address, adr);
          if( n_left < DESIRED_NEIGHBORS || n_right < DESIRED_NEIGHBORS ) {
            //We should connect to this node! if we are not already:
            BigInteger adr_dist = local.LeftDistanceTo(adr);
            if( ( null == ldist ) || adr_dist < ldist ) {
              ldist = adr_dist;
              nltarget = adr;
            }
            adr_dist = local.RightDistanceTo(adr);
            if( ( null == rdist ) || adr_dist < rdist ) {
              rdist = adr_dist;
              nrtarget = adr;
            }
          }
        }
      }
    }
Пример #23
0
 public DirectionalRouting(AHAddress local, ConnectionList structs) {
   _NULL_TRUE = new Pair<Connection, bool>(null, true);
   _NULL_FALSE = new Pair<Connection, bool>(null, false);
   Connection left_c = null;
   Connection right_c = null;
   if( structs.Count > 0) {
     int local_idx = ~structs.IndexOf(local);
     int left_idx = local_idx;
     int right_idx = left_idx - 1;
     left_c = structs[left_idx];
     right_c = structs[right_idx];
   }
   _LEFT_FALSE = new Pair<Connection, bool>(left_c, false);
   _RIGHT_FALSE = new Pair<Connection, bool>(right_c, false);
   _LEFT_TRUE = new Pair<Connection, bool>(left_c, true);
   _RIGHT_TRUE = new Pair<Connection, bool>(right_c, true);
 }
    /**
     * @param structs the ConnectionList to work with
     * @param target_to_for a mapping of Address -> Address, if we want to
     * connect to the key, the value should be the forwarder
     * @param neighs an IEnumerable of NodeInfo objects.
     */
    protected void ConnectToNearer(ConnectionList structs, 
                                   IDictionary target_to_for, IEnumerable neighs) {
      Address nltarget;
      Address nrtarget;
      CheckForNearerNeighbors(structs, neighs, out nltarget, out nrtarget);
      
      if( nrtarget != null ) {
        Address forwarder = (Address)target_to_for[nrtarget];
        ISender send = new ForwardingSender(_node, forwarder, nrtarget);
        ConnectTo(send, nrtarget, STRUC_NEAR, 1);
      }

      if( nltarget != null && !nltarget.Equals(nrtarget) ) {
        Address forwarder = (Address)target_to_for[nltarget];
        ISender send = new ForwardingSender(_node, forwarder, nltarget);
        ConnectTo(send, nltarget, STRUC_NEAR, 1);
      }
    }
Пример #25
0
 public AHState(AHAddress local, ConnectionList structs, ConnectionList leafs) {
   Leafs = leafs;
   Structs = structs;
   Local = local;
   
   _directional = new DirectionalRouting(local, structs);
   _greedy = new GreedyRouting(local, structs);
   _annealing = new AnnealingRouting(local, structs);
 }
Пример #26
0
 /**
  * Find neighbor connections within the range
  * return ArrayList of List<Connection> for left and right neighbors.
  */
 private Brunet.Collections.Pair<List<Connection>,List<Connection>> GetConnectionInfo(AHAddress t_addr, AHAddress start, AHAddress end, ConnectionList cl) {
    
   //this node is within the given range (start_addr, end_addr)
   List<Connection> left_con_list = new List<Connection>();
   List<Connection> right_con_list = new List<Connection>();
   foreach(Connection c in cl) {
     AHAddress adr = (AHAddress)c.Address;
     //if(adr.IsBetweenFromLeft(t_addr, end) ) {
     if (InRange(adr, start, t_addr) ) {
       left_con_list.Add(c);
     }
     //else if (adr.IsBetweenFromLeft(start, t_addr) ) {
     else if (InRange(adr, start, t_addr) ) {
       right_con_list.Add(c);
     }
     else {
       //Out of Range. Do nothing!
     }
   }
   //Make a compare and add it to ConnectionTable to sort by Address
   ConnectionLeftComparer left_cmp = new ConnectionLeftComparer(t_addr);
   left_con_list.Sort(left_cmp);
   ConnectionRightComparer right_cmp = new ConnectionRightComparer(t_addr);
   right_con_list.Sort(right_cmp);
   Brunet.Collections.Pair<List<Connection>,List<Connection>> ret = new Brunet.Collections.Pair<List<Connection>,List<Connection>>(left_con_list, right_con_list);
   return ret;
 }
Пример #27
0
 public AHState UpdateStructs(ConnectionList s) {
   return new AHState(Local, s, this.Leafs);
 }
Пример #28
0
    /// <summary>Returns a Tunnel Sync message containing all overlap and then
    /// the four fastest (if not included in the overlap.</summary>
    public override IDictionary GetSyncMessage(IList<Connection> current_overlap,
        Address local_addr, ConnectionList cons)
    {
      Hashtable ht = new Hashtable(40);
      DateTime now = DateTime.UtcNow;

      if(current_overlap != null) {
        foreach(Connection con in current_overlap) {
          Hashtable info = new Hashtable(3);
          info["ta"] = TransportAddress.TATypeToString(con.Edge.TAType);
          info["lat"] = _ncservice.GetMeasuredLatency(con.Address);
          info["ct"] = (int) (now - con.CreationTime).TotalMilliseconds;
          ht[con.Address.ToMemBlock()] = info;
        }
      }

      foreach(Connection con in GetClosest(cons)) {
        MemBlock key = con.Address.ToMemBlock();
        if(ht.Contains(key)) {
          continue;
        }
        // No need to verify it is >= 0, since addr comes from cons in a
        // previous stage
        Hashtable info = new Hashtable(3);
        info["ta"] = TransportAddress.TATypeToString(con.Edge.TAType);
        info["lat"] = _ncservice.GetMeasuredLatency(con.Address);
        info["ct"] = (int) (now - con.CreationTime).TotalMilliseconds;
        ht[key] = info;
      }

      return ht;
    }
    /**
     * Similar to the above except the forwarder is the same for all targets
     * @param cl ConnectionList of structs
     * @param forwarder the Node to forward through
     * @param ni an IEnumerable of NodeInfo objects representing neighbors
     * forwarder
     */
    protected void ConnectToNearer(ConnectionList cl, Address forwarder, IEnumerable ni)
    {
      Address nltarget;
      Address nrtarget;
      CheckForNearerNeighbors(cl, ni, out nltarget, out nrtarget);
      
      if( nrtarget != null ) {
        ISender send = new ForwardingSender(_node, forwarder, nrtarget);
        ConnectTo(send, nrtarget, STRUC_NEAR, 1);
      }

      if( nltarget != null && !nltarget.Equals(nrtarget) ) {
        ISender send = new ForwardingSender(_node, forwarder, nltarget);
        ConnectTo(send, nltarget, STRUC_NEAR, 1);
      }
    }
Пример #30
0
        /// <summary>Where data packets prepended with a tunnel come.  Here we
        /// receive data as well as create new RelayEdges.</summary>
        public void HandleData(MemBlock data, ISender return_path, object state)
        {
            AHSender         ah_from = return_path as AHSender;
            ForwardingSender fs_from = return_path as ForwardingSender;
            AHAddress        target  = null;

            if (ah_from == null)
            {
                if (fs_from == null)
                {
                    return;
                }
                target = (AHAddress)fs_from.Destination;
            }
            else
            {
                target = (AHAddress)ah_from.Destination;
            }

            int remote_id = NumberSerializer.ReadInt(data, 0);
            int local_id  = NumberSerializer.ReadInt(data, 4);

            RelayEdge te = null;

            // No locally assigned ID, so we'll create a new RelayEdge and assign it one.
            // This could be hit many times by the same RemoteID, but it is safe since
            // we'll attempt Linkers on all of them and he'll only respond to the first
            // one he receives back.
            if (local_id == -1)
            {
                if (fs_from == null)
                {
                    throw new Exception("No LocalID assigned but not from a useful sender!");
                }

                ConnectionList cons  = _connections;
                int            index = cons.IndexOf(fs_from.Forwarder);
                if (index < 0)
                {
                    return;
                }

                List <Connection> overlap_addrs = new List <Connection>();
                overlap_addrs.Add(cons[index]);

                while (true)
                {
                    te = new RelayEdge(this, (RelayTransportAddress)_local_tas[0],
                                       new RelayTransportAddress(target, overlap_addrs),
                                       _iasf.GetForwarderSelector(), overlap_addrs, remote_id);
                    lock (_sync) {
                        if (!_id_to_tunnel.ContainsKey(te.LocalID))
                        {
                            _id_to_tunnel[te.LocalID] = te;
                            break;
                        }
                    }
                    // Arriving here, implies another RelayEdge will be created and this
                    // one needs to be closed
                    te.Close();
                }

                local_id = te.LocalID;

                te.CloseEvent += CloseHandler;
                SendEdgeEvent(te);
            }

            if (!_id_to_tunnel.TryGetValue(local_id, out te))
            {
                // Maybe we closed this edge
                // throw new Exception("No such edge");
                // Old behavior would ignore these packets...
                return;
            }
            else if (te.RemoteID == -1)
            {
                // We created this, but we haven't received a packet yet
                te.RemoteID = remote_id;
            }
            else if (te.RemoteID != remote_id)
            {
                // Either we closed this edge and it was reallocated or something is up!
                // throw new Exception("Receiving imposter packet...");
                // Old behavior would ignore these packets...
                return;
            }

            if (te.IsClosed)
            {
                throw new Exception("Edge is closed...");
            }

            // Chop off the Ids
            data = data.Slice(8);
            te.ReceivedPacketEvent(data);
        }
Пример #31
0
    /// <summary>Returns the four fastest in the overlap.</summary>
    public override List<Connection> EvaluateOverlap(ConnectionList cons, IDictionary msg)
    {
      List<Connection> overlap = new List<Connection>();

      foreach(DictionaryEntry de in msg) {
        MemBlock key = de.Key as MemBlock;
        if(key == null) {
          key = MemBlock.Reference((byte[]) de.Key);
        }
        Address addr = new AHAddress(key);

        int index = cons.IndexOf(addr);
        if(index < 0) {
          continue;
        }

        Connection con = cons[index];

        // Since there are no guarantees about routing over two tunnels, we do
        // not consider cases where we are connected to the overlapping tunnels
        // peers via tunnels
        if(con.Edge.TAType.Equals(TransportAddress.TAType.Tunnel)) {
          Hashtable values = de.Value as Hashtable;
          TransportAddress.TAType tatype =
            TransportAddressFactory.StringToType(values["ta"] as string);
          if(tatype.Equals(TransportAddress.TAType.Tunnel)) {
            continue;
          }
        }
        overlap.Add(con);
      }

      return GetClosest(overlap);
    }
 public ConnectionEventArgs(Connection c, int idx, ConnectionList cl, int view) {
   Connection = c;
   Index = idx;
   CList = cl;
   View = view;
 }
Пример #33
0
        /// <summary>
        /// Reads a network definition from XML. 
        /// An activation function library is required to decode the function ID at each node, typically the
        /// library is stored alongside the network definition XML and will have already been read elsewhere and
        /// passed in here.
        /// </summary>
        /// <param name="xr">The XmlReader to read from.</param>
        /// <param name="activationFnLib">The activation function library used to decode node activation function IDs.</param>
        /// <param name="nodeFnIds">Indicates if node activation function IDs should be read. They are required
        /// for HyperNEAT genomes but not NEAT</param>
        public static NetworkDefinition ReadNetworkDefinition(XmlReader xr, IActivationFunctionLibrary activationFnLib, bool nodeFnIds)
        {
            // Find <Network>.
            XmlIoUtils.MoveToElement(xr, false, __ElemNetwork);
            int initialDepth = xr.Depth;

            // Find <Nodes>.
            XmlIoUtils.MoveToElement(xr, true, __ElemNodes);
            
            // Create a reader over the <Nodes> sub-tree.
            int inputNodeCount = 0;
            int outputNodeCount = 0;
            NodeList nodeList = new NodeList();
            using(XmlReader xrSubtree = xr.ReadSubtree())
            {
                // Re-scan for the root <Nodes> element.
                XmlIoUtils.MoveToElement(xrSubtree, false);

                // Move to first node elem.
                XmlIoUtils.MoveToElement(xrSubtree, true, __ElemNode);

                // Read node elements.
                do
                {
                    NodeType nodeType = ReadAttributeAsNodeType(xrSubtree, __AttrType);
                    uint id = XmlIoUtils.ReadAttributeAsUInt(xrSubtree, __AttrId);
                    int fnId = 0;
                    double[] auxState = null;
                    if(nodeFnIds) 
                    {   // Read activation fn ID.
                        fnId = XmlIoUtils.ReadAttributeAsInt(xrSubtree, __AttrActivationFunctionId);

                        // Read aux state as comma separated list of real values.
                        auxState = XmlIoUtils.ReadAttributeAsDoubleArray(xrSubtree, __AttrAuxState);
                    }

                    // TODO: Read node aux state data.
                    NetworkNode node = new NetworkNode(id, nodeType, fnId, auxState);
                    nodeList.Add(node);

                    // Track the number of input and output nodes.
                    switch(nodeType)
                    {
                        case NodeType.Input:
                            inputNodeCount++;
                            break;
                        case NodeType.Output:
                            outputNodeCount++;
                            break;
                    }
                } 
                while(xrSubtree.ReadToNextSibling(__ElemNode));
            }

            // Find <Connections>.
            XmlIoUtils.MoveToElement(xr, false, __ElemConnections);

            // Create a reader over the <Connections> sub-tree.
            ConnectionList connList = new ConnectionList();
            using(XmlReader xrSubtree = xr.ReadSubtree())
            {
                // Re-scan for the root <Connections> element.
                XmlIoUtils.MoveToElement(xrSubtree, false);

                // Move to first connection elem.
                string localName = XmlIoUtils.MoveToElement(xrSubtree, true);
                if(localName == __ElemConnection)
                {   // We have at least one connection.
                    // Read connection elements.
                    do
                    {
                        uint srcId = XmlIoUtils.ReadAttributeAsUInt(xrSubtree, __AttrSourceId);
                        uint tgtId = XmlIoUtils.ReadAttributeAsUInt(xrSubtree, __AttrTargetId);
                        double weight = XmlIoUtils.ReadAttributeAsDouble(xrSubtree, __AttrWeight);
                        NetworkConnection conn = new NetworkConnection(srcId, tgtId, weight);
                        connList.Add(conn);
                    } 
                    while(xrSubtree.ReadToNextSibling(__ElemConnection));
                }
            }

            // Move the reader beyond the closing tags </Connections> and </Network>.
            do
            {
                if (xr.Depth <= initialDepth) {
                    break;
                }
            }
            while(xr.Read());

            // Construct and return loaded network definition.
            return new NetworkDefinition(inputNodeCount, outputNodeCount, activationFnLib, nodeList, connList);
        }
Пример #34
0
 public static bool Connect(string hostName, string userName, string password, string database)
 {
     DatabaseName = database;
     ConnectionList.Connect(hostName, userName, password, database);
     return(ConnectionList.Connected);
 }