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

📄 distributor.java

📁 简单的分布式算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				if (configNode.getNodeName().equals("target_group"))				{					// targets is a LinkedList in order to make the					// re-ordering of the list by the round robin					// algorithm speedy					List targets = new LinkedList();					Element tgElement = (Element) configNode;					NodeList tgChildren = tgElement.getChildNodes();					for (int j=0 ; j<tgChildren.getLength() ; j++)					{						Node tgNode = tgChildren.item(j);						if (tgNode.getNodeName().equals("target"))						{							Element targetElement = (Element) tgNode;							targets.add(								new Target(									this,									InetAddress.getByName(										targetElement.getAttribute("hostname")),									Integer.parseInt(										targetElement.getAttribute("port")),									connectionFailureLimit,									terminateOnDisable));						}					}					if (targets.size() > 0)					{						targetGroups.add(targets);					}				}			}			// Log the target group configuration			if (targetGroups.size() > 0)			{				logger.config("Target groups:");				Iterator tgIter = targetGroups.iterator();				int tgCounter = 0;				while (tgIter.hasNext())				{					logger.config("Group " + tgCounter + ":");					tgCounter++;					List targets = (List) tgIter.next();					Iterator targetIter = targets.iterator();					while (targetIter.hasNext())					{						logger.config("  " + targetIter.next());					}				}			}			else			{				logger.severe("At least one target group must be specified");				System.exit(1);			}			//			// Read the service test configuration and create the			// service test object			//			serviceTest = null;			// First get the service type			String serviceType = rootElement.getAttribute("service_type");			logger.config("Service type:  " + serviceType);			if (serviceType.equals(""))			{				logger.warning("No service test specified, none will be used");			}			else if (serviceType.equals("none"))			{				logger.config("Configured for no service test");			}			else			{				// Then get the test parameters and class name that go with				// that service type				Element testParameters = null;				String testClassName = null;				for (int i=0 ; i<configChildren.getLength() ; i++)				{					Node node = configChildren.item(i);					if (node.getNodeName().equals("test_parameters"))					{						Element elem = (Element) node;						if (elem.getAttribute("service_type").							equals(serviceType))						{							testParameters = elem;						}					}					else if (node.getNodeName().equals("type_mapping"))					{						Element elem = (Element) node;						if (elem.getAttribute("service_type").							equals(serviceType))						{							testClassName = elem.getAttribute("class");							logger.config("Service test class:  " +								testClassName);						}					}				}				if (testParameters == null)				{					logger.severe("Test parameters for service test not found");					System.exit(1);				}				if (testClassName == null)				{					logger.severe("Service test class name not found");					System.exit(1);				}				// Now construct the service test object				logger.finer("Constructing " + testClassName);				serviceTest = constructObjectFromName(					testClassName, testParameters);			}		}		catch (ParserConfigurationException e)		{			System.err.println("Error reading config file: " + e.getMessage());			System.exit(1);		}		catch (SAXException e)		{			System.err.println("Error reading config file: " + e.getMessage());			System.exit(1);		}		catch (IOException e)		{			System.err.println("Error reading config file: " + e.getMessage());			System.exit(1);		}		catch (NumberFormatException e)		{			System.err.println("Error reading config file: " + e.getMessage());			System.exit(1);		}		if (controlPort != 0)		{			controller = new Controller(this, controlPort);		}		targetSelector = new TargetSelector(this);		// Start all of the threads which require delayed initialization		targetSelector.startThread();		Iterator iter = distributionAlgorithms.iterator();		while(iter.hasNext())		{			DistributionAlgorithm algo = (DistributionAlgorithm) iter.next();			algo.startThread();		}	}	private Object constructObjectFromName(		String className, Element configElement)	{		Object obj;		try		{			Class objClass = Class.forName(className);			Class[] constructorArgumentClasses = {				this.getClass(),				Class.forName("org.w3c.dom.Element") };			Constructor classConstructor =				objClass.getConstructor(constructorArgumentClasses);			Object[] constructorArguments = {				this,				configElement };			obj = classConstructor.newInstance(constructorArguments);			return obj;		}		catch (ClassNotFoundException e)		{			logger.severe("Class not found:  " + e.getMessage());			System.exit(1);		}		catch (NoSuchMethodException e)		{			logger.severe(				"Constructor in class not found:  " + e.getMessage());			System.exit(1);		}		catch (InstantiationException e)		{			logger.severe("Class is abstract:  " + e.getMessage());			System.exit(1);		}		catch (IllegalAccessException e)		{			logger.severe(				"Access to class constructor prohibited:  " +				e.getMessage());			System.exit(1);		}		catch (InvocationTargetException e)		{			// Print the message from the exception thrown by the			// constructor, not the message in the			// InvocationTargetException (which is generally null in my			// experience).			logger.severe(				"Class constructor threw exception:  " +				e.getCause().getMessage());			System.exit(1);		}		return null;	}	public Logger getLogger()	{		return logger;	}	public List getDistributionAlgorithms()	{		return distributionAlgorithms;	}	public List getTargetGroups()	{		return targetGroups;	}	public TargetSelector getTargetSelector()	{		return targetSelector;	}	public int getConnectionTimeout()	{		return connectionTimeout;	}	public int getConnectionFailureLimit()	{		return connectionFailureLimit;	}	public boolean getTerminate()	{		return terminateOnDisable;	}	/*	 * Returns a list of all of the Targets.  Useful for those who don't	 * care about the target groups.	 */	public List getTargets()	{		List all = new ArrayList();		Iterator i = targetGroups.iterator();		List tg;		while (i.hasNext())		{			tg = (List) i.next();			all.addAll(tg);		}		return all;	}	private void balance()	{		// Open the listening socket and wait for connections		try		{			ServerSocketChannel server = ServerSocketChannel.open();			server.socket().bind(new InetSocketAddress(bindAddress, port));			while (true)			{				SocketChannel client = server.accept();				logger.fine("Accepted connection from " + client);				// Hand the client off to another thread which will				// select a target for them.  This frees us up to go				// back to listening for new connections.				targetSelector.addNewClient(client);			}		}		catch (IOException e)		{			logger.severe("Error with server socket: " + e.getMessage());			System.exit(1);		}	}	/*	 * Parse log level names into Level constants.	 * i.e. take "warning" and return Level.WARNING.	 */	public static Level parseLogLevel(String levelName)		throws ParseException	{		if (levelName.equals("off"))		{			return Level.OFF;		}		else if (levelName.equals("severe"))		{			return Level.SEVERE;		}		else if (levelName.equals("warning"))		{			return Level.WARNING;		}		else if (levelName.equals("info"))		{			return Level.INFO;		}		else if (levelName.equals("config"))		{			return Level.CONFIG;		}		else if (levelName.equals("fine"))		{			return Level.FINE;		}		else if (levelName.equals("finer"))		{			return Level.FINER;		}		else if (levelName.equals("finest"))		{			return Level.FINEST;		}		else if (levelName.equals("all"))		{			return Level.ALL;		}		else		{			throw new ParseException("Unrecognized log level", 0);		}	}}

⌨️ 快捷键说明

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