⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iprangeimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
字号:
/*
 * File    : IpRange.java
 * Created : 8 oct. 2003 13:02:23
 * By      : Olivier 
 * 
 * Azureus - a Java Bittorrent client
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details ( see the LICENSE file ).
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.gudy.azureus2.core3.ipfilter.impl;

import java.net.UnknownHostException;

import org.gudy.azureus2.core3.ipfilter.IpFilterManagerFactory;
import org.gudy.azureus2.core3.ipfilter.IpRange;
import org.gudy.azureus2.core3.tracker.protocol.PRHelpers;

/**
 * @author Olivier
 * 
 */

public class IpRangeImpl implements IpRange
{
	private final static byte FLAG_SESSION_ONLY = 0x1;

	private final static byte FLAG_ADDED_TO_RANGE_LIST = 0x2;

	private Object startIp; // Integer if value, String/null otherwise

	private Object endIp; // Integer if value, String/null otherwise

	private byte flags;

	private Object descRef = null;

	// Merging stuff 
	private final static byte FLAG_MERGED = 0x4;

	private int merged_end;

	private IpRange[] my_merged_entries;

	public IpRangeImpl(String _description, String _startIp, String _endIp,
			boolean _sessionOnly) {
		if (_sessionOnly) {
			flags = FLAG_SESSION_ONLY;
		}

		if (_startIp == null || _endIp == null) {

			throw (new RuntimeException(
					"Invalid start/end values - null not supported"));
		}

		if (_startIp == "") {
			startIp = "";
		} else {
			startIp = _startIp.trim();
		}

		if (_endIp == "") {
			endIp = "";
		} else {
			endIp = _endIp.trim();
		}

		if (_description != "") {
			setDescription(_description);
		}

		checkValid();
	}

	public void checkValid() {
		if (startIp != null && endIp != null) {

			if (startIp instanceof String) {

				try {
					startIp = new Integer(PRHelpers.addressToInt((String) startIp));

				} catch (UnknownHostException e) {

				}
			}

			if (endIp instanceof String) {

				try {
					endIp = new Integer(PRHelpers.addressToInt((String) endIp));

				} catch (UnknownHostException e) {

				}
			}
		}

		((IpFilterImpl) IpFilterImpl.getInstance()).setValidOrNot(this, isValid());

	}

	public boolean isValid() {
		if (startIp instanceof Integer && endIp instanceof Integer) {

			long start_address = ((Integer) startIp).intValue();
			long end_address = ((Integer) endIp).intValue();

			if (start_address < 0) {

				start_address += 0x100000000L;
			}
			if (end_address < 0) {

				end_address += 0x100000000L;
			}

			return (end_address >= start_address);
		}

		return (false);
	}

	public boolean isInRange(String ipAddress) {
		if (!isValid()) {
			return false;
		}

		try {
			long int_address = PRHelpers.addressToInt(ipAddress);

			if (int_address < 0) {

				int_address += 0x100000000L;
			}

			long start_address = ((Integer) startIp).intValue();
			long end_address = ((Integer) endIp).intValue();

			if (start_address < 0) {

				start_address += 0x100000000L;
			}
			if (end_address < 0) {

				end_address += 0x100000000L;
			}

			return (int_address >= start_address && int_address <= end_address);

		} catch (UnknownHostException e) {

			return (false);
		}
	}

	public String getDescription() {
		return new String(IpFilterManagerFactory.getSingleton().getDescription(
				descRef));
	}

	public void setDescription(String str) {
		descRef = IpFilterManagerFactory.getSingleton().addDecription(this,
				str.getBytes());
	}

	public String getStartIp() {
		return (startIp instanceof Integer
				? PRHelpers.intToAddress(((Integer) startIp).intValue())
				: (String) startIp);
	}

	public long getStartIpLong() {
		if (startIp instanceof Integer) {

			long val = ((Integer) startIp).intValue();

			if (val < 0) {

				val += 0x100000000L;
			}

			return (val);
		}

		return (-1);
	}

	public void setStartIp(String str) {
		if (str == null) {
			throw (new RuntimeException("Invalid start value - null not supported"));
		}

		if (str.equals(getStartIp())) {
			return;
		}

		startIp = str;

		if (endIp != "") {
			checkValid();
		}
	}

	public String getEndIp() {
		return (endIp instanceof Integer)
				? PRHelpers.intToAddress(((Integer) endIp).intValue()) : (String) endIp;
	}

	public long getEndIpLong() {
		if (endIp instanceof Integer) {

			long val = ((Integer) endIp).intValue();

			if (val < 0) {
				val += 0x100000000L;
			}

			return (val);
		}

		return (-1);
	}

	public void setEndIp(String str)

	{
		if (str == null) {
			throw (new RuntimeException("Invalid end value - null not supported"));
		}

		if (str.equals(getEndIp())) {
			return;
		}

		endIp = str;

		if (startIp != "") {
			checkValid();
		}
	}

	public String toString() {
		return getDescription() + " : " + getStartIp() + " - " + getEndIp();
	}

	public boolean isSessionOnly() {
		return (flags & FLAG_SESSION_ONLY) != 0;
	}

	public void setSessionOnly(boolean _sessionOnly) {
		if (_sessionOnly) {
			flags |= FLAG_SESSION_ONLY;
		} else {
			flags &= ~FLAG_SESSION_ONLY;
		}
	}

	public int compareStartIpTo(IpRange other) {
		long l = getStartIpLong() - ((IpRangeImpl) other).getStartIpLong();

		if (l < 0) {
			return (-1);
		} else if (l > 0) {
			return (1);
		} else {
			return (0);
		}
	}

	public int compareEndIpTo(IpRange other) {
		long l = getEndIpLong() - ((IpRangeImpl) other).getEndIpLong();

		if (l < 0) {
			return (-1);
		} else if (l > 0) {
			return (1);
		}
		return (0);
	}

	protected void setAddedToRangeList(boolean b) {
		if (b) {
			flags |= FLAG_ADDED_TO_RANGE_LIST;
		} else {
			flags &= ~FLAG_ADDED_TO_RANGE_LIST;
		}
	}

	protected boolean getAddedToRangeList() {
		return (flags & FLAG_ADDED_TO_RANGE_LIST) != 0;
	}

	public int compareDescription(IpRange other) {
		return getDescription().compareTo(other.getDescription());
	}

	protected Object getDescRef() {
		return descRef;
	}

	protected void setDescRef(Object descRef) {
		this.descRef = descRef;
	}

	public long getMergedEndLong() {
		return (merged_end < 0 ? (merged_end + 0x100000000L) : merged_end);
	}

	public IpRange[] getMergedEntries() {
		return (my_merged_entries);
	}

	public void resetMergeInfo() {
		flags &= ~FLAG_MERGED;

		if (endIp instanceof Integer) {
			merged_end = ((Integer) endIp).intValue();
		}
	}

	public boolean getMerged() {
		return (flags & FLAG_MERGED) != 0;
	}

	public void setMerged() {
		flags |= FLAG_MERGED;
	}

	public void setMergedEnd(long endIpLong) {
		merged_end = (int) (endIpLong >= 0x100000000L ? endIpLong - 0x100000000L
				: endIpLong);
	}

	public void addMergedEntry(IpRange e2) {
		if (my_merged_entries == null) {

			my_merged_entries = new IpRange[] { e2 };

		} else {

			IpRange[] x = new IpRange[my_merged_entries.length + 1];

			System.arraycopy(my_merged_entries, 0, x, 0, my_merged_entries.length);

			x[x.length - 1] = e2;

			my_merged_entries = x;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -