settingsxml.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 959 行 · 第 1/3 页

JAVA
959
字号
/* Stream-2-Stream - Peer to peer television and radio
 * Project homepage: http://s2s.sourceforge.net/
 * Copyright (C) 2005-2006 Jason Hooks
 * ---------------------------------------------------------------------------
 * 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, or
 * (at your option) any later version.
 *
 * 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.
 *
 * 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 stream2stream.XML;
import stream2stream.network.*;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JRadioButton;
import javax.xml.parsers.*; // JAXP
import javax.xml.transform.*; // JAXP
import javax.xml.transform.dom.DOMSource; // JAXP
import javax.xml.transform.stream.StreamResult; // JAXP
import org.xml.sax.SAXException;
import org.w3c.dom.*;

import ndt.Tcpbw100;

import p2pradio.Messages;
import p2pradio.Radio;
import p2pradio.logging.Logger;
import stream2stream.network.UniversalSocket;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class SettingsXML {
	public static final int TCP = 0;
	//Datagrams
	public static final int UDP = 1;
	public static final int MULTICAST_PLUS = 2; 
	
	public static final String serverFilename = "ServerSettings.xml";
	public static final String clientFilename = "ClientSettings.xml";
	public static final boolean START_MEDIA_PLAYER_IN_LISTENING_MODE= true;
	public static final boolean START_MEDIA_PLAYER_IN_BROADCASTING_MODE = false;
	public static final boolean SIGN_STREAM_PACKETS = false;
	public static final boolean VERIFY_STREAM_PACKETS = false;
	public static final String TRUE = "1";
	public static final String FALSE = "0";
	
	private double playerGain = .5;
	private double LAN_Max_Upload = -2;//-2 will serve up to 2 childs
	private double LAN_Max_Download = -2;//-2 will download up to 2 streams
	private double Internet_Upload_Limit = 0;
	private double Internet_Download_Limit = 0;
	private double Internet_Upload_Max = 0;
	private double Internet_Download_Max = 0;
	private boolean serverUsesVerifiedBandwidth = true;
	private int port = 2000; 
	private int MinimumChildrenClientsMustServe = 0;
	private int ttl = 255;
	private boolean displayDebug = false;
	private boolean logByDate = false;
	private boolean enableMonitor = false;
	private boolean bTest = false;
	private TreeMap links;
	private Document doc;
	private Element rootElement, history, network, advanced, general, server, last;
	private boolean isServer, isValid, startMediaPlayerAtStartup,
	signOrVerify;
	private boolean negativeLag = false;
	private String ServerOrClient;
	private String monitorAddress = "";
	private String encrypted = "";
	private String logFilename = "";
	private String multicastAddress = "";
	private int preset = MULTICAST_PLUS;
	private Transformer idTransform;
	private Result output;
	private Source input;
	private File xmlFile;
	private String IP;
	private boolean useIntegratedPlayer = true;
	private int messageMode = UDP;
	private int streamMode = MULTICAST_PLUS;
	private int stationPanelChoice = 0;  //Internet
	private boolean Allow_Connections_From_Remote_Hosts = false;
	
	public SettingsXML(boolean isServer, String filename)
	{
		isValid  = false;
		if (isServer)
		{
			signOrVerify = SIGN_STREAM_PACKETS; 
			startMediaPlayerAtStartup = START_MEDIA_PLAYER_IN_BROADCASTING_MODE;
		}
		else
		{
			signOrVerify = VERIFY_STREAM_PACKETS;
			startMediaPlayerAtStartup = START_MEDIA_PLAYER_IN_LISTENING_MODE;
		}
		ServerOrClient = filename;
		
		this.isServer = isServer; 
		String sBool;
		xmlFile = new File (ServerOrClient);
		try {
		      DocumentBuilderFactory factory 
		       = DocumentBuilderFactory.newInstance();
		      //factory.setIgnoringElementContentWhitespace(true);
		      DocumentBuilder parser = factory.newDocumentBuilder();
		      doc = parser.parse(xmlFile); 
		      rootElement = doc.getDocumentElement();
		      history = findTag(rootElement, "history");
		      advanced = findTag(rootElement, "advanced");
		      general = findTag(rootElement, "general");
		      network = findTag(rootElement, "network");
		      server = findTag(rootElement, "server");
		      last = findTag(rootElement, "last");
		      
		      if (network != null)
		      {
		    	  Element bandwidthTag = findTag(network, "bandwidth");
		    	  if (bandwidthTag != null)
		    	  {
		    		  Element unverified = findTag(bandwidthTag, "unverified");
		    		  if (unverified != null)
		    		  {
		    			  Element uploadTag = findTag(unverified, "upload");
		    			  if (uploadTag != null)
		    				  LAN_Max_Upload = Double.parseDouble(uploadTag.getFirstChild().getNodeValue());
		    			  Element downloadTag = findTag(unverified, "download");
		    			  if (downloadTag != null)
		    				  LAN_Max_Download = Double.parseDouble(downloadTag.getFirstChild().getNodeValue());
		    		  }
		    		  
		    		  Element verified = findTag(bandwidthTag, "verified");
		    		  if (verified != null)
		    		  {
		    			  Element max = findTag(verified, "max");
		    			  if (max != null)
		    			  {
		    				  encrypted = firstChild(max);
		    				  int index = encrypted.indexOf(" ");
		    				  if (index != -1)
		    				  {
		    					  String base64key = encrypted.substring(0, index);
		    					  //System.out.println("key:" + base64key);
		    						
		    					  String info = encrypted.substring(index + 1, encrypted.length());
		    					  //System.out.println("info:" + info);
		    						
		    					  byte[] key = new byte[10];
		    					  try {
		    						  key = new sun.misc.BASE64Decoder().decodeBuffer(base64key);
		    					  } catch (IOException e) {
		    							// TODO Auto-generated catch block
		    						  e.printStackTrace();
		    					  }
		    					  SecretKey myKey = new SecretKeySpec(key, "DES");
		    					  DesEncrypter decoder = new DesEncrypter(myKey);
		    					  String decodedInfo = decoder.decrypt(info);  //The decoded "IP Upload Download"
		    					  int ipIndex = decodedInfo.indexOf(" ");
		    					  int uploadIndex = decodedInfo.indexOf(" ", ipIndex + 1);
		    					  IP = decodedInfo.substring(0, ipIndex);
		    					  String upload = decodedInfo.substring(ipIndex + 1, uploadIndex);
		    					  String download = decodedInfo.substring(uploadIndex + 1, decodedInfo.length());
		    					  Internet_Upload_Max = Double.parseDouble(upload);
		    					  Internet_Download_Max = Double.parseDouble(download);
		    				  }
		    			  }
		    			  Element limit = findTag(verified, "limit");
		    			  if (limit != null)
		    			  {
		    				  Element upload = findTag(limit, "upload");
		    				  if (upload != null)
		    					  Internet_Upload_Limit = Double.parseDouble(upload.getFirstChild().getNodeValue());
		    				  Element download = findTag(limit, "download");
		    				  if (download != null)
		    				  	  Internet_Download_Limit = Double.parseDouble(download.getFirstChild().getNodeValue());
		    				  if (Internet_Upload_Limit < 0 || Internet_Upload_Limit > Internet_Upload_Max)
		    					  Internet_Upload_Limit = Internet_Upload_Max;
		    				  if (Internet_Download_Limit < 0 || Internet_Download_Limit > Internet_Download_Max)
		    					  Internet_Download_Limit = Internet_Download_Max;
		    			  }
		    		  }
		    		  
		    		 
		    	  }
		    	  Element portTag = findTag(network, "streamport");
		    	  if (portTag != null)
		    		  port = Integer.parseInt(portTag.getFirstChild().getNodeValue());
		      }
		      
		      if (history != null)
		      {
		    	  links = discoverLinks(history);
		      }
		      
		      if (general != null)
		      {
		    	  Element logging = findTag(general, "logging");
		    	  if (logging != null)
		    	  {
		    		  Element log = findTag(logging, "log");
		    		  sBool = firstChild(log);
		    		  if (sBool.equals(TRUE))
		    			  logByDate = true;
		    		  else if (sBool.equals(FALSE))
		    			  logByDate = false;
		    		  Element logAs = findTag(logging, "logAs");
		    		  if (logAs != null)
		    		  {
		    			  Node child = logAs.getFirstChild();
		    			  if (child != null)
		    				  logFilename = child.getNodeValue(); 
		    		  }
		    		  Element displayDebug = findTag(logging, "displaydebug");
		    		  sBool = firstChild(displayDebug);
		    		  if (sBool.equals(TRUE))
		    			  this.displayDebug = true;
		    		  else if (sBool.equals(FALSE))
		    			  this.displayDebug = false;
		    	  }
		    	  
		    	  Element useIntegrated = findTag(general, "use_integrated_media");
		    	  sBool = firstChild(useIntegrated);
		    	  if (sBool.equals(TRUE))
		    		  useIntegratedPlayer = true;
		    	  else if (sBool.equals(FALSE))
		    		  useIntegratedPlayer = false;
		    	  
		    	  Element startMedia = findTag(general, "start_media_player_at_startup");
		    	  sBool = firstChild(startMedia);
		    	  if (sBool.equals(TRUE))
		    		  startMediaPlayerAtStartup = true;
		    	  else if (sBool.equals(FALSE))
		    		  startMediaPlayerAtStartup = false;
		    		
		      }
		      
		      if (advanced != null)
		      {
		    	  if (isServer)
		    	  {
		    		  Element sign = findTag(advanced, "sign");
		    		  sBool = firstChild(sign);
		    		  if (sBool.equals(TRUE))
		    			signOrVerify = true;
		    		  else if (sBool.equals(FALSE))
		    			  signOrVerify = false;
		    		  
		    	  }
		    	  else
		    	  {
		    		  Element verify = findTag(advanced, "verify");
		    		  sBool = firstChild(verify);
		    		  if (sBool.equals(TRUE))
		    			signOrVerify = true;
		    		  else if (sBool.equals(FALSE))
		    			signOrVerify = false;
		    	  }
		    	  Element monitor = findTag(advanced, "monitor");
	    		  if (monitor != null)
	    		  {
	    			  Element enable = findTag(monitor, "enable");
	    			  if (enable != null)
	    			  {
	    				  sBool = firstChild(enable);
	    				  if (sBool.equals(TRUE))
	    					  enableMonitor = true;
	    				  else if (sBool.equals(FALSE))
	    					  enableMonitor = false;
	    			  }
	    			  if (isServer)
	    			  {
	    				  Element address = findTag(monitor, "address");
	    				  if (address != null && firstChild(address) != "-1")
	    					  monitorAddress = firstChild(address);
	    				  	 
	    			  }
	    		  }
	    		  Element nLag = findTag(advanced, "nlag");
	    		  if (nLag != null)
	    		  {
	    			  sBool = firstChild(nLag);
	    			  if(sBool.equals(TRUE))
	    				  negativeLag = true;
	    			  else if (sBool.equals(FALSE))
	    				  negativeLag = false;
	    		  }
		    	  
		      }
		      if (server != null)
		      {
		    	  Element minChildren = findTag(server, "min_children_clients_serve");
	    		  if (minChildren != null)
	    			  MinimumChildrenClientsMustServe = Integer.parseInt(firstChild(minChildren));
	    		  Element verifiedBandwidth = findTag(server, "verified_bandwidth");
	    		  if (verifiedBandwidth != null)
	    		  {
	    			  sBool = firstChild(verifiedBandwidth);
	    			  if(sBool.equals(TRUE))
	    				  serverUsesVerifiedBandwidth = true;
	    			  if(sBool.equals(FALSE))

⌨️ 快捷键说明

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