关于Peersim的配置管理器的解析

peersim的配置管理器的代码都放在了peersim下的config包里面,如上图所示!其中比较重要的有四个类:ConfigContainer、ConfigProperties、Configuration、ParsedProperties。这四个类可以分为两组,第一组就是ParseProperties、ConfigProperties这两个类,主要是用来解析配置文件(.cfg文件);第二组就是ConfigContainer、Configuration这两个类,主要是用来解析Properties里面的参数。

而在这个包里面其他比较重要的就是CheckConfig,这个类带有main()方法,因此可以单独运行,其主要作用就是检查你的配置文件,并且输出配置文件的相应配置信息。至于其他的本人没有深入了解了。

下面就以上提到的两组类进行详细分析:

第一组:ConfigProperties类是用来处理配置文件的,它继承Properties类,该类有四种构造方式,代码如下:

// =========== Public Constructors ===================================
// ===================================================================


/**
* Calls super constructor.
*/
public ConfigProperties() { super(); }

// -------------------------------------------------------------------

/**
* Constructs a ConfigProperty object from a parameter list.
* The algorithm is as follows: first <code>resource</code> is used to attempt
* loading default values from the given system resource.
* Then all Strings in <code>pars</code> are processed in the order they
* appear in the array. For <code>pars[i]</code>, first a property file
* with the name <code>pars[i]</code> is attempted to be loaded. If the file
* does not exist or loading produces any other IOException, <code>pars[i]</code>
* is interpreted as a property definition, and it is set.
* <p>
* A little inconvenience is that if <code>pars[i]</code> is supposed to be 
* a command line argument, but it is a valid filename at the same time by
* accident, the algorithm will process it as a file instead of a command line
* argument. The caller must take care of that.
* <p>
* No exceptions are thrown, instead error messages are written to the
* standard error. Users who want a finer control should use
* the public methods of this class.
*
* @param pars The (probably command line) parameter list.
* @param resource The name of the system resource that contains the
* defaults. null if there isn't any.
* 
*/
public	ConfigProperties( String[] pars, String resource ) {
	
	try
	{
		if( resource != null )
		{
			loadSystemResource(resource);
			System.err.println("ConfigProperties: System resource "
			+resource+" loaded.");
		}
	}
	catch( Exception e )
	{
		System.err.println("ConfigProperties: " + e );
	}
	
	if( pars == null || pars.length == 0 ) return;
	
	for (int i=0; i < pars.length; i++)
	{
		try
		{
			load( pars[i] );
			System.err.println(
				"ConfigProperties: File "+pars[i]+" loaded.");
			pars[i] = "";
		}
		catch( IOException e )
		{
			try
			{
				loadPropertyString( pars[i] );
				System.err.println("ConfigProperties: Property '" +
					pars[i] + "' set.");
			}
			catch( Exception e2 )
			{
				System.err.println("ConfigProperties: " + e2 );
			}
		}
		catch( Exception e )
		{
			System.err.println("ConfigProperties: " + e );
		}
	}
}

// -------------------------------------------------------------------

/**
* Constructs a ConfigProperty object by loading a file by calling
* {@link #load}.
* @param fileName The name of the configuration file.
*/
public ConfigProperties( String fileName ) throws IOException {

	//重写了Properties的load方法
	load( fileName );
}

// -------------------------------------------------------------------

/**
* Calls super constructor.
*/
public	ConfigProperties( Properties props ) {

	super( props );
}

// -------------------------------------------------------------------

/**
* Calls {@link #ConfigProperties(String[],String)} with resource set to null.
*/
public	ConfigProperties( String[] pars ) {

	this( pars, null );
}

可以通过四种方式处理配置文件。并且重写了父类中的load(String fileName )方法,其代码如下:

/**
* Loads given file. Calls <code>Properties.load</code> with a file
* input stream to the given file.
*/
public void load( String fileName ) throws IOException {
	
	//读取配置文件从fileName的路径下读取
	FileInputStream fis = new FileInputStream( fileName );
	
	//load配置文件,把配置文件一行一行的读出来,并且存储在hashtable中
	//load属于Properties下的一个方法,而Properties其实就是对于一个Hashtable的封装
	load( fis );
	
	//注意关闭IO
	fis.close();
}
这个load方法就是把配置文件一行一行的读出来,并且存储在hashtable中。


