Brunet.AddressParser.Parse C# (CSharp) Method

Parse() static public method

static public Parse ( MemBlock mb ) : Brunet.Address
mb MemBlock
return Brunet.Address
    static public Address Parse(MemBlock mb)
    {
#if BRUNET_SIMULATOR
      Address a = null;
#else
      //Read some of the least significant bytes out,
      //AHAddress all have last bit 0, so we skip the last byte which
      //will have less entropy
      ushort idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3);
      Address a = _mb_cache[idx];
      if( a != null ) {
        if( a.ToMemBlock().Equals(mb) ) {
          return a;
        }
      }
#endif
      //Else we need to read the address and put it in the cache
      try {
        if( 2 * mb.Length < mb.ReferencedBufferLength ) {
            /*
             * This MemBlock is much smaller than the array
             * we are referencing, don't keep the big one
             * in scope, instead make a copy
             */
          mb = MemBlock.Copy((ICopyable)mb);
        }
        int add_class = Address.ClassOf(mb);
        switch (add_class) {
        case AHAddress.ClassValue:
          a = new AHAddress(mb);
          break;
        case DirectionalAddress.ClassValue:
          a = new DirectionalAddress(mb);
          break;
        default:
          a = null;
          throw new ParseException("Unknown Address Class: " +
                                   add_class + ", buffer:" +
                                   mb.ToString());
        }
#if BRUNET_SIMULATOR
        return SimulatorCache(a);
#else
        //Cache this result:
        _mb_cache[ idx ] = a;
        return a;
#endif
      }
      catch(ArgumentOutOfRangeException ex) {
        throw new ParseException("Address too short: " +
                                 mb.ToString(), ex);
      }
      catch(ArgumentException ex) {
        throw new ParseException("Could not parse: " +
                                 mb.ToString(), ex);
      }
    }
  }

Same methods

AddressParser::Parse ( string ascii ) : Brunet.Address

Usage Example

Example #1
0
        /**
         * Create a node with a given local address and
         * a set of Routers.
         * @param addr Address for the local node
         * @param realm the Realm or Namespace this node belongs to
         */

        protected Node(Address addr, string realm)
        {
            //Start with the address hashcode:

            _sync = new Object();
            lock (_sync)
            {
                DemuxHandler = new DemuxHandler();

                /*
                 * Make all the hashtables :
                 */
                _local_add = AddressParser.Parse(addr.ToMemBlock());
                _realm     = String.Intern(realm);

                /* Set up the heartbeat */
                _heart_period       = 500; //500 ms, or 1/2 second.
                _heartbeat_handlers = new Dictionary <EventHandler, Brunet.Util.FuzzyEvent>();

                _task_queue   = new NodeTaskQueue(this);
                _packet_queue = new BCon.LFBlockingQueue <IAction>();

                _running    = 0;
                _send_pings = 1;
                _LOG        = ProtocolLog.Monitor.Enabled;

                _connection_table = new ConnectionTable(_local_add);
                _connection_table.ConnectionEvent += this.ConnectionHandler;

                //We start off offline.
                _con_state = Node.ConnectionState.Offline;

                /* Set up the ReqrepManager as a filter */
                _rrm = new ReqrepManager(this);
                DemuxHandler.GetTypeSource(PType.Protocol.ReqRep).Subscribe(_rrm, null);
                _rrm.Subscribe(this, null);
                this.HeartBeatEvent += _rrm.TimeoutChecker;
                /* Set up RPC */
                _rpc = new RpcManager(_rrm);
                DemuxHandler.GetTypeSource(PType.Protocol.Rpc).Subscribe(_rpc, null);

                /*
                 * Where there is a change in the Connections, we might have a state
                 * change
                 */
                _connection_table.ConnectionEvent    += this.CheckForStateChange;
                _connection_table.DisconnectionEvent += this.CheckForStateChange;
                _connection_table.StatusChangedEvent += this.CheckForStateChange;

                _codeinjection = new Brunet.Services.CodeInjection(this);
                _codeinjection.LoadLocalModules();

                /*
                 * We must later make sure the EdgeEvent events from
                 * any EdgeListeners are connected to _cph.EdgeHandler
                 */
                /**
                 * Here are the protocols that every edge must support
                 */
                /* Here are the transport addresses */
                _remote_ta = new ArrayList();

                /*@throw ArgumentNullException if the list ( new ArrayList()) is null.
                 */
                /* EdgeListener's */
                _edgelistener_list = new ArrayList();
                _edge_factory      = new EdgeFactory();

                /* Initialize this at 15 seconds */
                _connection_timeout = new TimeSpan(0, 0, 0, 0, 15000);
                //Check the edges from time to time
                IAction cec_act = new HeartBeatAction(this, this.CheckEdgesCallback);
                _check_edges = Brunet.Util.FuzzyTimer.Instance.DoEvery(delegate(DateTime dt) {
                    this.EnqueueAction(cec_act);
                }, 15000, 1000);
            }
        }