All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.csource.fastdht.ServerGroup Maven / Gradle / Ivy

The newest version!
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDHT Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDHT Home Page http://fastdht.csource.org/ for more detail.
**/

package com.csource.fastdht;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import com.csource.common.IniFileReader;
import com.csource.common.MyException;

/**
* Global variables
* @author Happy Fish / YuQing
* @version Version 1.02
*/
@SuppressWarnings({"unchecked","rawtypes"})
public class ServerGroup
{
	public static class ServerComparator implements java.util.Comparator
	{
		public int compare(Object o1, Object o2)
		{
			ServerInfo s1 = (ServerInfo)o1;
			ServerInfo s2 = (ServerInfo)o2;
			
			int compResult = s1.address.getAddress().getHostAddress().compareTo(s2.address.getAddress().getHostAddress());
			if (compResult != 0)
			{
				return compResult;
			}
			
			return s1.address.getPort() - s2.address.getPort();
		}
		
		public boolean equals(Object obj)
		{
			return this.compare(this, obj) == 0;
		}
	}
	
	protected ServerInfo[] servers;  //distinct servers
	protected ServerInfo[][] groups; //group info array
	protected boolean keep_alive;    //if keep persistent connection

	protected ServerGroup()
	{
		
	}
	
	public ServerGroup(ServerInfo[] servers, ServerInfo[][] groups, boolean keep_alive)
	{
		this.servers = servers;
		this.groups = groups;
		this.keep_alive = keep_alive;
	}
	
	public void setKeepAlive(boolean keep_alive)
	{
		this.keep_alive = keep_alive;
	}
	
	public boolean getKeepAlive()
	{
		return this.keep_alive;
	}
	
	public int getGroupCount()
	{
		return this.groups.length;
	}
	
	public ServerInfo[][] getGroups()
	{
		return this.groups;
	}
	
	public int getServerCount()
	{
		return this.servers.length;
	}
	
	public ServerInfo[] getServers()
	{
		return this.servers;
	}
	
	public Object clone()
	{
		int i;
		int k;
		int index;
		ServerGroup serverGroup;
		ServerInfo[] group_servers;
		ServerComparator compObj = new ServerComparator();
		
		serverGroup = new ServerGroup();
		
		serverGroup.servers = new ServerInfo[this.servers.length];
		for (i=0; i 0)
	  				{
	  					System.arraycopy(serverGroup.servers, 0, servers, 0, serverGroup.servers.length);
	  				}
	  				servers[serverGroup.servers.length] = serverInfo;
	  				java.util.Arrays.sort(servers, compObj);
	  				serverGroup.servers = servers;
	  				server_index = java.util.Arrays.binarySearch(serverGroup.servers, serverInfo, compObj);
	  			}
	  			
	  			if (java.util.Arrays.binarySearch(serverGroup.groups[i], serverInfo, compObj) < 0)
	  			{
						servers = new ServerInfo[serverGroup.groups[i].length + 1];
	  				if (serverGroup.groups[i].length > 0)
	  				{
	  					System.arraycopy(serverGroup.groups[i], 0, servers, 0, serverGroup.groups[i].length);
	  				}
	  				servers[serverGroup.groups[i].length] = serverGroup.servers[server_index];
	  				java.util.Arrays.sort(servers, compObj);
	  				serverGroup.groups[i] = servers;
  				}
	  		}
  		}
  		
  		return serverGroup;
	}
	
/**
* connect to server
* @param server the server
* @return true for success, false for fail
*/
	@SuppressWarnings("resource")
	public boolean connectServer(ServerInfo server)
	{
			Socket sock;
    	if (server.sock != null)
    	{
    		return true;
    	}
    	
			try
			{
				sock = new Socket();
				sock.setReuseAddress(true);
				sock.setSoTimeout(ClientGlobal.g_network_timeout);
				sock.connect(server.address, ClientGlobal.g_network_timeout);
				server.sock = sock;
				return true;
			}
			catch(IOException ex)
			{
				System.err.println("connect to server " + server.address.getAddress().getHostAddress()
							 + ":" + server.address.getPort() + " fail, error info: " + ex.getMessage());
				return false;
			}
	}
	
/**
* get connected server
* @param key_hash_code the key hash code
* @return != null for success, null for fail
*/
	public ServerInfo getServer(int key_hash_code)
	{
		int group_id;
    int server_index;
    int server_count;
    int new_hash_code;
		int i;
		
		group_id = key_hash_code % this.getGroupCount();
		server_count = this.groups[group_id].length;
		
    new_hash_code = (key_hash_code << 16) | (key_hash_code >> 16);
    if (new_hash_code < 0)
    {
            new_hash_code &= 0x7FFFFFFF;
    }
    server_index = new_hash_code % server_count;
    
    for (i=server_index; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy