Esempio n. 1
0
  @Override
  public boolean equivale(IP obj) {

    if (obj.getClass() == IP.class) {
      IP alvo = (IP) obj;
      for (IP i : ip) {
        if (i.equivale(alvo)) return true;
      }
      return false;
    }

    IPConjunto o = (IPConjunto) obj;
    boolean sucesso;

    for (int i = 0; i < ip.length; i++) {
      sucesso = false;
      for (int j = 0; j < o.ip.length; j++) {
        if (ip[i].equivale(o.ip[j])) {
          sucesso = true;
          break;
        }
      }
      if (!sucesso) return false;
    }

    return true;
  }
Esempio n. 2
0
 /**
  * Creates a <code>IpRange</code> instance by a string.
  *
  * @param range a string such as "1.1.1.1-1.1.2.255".
  * @return IP range.
  */
 public static IPRange parseFromString(String range) {
   String[] ips = range.split("-");
   Preconditions.checkArgument(
       ips.length == 2,
       "IP range string must be fomarted as [minIP-maxIP],error argument:" + range);
   return new IPRange(IP.parseFromString(ips[0]), IP.parseFromString(ips[1]));
 }
Esempio n. 3
0
 public String toString() {
   StringBuilder s = new StringBuilder(ip.length * 15 + ip.length);
   boolean primeiro = true;
   for (IP i : ip) {
     if (primeiro) {
       s.append(i.toString());
       primeiro = false;
     } else {
       s.append("+" + i.toString());
     }
   }
   return s.toString();
 }
Esempio n. 4
0
  /**
   * Creates a {@link IPRange} instance by IP with mask.
   *
   * @param ipWithMask IP/mask, such as 192.168.70.1/24
   * @return {@link IPRange} instance
   */
  public static IPRange parseFromIPWithMask(String ipWithMask) {
    long minIpAsLong = 0;
    long maxIpAsLong = 0;
    String[] strs = ipWithMask.split("/");

    if (strs.length == 2) {
      IP ip = IP.parseFromString(strs[0]);
      int mask = Integer.parseInt(strs[1]);
      long maskAsLong = 0xffffffff << (32 - mask);
      minIpAsLong = ip.toLong();
      maxIpAsLong = minIpAsLong | (~maskAsLong);
    } else {
      throw new IllegalArgumentException(
          "The input String format error. for example" + " 192.168.1.1/24");
    }
    return new IPRange(new IP(minIpAsLong), new IP(maxIpAsLong));
  }
Esempio n. 5
0
  /**
   * Constructs a <code>IpRange</code> instance by given tow IP.
   *
   * @param startIp IP starts.
   * @param endIp IP ends.
   */
  public IPRange(IP startIp, IP endIp) {

    int result = endIp.compareTo(startIp);
    if (result > 0 || result == 0)
      Preconditions.checkArgument(
          result > 0 || result == 0, "maxIP must equal or bigger than minIP");

    this.startIP = startIp;
    this.endIP = endIp;
  }
Esempio n. 6
0
  public void resendMessage(Message message, IP remoteAddress, int remotePort) throws IOException {
    if (remoteAddress == null) remoteAddress = IP.getLocalHost();

    message.setSentStamp(getConvertedTime(remoteAddress, remotePort));
    message.setMessageServer(this);
    try {
      sendBuffer.clear();
      sendBuffer.put(extractor.convertMessage(message));
      sendBuffer.flip();
      channel.send(sendBuffer, new InetSocketAddress(remoteAddress.toString(), remotePort));
      message.setRemoteAddress(remoteAddress);
      message.setRemotePort(remotePort);
      messageSent(message);
    } catch (IllegalAccessException exc) {
      throw new RuntimeException(exc);
    } catch (IllegalArgumentException exc) {
      throw new RuntimeException(exc);
    } catch (InvocationTargetException exc) {
      throw new RuntimeException(exc);
    }
  }
Esempio n. 7
0
 protected synchronized Message receiveMessage() throws IOException {
   if (messageBuffer.size() > 0) {
     Message m = (Message) messageBuffer.get(0);
     messageBuffer.remove(0);
     return m;
   }
   try {
     InetSocketAddress remoteAddress = (InetSocketAddress) channel.receive(receiveBuffer);
     if (remoteAddress != null) {
       int len = receiveBuffer.position();
       receiveBuffer.rewind();
       receiveBuffer.get(buf, 0, len);
       try {
         IP address = IP.fromInetAddress(remoteAddress.getAddress());
         int port = remoteAddress.getPort();
         extractor.appendData(buf, 0, len, new SocketDescriptor(address, port));
         receiveBuffer.clear();
         extractor.updateAvailableMessages();
         return extractor.nextMessage();
       } catch (EOFException exc) {
         exc.printStackTrace();
         System.err.println(buf.length + ", " + len);
       } catch (InvocationTargetException exc) {
         exc.printStackTrace();
       } catch (IllegalAccessException exc) {
         exc.printStackTrace();
       } catch (InstantiationException exc) {
         exc.printStackTrace();
       } catch (IllegalArgumentException e) {
         e.printStackTrace();
       } catch (InvalidCompressionMethodException e) {
         e.printStackTrace();
       }
     }
   } catch (ClosedChannelException exc) {
     if (isKeepAlive()) {
       throw exc;
     }
   }
   return null;
 }
Esempio n. 8
0
 public UDPMessageServer(InetSocketAddress address) throws IOException {
   this(IP.fromInetAddress(address.getAddress()), address.getPort());
 }
Esempio n. 9
0
 public int hashCode() {
   if (hash == 0) {
     for (IP i : ip) hash += i.hashCode();
   }
   return hash;
 }
Esempio n. 10
0
 /**
  * Returns size of IP range.
  *
  * @return Size of IP range.
  */
 public long size() {
   return (endIP.getValue() - startIP.getValue() + 1L);
 }
Esempio n. 11
0
 /**
  * Returns <code>true</code> if the given IP is in the IP range.
  *
  * @param ip IP.
  * @return If the IP is in the rang return <code>true</code>.
  */
 public boolean contains(IP ip) {
   if (ip.compareTo(startIP) >= 0 && ip.compareTo(endIP) <= 0) {
     return true;
   }
   return false;
 }