Package org.telehash.model

Examples of org.telehash.model.Line


  }
 
  @Override
  public void sessionCreated(IoSession session) throws Exception {
    super.sessionCreated(session);
    Line line = state.getOrCreateLine((InetSocketAddress)session.getRemoteAddress());
    session.setAttribute("line", line);
    logger.debug("Session[{}] created", session.getId());
  }
View Full Code Here


  public void seed(final InetSocketAddress seedAddress) {
    state.seeding(new Runnable(){
      @Override
      public void run() {
        state.setSeedAddress(seedAddress);
        Line seedLine = state.getOrCreateLine(seedAddress);
        send(tf.createTelex().withTo(seedLine)
            .withEnd(Hash.of(state.getSeedAddress())));
      }
    });
  }
View Full Code Here

    }
    scannerThread = null;
  }

  public void send(final Telex telex) {
      final Line line = state.getOrCreateLine(telex.getTo());
     
      // check br and drop if too much
      if (line.getBsent() - line.getBrIn() > 10000) {
          logger.warn("MAX SEND DROP");
          return;
      }
     
      // if a line is open use that, else send a ring
      if (line.isSetLineId()) {
          telex.setLine(line.getLineId());
      }
      else {
        telex.unsetLine();
          telex.setRing(line.getRingOut());
      }
     
      // update our bytes tracking and send current state
      line.setBrOut(line.getBr());
      telex.setBytesReceived(line.getBr());
     
      try {
          final String msg = JsonMapper.toJson(telex);
          final byte[] bytes = msg.getBytes();
          final IoBuffer buffer = allocator.allocate(bytes.length, true);
          buffer.put(bytes);
          buffer.flip();
         
          final IoFutureListener<IoFuture> onWriteComplete = new IoFutureListener<IoFuture>() {
            public void operationComplete(IoFuture future) {
                line.setBsent(line.getBsent() + bytes.length);
                line.setSentAt(time());
            };
      };
     
          if (line != null && line.getSession() != null && line.getSession().getAttribute("line") == line) {
              logger.debug("SEND[{}]: {} bytes: {}", new Object[]{
                  line.getSession().getId(), bytes.length, msg});
            line.getSession().write(buffer).addListener(onWriteComplete);
          }
          else {
            IoSession session = acceptor.newSession(telex.getTo(), acceptor.getLocalAddress());
                line.setSession(session);
                session.setAttribute("line", line);
                logger.debug("SEND[{}]: {} bytes: {}", new Object[]{
                    session.getId(), bytes.length, msg});
                session.write(buffer).addListener(onWriteComplete);
          }
View Full Code Here

      }
    }
  }

  protected void processTelex(IoSession session, final Telex telex, int br) {
    Line line = (Line) session.getAttribute("line");
    if (line == null) {
      session.close(true);
      return;
    }
   
    boolean lineStatus = checkLine(line, telex, br);
    if (lineStatus) {
      logger.debug("LINE [{}] STATUS {}", line.getAddress(),
          telex.isSetLine() ? "OPEN" : "RINGING");
    }
    else {
      logger.debug("LINE [{}] FAIL", line.getAddress());
      return;
    }
   
    for (TelexHandler handler : telexHandlers) {
      if (handler.isMatch(telex)) {
View Full Code Here

      @Override
      public void run() {
        state.setSelfAddress(telex.getTo());
        logger.debug("SELF[{} = {}]", state.getSelfAddress(), state.getSelfHash());
       
        Line line = state.getOrCreateLine(state.getSelfAddress());
        if (line == null) {
          return;
        }
       
        line.setVisible(true);
        line.getRules().addAll(tapRules);
       
        if (state.getSelfAddress().equals(session.getRemoteAddress())) {
          logger.debug("We're the seed.");
        }
       
View Full Code Here

        Hash hash = entry.getKey();
          if (hash.equals(state.getSelfHash())) {
              continue; // skip our own endpoint and what is this (continue)
          }
         
          Line line = entry.getValue();
          if (!line.getEnd().equals(hash)) {
            continue; // empty/dead line (continue)
          }
         
          if ((line.getSeenAt() == 0 && now - line.getInit() > 70)
                  || (line.getSeenAt() != 0 && now - line.getSeenAt() > 70)) {
              // remove line if they never responded or haven't in a while
              logger.debug("PURGE[" + hash + " " + line.getAddress() + "] last seen "
                  + Long.toString(now - line.getSeenAt()) + "s ago");
              entryIter.remove();
              continue;
          }
         
          numValid++;
         
          if (state.getConnectionStatus() == ConnectionStatus.CONNECTED) {
         
              // +end ourselves to see if they know anyone closer as a ping
            Telex telexOut = tf.createTelex().withTo(line).withEnd(state.getSelfHash());
             
              // also .see ourselves if we haven't yet, default for now is to participate in the DHT
              if (!line.isAdvertised()) {
                line.setAdvertised(true);
                telexOut.getSee().add(state.getSelfAddress());
              }
             
              // also .tap our hash for +pop requests for NATs
              TapRule tapRule = tf.createTapRule();
View Full Code Here

      }
  }
 
  public Collection<Hash> nearTo(final Hash endHash, InetSocketAddress address) {
    Hash addrHash = Hash.of(address);
      Line addrLine = getLine(addrHash);
      if (addrLine == null) {
          return Collections.emptyList(); // should always exist except in startup or offline, etc
      }
     
      // of the existing and visible cached neighbors, sort by distance to this end
      List<Hash> visibleNeighbors = Lists.newArrayList(
        Iterables.filter(addrLine.getNeighbors(),
          new Predicate<Hash>() {
            @Override
            public boolean apply(Hash hash) {
              Line line = getLine(hash);
              return line != null && line.isVisible();
            }
        }));
      Collections.sort(visibleNeighbors, new Comparator<Hash>(){
        @Override
        public int compare(Hash o1, Hash o2) {
          return endHash.diffBit(o1) - endHash.diffBit(o2);
        }
      });
     
//      console.log("near_to: see[]=" + JSON.stringify(see));
//      console.log("near_to: line=" + JSON.stringify(line));
     
      if (visibleNeighbors.isEmpty()) {
          return Collections.emptyList();
      }
     
      Hash firstSeeHash = visibleNeighbors.get(0);
     
      logger.debug(StringUtils.join(
          new String[]{
            "NEARTO " + endHash,
            address.toString(),
            endHash.toString(),
            Integer.toString(addrLine.getNeighbors().size()),
            ">",
            Integer.toString(visibleNeighbors.size()),
            ";",
            firstSeeHash.toString(),
            "=",
            Integer.toString(addrLine.getEnd().diffBit(endHash))
          }, " "));
     
      // it's either us or we're the same distance away so return these results
      if (firstSeeHash.equals(addrLine.getEnd())
              || (firstSeeHash.diffBit(endHash) == addrLine.getEnd().diffBit(endHash))) {
         
          // this +end == this line then replace the neighbors cache with this result
          // and each in the result walk and insert self into their neighbors
          if (addrLine.getEnd().equals(endHash)) {
            logger.debug("NEIGH for " + endHash + " was "
                + StringUtils.join(addrLine.getNeighbors(), ",")
                + " " + visibleNeighbors.size());
           
            addrLine.getNeighbors().clear();
            Iterables.addAll(addrLine.getNeighbors(),
                Iterables.limit(visibleNeighbors, 5));
             
            logger.debug("NEIGH for " + endHash + " is now "
                + StringUtils.join(addrLine.getNeighbors(), ",")
                + " " + visibleNeighbors.size());
           
            for (Hash neighborHash : addrLine.getNeighbors()) {
              Line neighborLine = getLine(neighborHash);
              if (neighborLine == null || neighborLine == addrLine
                  || neighborLine.getNeighbors().contains(endHash)) {
                continue;
              }
             
              neighborLine.getNeighbors().add(endHash);
                    logger.debug("SEED " + address + " into " + neighborLine.getAddress());
            }
           
          }
         
          logger.debug("SEE distance=" + endHash.diffBit(firstSeeHash)
View Full Code Here

            continue;
          }
         
          Collection<Hash> nearestHashes = nearTo(tapEnd, state.getSelfAddress());
          for (Hash hash : Iterables.limit(nearestHashes, 3)) {
              Line line = getLine(hash);
             
              if (line.isSetTapLastAt() && line.getTapLastAt() + 50 > time()) {
                  return; // only tap every 50sec
              }
             
              line.setTapLastAt(time());
              Telex telexOut = (Telex) tf.createTelex().withTo(line)
                .with(".tap", Lists.newArrayList(tapRule)); // tap the closest ipp to our target end
              logger.info("TAPTAP to {} end {} tap {}", new Object[]{
                  line.getAddress(), tapEnd, JsonMapper.toJson(telexOut)});
              send(telexOut);
          }
      }
  }
View Full Code Here

    if (endpoint == null) {
      return null;
    }
   
    Hash endHash = Hash.of(endpoint);
    Line line = lines.get(endHash);
    if (line == null || !line.getAddress().equals(endpoint)) {
      line = tf.createLine().withAddress(endpoint).withEnd(endHash);
      line.getNeighbors().add(endHash);
      lines.put(endHash, line);
    }
    return line;
  }
View Full Code Here

      if (result == null)
        result = defaultCase(theEObject);
      return result;
    }
    case TelehashPackage.LINE: {
      Line line = (Line) theEObject;
      T result = caseLine(line);
      if (result == null)
        result = defaultCase(theEObject);
      return result;
    }
View Full Code Here

TOP

Related Classes of org.telehash.model.Line

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.