而ParseProperties继承自ConfigProperties,并且提供了两种构造方式,代码如下:

// ================= initialization =================================
// ==================================================================

/**
* Calls super constructor.
* @see ConfigProperties#ConfigProperties(String[])
*/
public	ParsedProperties( String[] pars ) {

	super( pars );
}

// ------------------------------------------------------------------

/**
* Calls super constructor.
* @see ConfigProperties#ConfigProperties(String)
*/
//解析配置文件
public	ParsedProperties( String filename ) throws IOException {
	
	//由于ParseProperties重写了ConfigProperties中的load方法,而ConfigProperties又重写了Properties的load方法,
	//所以以下执行的是ParseProperties中的load方法。
	super( filename );
}

这个类中最重要的就是重写了ConfigProperties中的load方法,为配置文件的解析提供了一种自由自我的解析方式,因此对于复杂的配置文件,诸如:

/* This set is used to store prefixes that have been associated
* to brackets blocks. If a prefix is inserted twice, this means
* that there are two blocks referring to the same prefix - 
* which may be caused by a commented prefix in the config
* file, something like this:

*  prefix1
*  {
*    property 1
*  }
*  #prefix2
*  {
*    property 2
*  }
*
*/

都可以提供解析,并且把解析的参数以键值对的方式存储在一个Properties(Properties继承自Hashtable)中。

第二组:这两个类中,Configuration是对ConfigContainer的封装,并且Configuration不提供继承。这组类的主要是从第一组解析出的Properties中提取出参数,并且设置配置参数到到相应的配置属性值上。ConfigContainer提供了所有取得配置参数的方法,比如:getInt()、getDouble()、getInstance()等等。其最主要的行为在于其构造器,其代码如下:

// =================== initialization ================================
// ===================================================================

public ConfigContainer(Properties config, boolean check)
{
	this.config = config;
	this.check = check;
	//此处设置迭代的次数,假如配置文件中没有相应的参数设置默认设置为100
	maxdepth = getInt(Configuration.PAR_MAXDEPTH, Configuration.DEFAULT_MAXDEPTH);

	// initialize protocol id-s
	protocols = new HashMap<String, Integer>();
	String[] prots = getNames(Configuration.PAR_PROT);// they're returned in correct order
	
	for (int i = 0; i < prots.length; ++i) {
		protocols.put(prots[i].substring(Configuration.PAR_PROT.length() + 1), Integer.valueOf(i));
	}
	String debug = config.getProperty(Configuration.PAR_DEBUG);
	if (Configuration.DEBUG_EXTENDED.equals(debug))
		debugLevel = DEBUG_CONTEXT;
	else if (Configuration.DEBUG_FULL.equals(debug)) {
		Map<String, String> map = new TreeMap<String, String>();
		Enumeration e = config.propertyNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = config.getProperty(name);
			map.put(name, value);
		}
		Iterator i = map.keySet().iterator();
		while (i.hasNext()) {
			String name = (String) i.next();
			System.err.println("DEBUG " + name
					+ ("".equals(map.get(name)) ? "" : " = " + map.get(name)));
		}
	} else if (debug != null) {
		debugLevel = DEBUG_REG;
	} else {
		debugLevel = DEBUG_NO;
	}
}
ConfigContainer就是一个配置参数的容器,其可以实现对Properties的解析,提取出所有的配置参数。

而对于CheckConfig,以下只提供运行这个方法的结果,一看就明白这个类的用途:

