proxy.java

来自「The source code for this package is loca」· Java 代码 · 共 1,111 行 · 第 1/3 页

JAVA
1,111
字号
			ClientTransaction clientTransaction = timeOutEvent					.getClientTransaction();			ServerTransaction st = null;			transactionsMapping = (TransactionsMapping) st.getApplicationData();			if (transactionsMapping != null) {				st = transactionsMapping						.getServerTransaction(clientTransaction);			}			if (st == null) {				logger						.error("ERROR, Unable to retrieve the server transaction,"								+ " cannot process timeout!");				return;			}			Request request = st.getRequest();			// This removes the given mapping from the table but not			// necessarily the whole thing.			transactionsMapping.removeMapping(clientTransaction);			if (!transactionsMapping.hasMapping(st)) {				// No more mappings left in the transaction table.				try {					Response response = messageFactory.createResponse(							Response.REQUEST_TIMEOUT, request);					st.sendResponse(response);				} catch (ParseException ex) {					ex.printStackTrace();				} catch (SipException ex1) {					ex1.printStackTrace();				} catch (InvalidArgumentException e) {					e.printStackTrace();				}			}		}	}	/***************************************************************************	 * Methods for *************** * starting and stopping the proxy *	 **************************************************************************/	/**	 * Start the proxy, this method has to be called after the init method	 * throws Exception that which can be caught by the upper application	 */	public void start() throws Exception {		if (configuration != null && configuration.isValidConfiguration()) {			Properties properties = new Properties();			// LOGGING property:			if (configuration.enableDebug) {				logger.setLevel(Level.DEBUG);				logger.addAppender(new FileAppender(new SimpleLayout(), configuration.debugLogFile));				logger.debug("DEBUG properties set!");				// The debug log file.				if (configuration.debugLogFile != null) {					properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",							configuration.debugLogFile);				}				// The server log file				if (configuration.serverLogFile != null)					properties.setProperty("gov.nist.javax.sip.SERVER_LOG",							configuration.serverLogFile);				if (configuration.debugLogFile != null)					properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",							"32");				else if (configuration.serverLogFile != null)					properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",							"16");				else					properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",							"0");			} else {				logger.info("DEBUG properties not set!");			}			registrar.setExpiresTime(configuration.expiresTime);			// STOP TIME			if (configuration.stopTime != null) {				try {					long stopTime = Long.parseLong(configuration.stopTime);					StopProxy stopProxy = new StopProxy(this);					Timer timer = new Timer();					timer.schedule(stopProxy, stopTime);				} catch (Exception e) {					e.printStackTrace();				}			}			sipStack = null;			SipFactory sipFactory = SipFactory.getInstance();			sipFactory.setPathName("gov.nist");			headerFactory = sipFactory.createHeaderFactory();			addressFactory = sipFactory.createAddressFactory();			messageFactory = sipFactory.createMessageFactory();			// We have to add the IP address of the proxy for the domain:			configuration.domainList.addElement(configuration.stackIPAddress);			logger.debug("The proxy is responsible for the domain:"					+ configuration.stackIPAddress);			properties.setProperty("javax.sip.STACK_NAME",					configuration.stackName);			// Proxy does not get automatic dialog support.			properties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT", "off");			if (configuration.check(configuration.maxConnections))				properties.setProperty("gov.nist.javax.sip.MAX_CONNECTIONS",						configuration.maxConnections);			if (configuration.check(configuration.threadPoolSize))				properties.setProperty("gov.nist.javax.sip.THREAD_POOL_SIZE",						configuration.threadPoolSize);			if (configuration.domainList != null) {				for (int j = 0; j < configuration.domainList.size(); j++) {					String domain = (String) configuration.domainList							.elementAt(j);					logger							.debug("Here is one domain to take care of:"									+ domain);				}			} else				logger.debug("No domain to take care of...");			// Allows you to acces the proxy log file via RMI			if (configuration.accessLogViaRMI) {				properties.setProperty("gov.nist.javax.sip.ACCESS_LOG_VIA_RMI",						"true");				properties.setProperty("gov.nist.javax.sip.RMI_PORT",						configuration.logRMIPort);				if (configuration.check(configuration.logLifetime))					properties.setProperty("gov.nist.javax.sip.LOG_LIFETIME",							configuration.logLifetime);			}			sipStack = sipFactory.createSipStack(properties);			// Authentication part:			if (configuration.enableAuthentication) {				authentication = new Authentication(this);				try {					Class authMethodClass = Class							.forName(configuration.classFile);					AuthenticationMethod authMethod = (AuthenticationMethod) authMethodClass							.newInstance();					authMethod.initialize(configuration.passwordsFile);					authentication.setAuthenticationMethod(authMethod);				} catch (Exception e) {					logger							.error(									"ERROR, authentication process stopped, exception raised:",									e);					e.printStackTrace();				}			}			// We create the Listening points:			Vector lps = configuration.getListeningPoints();			for (int i = 0; lps != null && i < lps.size(); i++) {				Association a = (Association) lps.elementAt(i);				try {					logger.info("transport  " + a.transport);					logger.info("port  " + Integer.valueOf(a.port).intValue());					ListeningPoint lp = sipStack.createListeningPoint(							configuration.stackIPAddress, Integer.valueOf(									a.port).intValue(), a.transport);					this.listeningPoints.add(lp);					SipProvider sipProvider = sipStack.createSipProvider(lp);					if (this.defaultProvider == null)						this.defaultProvider = sipProvider;					sipProvider.addSipListener(this);				} catch (Exception e) {					e.printStackTrace();					logger.error("ERROR: listening point not created ", e);				}			}			// Export the registry for polling by the responder.			if (configuration.exportRegistry)				// initialize the registrar for RMI.				this.registrar.initRMIBindings();			// Parse static configuration for registrar.			if (configuration.enableRegistrations) {				String value = configuration.registrationsFile;				logger.debug("Parsing the XML registrations file: " + value);				if (value == null || value.trim().equals(""))					logger.debug("You have to set the registrations file...");				else					registrar.parseXMLregistrations(value);			} else				logger.warn("No registrations to parse...");			// Register to proxies if any:			registrar.registerToProxies();		} else {			logger.info("ERROR: the configuration file is not correct!"					+ " Correct the errors first.");		}	}	/**	 * Stop the proxy, this method has to be called after the start method	 * throws Exception that which can be caught by the upper application	 */	public void stop() throws Exception {		if (sipStack == null)			return;		this.presenceServer.stop();		Iterator sipProviders = sipStack.getSipProviders();		if (sipProviders != null) {			while (sipProviders.hasNext()) {				SipProvider sp = (SipProvider) sipProviders.next();				sp.removeSipListener(this);				sipStack.deleteSipProvider(sp);				sipProviders = sipStack.getSipProviders();				logger.info("One sip Provider removed!");			}		} else {			logger.warn("WARNING, STOP_PROXY, The proxy "					+ " has no sip Provider to remove!");		}		Iterator listeningPoints = sipStack.getListeningPoints();		if (listeningPoints != null) {			while (listeningPoints.hasNext()) {				ListeningPoint lp = (ListeningPoint) listeningPoints.next();				sipStack.deleteListeningPoint(lp);				listeningPoints = sipStack.getListeningPoints();				logger.info("One listening point removed!");			}		} else {			logger.warn("WARNING, STOP_PROXY, The proxy "					+ " has no listening points to remove!");		}		registrar.clean();	}	/**	 * Exit the proxy, throws Exception that which can be caught by the upper	 * application	 */	public void exit() throws Exception {		Iterator sipProviders = sipStack.getSipProviders();		if (sipProviders != null) {			while (sipProviders.hasNext()) {				SipProvider sp = (SipProvider) sipProviders.next();				sp.removeSipListener(this);				sipStack.deleteSipProvider(sp);				sipProviders = sipStack.getSipProviders();				logger.info("One sip Provider removed!");			}		} else {			logger.warn("WARNING, STOP_PROXY, The proxy "					+ " has no sip Provider to remove!");		}		Iterator listeningPoints = sipStack.getListeningPoints();		if (listeningPoints != null) {			while (listeningPoints.hasNext()) {				ListeningPoint lp = (ListeningPoint) listeningPoints.next();				sipStack.deleteListeningPoint(lp);				listeningPoints = sipStack.getListeningPoints();				logger.info("One listening point removed!");			}		} else {			logger.warn("WARNING, STOP_PROXY, The proxy "					+ " has no listening points to remove!");		}		logger.debug("Proxy exit.........................");		configuration.listeningPoints.clear();		registrar.clean();	}	public ViaHeader getStackViaHeader() {		try {			ListeningPoint lp = (ListeningPoint) sipStack.getListeningPoints()					.next();			String host = lp.getIPAddress();			int port = lp.getPort();			String transport = lp.getTransport();			// branch id is assigned by the transaction layer.			return headerFactory.createViaHeader(host, port, transport, null);		} catch (Exception e) {			e.printStackTrace();			return null;		}	}	public ContactHeader getStackContactHeader() {		try {			ListeningPoint lp = (ListeningPoint) sipStack.getListeningPoints()					.next();			String host = lp.getIPAddress();			int port = lp.getPort();			String transport = lp.getTransport();			SipURI sipURI = addressFactory.createSipURI(null, host);			sipURI.setPort(port);			sipURI.setTransportParam(transport);			Address contactAddress = addressFactory.createAddress(sipURI);			return headerFactory.createContactHeader(contactAddress);		} catch (Exception e) {			e.printStackTrace();			return null;		}	}	/** ********************************************************************** */	/** ********** The main method: to launch the proxy ************ */	/** ********************************************************************* */	public static void main(String args[]) {		try {			// the Proxy:			if (args.length == 0) {				logger.info("Config file missing!");				System.exit(0);			}			logger.info("Using configuration file " + args[1]);			String confFile = (String) args[1];			Proxy proxy = new Proxy(confFile);			proxy.start();			logger.debug("Proxy ready to work");		} catch (Exception e) {			logger.info("ERROR: Set the configuration file flag: "					+ "USE: -cf configuration_file_location.xml");			logger.info("ERROR, the proxy can not be started, "					+ " exception raised:\n");			e.printStackTrace();		}	}	public void processIOException(IOExceptionEvent arg0) {		logger.error("Unexpected IO Exception!!");	}	public void processTransactionTerminated(TransactionTerminatedEvent txe) {		logger.info("transaction terminated event");	}	public void processDialogTerminated(DialogTerminatedEvent arg0) {		logger.error("Unexpected event... dialog terminated event.");	}}

⌨️ 快捷键说明

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