Warning: Property expressions.maxdepth = 100 (DEFAULT)
Warning: Property include.protocol = null (DEFAULT)
Warning: Property order.protocol = null (DEFAULT)
Warning: Property include.range = null (DEFAULT)
Warning: Property order.range = null (DEFAULT)
Warning: Property network.initialCapacity = 50000 (DEFAULT)
Warning: Property include.protocol = null (DEFAULT)
Warning: Property order.protocol = null (DEFAULT)
Warning: Property include.control = null (DEFAULT)
Warning: Property order.control = null (DEFAULT)
Warning: Property include.protocol = null (DEFAULT)
Warning: Property order.protocol = null (DEFAULT)
Warning: Property protocol.avg.step = 1 (DEFAULT)
Warning: Property protocol.avg.from = 0 (DEFAULT)
Warning: Property protocol.avg.until = 9223372036854775807 (DEFAULT)
Warning: Property protocol.lnk.step = 1 (DEFAULT)
Warning: Property protocol.lnk.from = 0 (DEFAULT)
Warning: Property protocol.lnk.until = 9223372036854775807 (DEFAULT)
Warning: Property  .step = 1 (DEFAULT)
Warning: Property  .from = 0 (DEFAULT)
Warning: Property  .until = 9223372036854775807 (DEFAULT)
Warning: Property control.ao.accuracy = -1.0 (DEFAULT)
Warning: Property control.ao.step = 1 (DEFAULT)
Warning: Property control.ao.from = 0 (DEFAULT)
Warning: Property control.ao.until = 9223372036854775807 (DEFAULT)
Warning: Property include.control.dnet.init = null (DEFAULT)
Warning: Property order.control.dnet.init = null (DEFAULT)
Warning: Property control.dnet.maxsize = 2147483647 (DEFAULT)
Warning: Property control.dnet.minsize = 0 (DEFAULT)
Warning: Property control.dnet.step = 1 (DEFAULT)
Warning: Property control.shf.step = 1 (DEFAULT)
Warning: Property control.shf.from = 0 (DEFAULT)
Warning: Property control.shf.until = 9223372036854775807 (DEFAULT)
才疏学浅,研究不深!先写了这么多,等再研究了,再写点。。。。。。。。。。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园悬赏任务平台对字典管理、论坛管理、任务资讯任务资讯公告管理、接取用户管理、任务管理、任务咨询管理、任务收藏管理、任务评价管理、任务订单管理、发布用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行校园悬赏任务平台程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。校园悬赏任务平台的开发让用户查看任务信息变得容易,让管理员高效管理任务信息。 校园悬赏任务平台具有管理员角色,用户角色,这几个操作权限。 校园悬赏任务平台针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理任务信息,管理任务资讯公告信息等内容。 校园悬赏任务平台针对用户设置的功能有:查看并修改个人信息,查看任务信息,查看任务资讯公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看任务,删除任务操作,新增任务操作,修改任务操作。任务资讯公告信息管理页面提供的功能操作有:新增任务资讯公告,修改任务资讯公告,删除任务资讯公告操作。任务资讯公告类型管理页面显示所有任务资讯公告类型,在此页面既可以让管理员添加新的任务资讯公告信息类型,也能对已有的任务资讯公告类型信息执行编辑更新,失效的任务资讯公告类型信息也能让管理员快速删除。
Peersim是一个用于模拟P2P(对等网络)的开源框架。它提供了一个灵活、可扩展的平台,用于构建和运行对等网络仿真实例。借助Peersim,研究人员可以方便地调查不同算法和策略在P2P网络中的行为和性能。 以一个简单的P2P文件共享系统为例,我们可以使用Peersim进行仿真实验。在这个实例中,我们假设有一个包含多个节点的P2P网络,每个节点具有上传和下载文件的能力。我们可以设置每个节点的带宽和存储空间。 首先,我们需要定义节点的行为和通信协议。节点可以选择是一个文件的提供者(有该文件的副本)还是一个文件的请求者。当一个节点成为一个文件的请求者时,它可以向其他节点发出请求,并从上传者那里下载文件。节点之间的通信可以使用经典的请求-响应模式。 接下来,我们可以设置节点和网络的参数。例如,我们可以设置节点的初始文件和索引,以及它们之间的邻居关系。我们还可以设置节点的带宽和存储空间大小,以模拟实际情况中的资源限制。 然后,我们可以定义仿真实验的评价指标。例如,我们可以测量文件传递的延迟、节点之间的负载平衡、系统的容错性等。这些指标可以帮助我们评估不同的算法和策略在P2P文件共享系统中的性能。 最后,我们可以使用Peersim运行仿真实验并收集数据。Peersim提供了一个简单易用的仿真引擎,可以在真实时间或步骤数的基础上运行仿真。通过分析收集到的数据,我们可以评估不同算法和策略的优劣,并提出改进和优化的建议。 总而言之,Peersim是一个强大的工具,可用于实现P2P网络的仿真实例。通过使用Peersim进行仿真实验,我们可以更好地理解和改进P2P网络的行为和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值