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

📄 smscconnectionmanager.java~1~

📁 本人为友邦公司做的模拟网关源代码
💻 JAVA~1~
字号:
package smsc;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:  TALKWEB LTD.
 * @author  Li yongjun
 * @version 2.0
*/

public class SMSCConnectionManager {
	private int mysize;
	private String myname;
	private ServerSocket MOServerSocket = null;
	public static Hashtable SmppHost = null;
	private static Hashtable myconnection = null;
	private ServerListenThread svrLsnMOThread = null;
	private ServerListenThread SvrlsnMtThread = null;
	private static SMSCConnectionManager smsc = null;

	public String getName() {
		return myname;
	}
	public int getSize() {
		return myconnection.size();
	}

	public SMSCConnectionManager(String name, int size) {
		myname = name;
		mysize = size > 0 ? size : 1;
		SmppHost = new Hashtable(10);
		myconnection = new Hashtable (size);
	}

	public static SMSCConnectionManager instance() {
		if(smsc == null)
			smsc = new SMSCConnectionManager("TW SMPPConnectionManager", 4);
		return smsc;
	}

	public boolean setupSMSCServer(String name, int MOPort,int MTPort) {
		try {
			if(MOServerSocket != null) {
				try {
					MOServerSocket.close();
				} catch(Exception e) { }
			}
			MOServerSocket = new ServerSocket( MOPort );
			if(svrLsnMOThread == null)
				svrLsnMOThread = new ServerListenThread(MOServerSocket);
			svrLsnMOThread.start();
			
			SMSCFrame.RecvArea.append("Create ISMG server MO Listener succeed .\n");
			SMSCFrame.RecvArea.append("Listen Server name = " + MOServerSocket.getInetAddress().getLocalHost().getHostName() +
									  "  address = " + MOServerSocket.getInetAddress().getLocalHost().getHostAddress() +
			                          "  port = " + MOPort + "\n");
			
			MOServerSocket = new ServerSocket( MTPort );
			if(svrLsnMOThread == null)
				svrLsnMOThread = new ServerListenThread(MOServerSocket);
			svrLsnMOThread.start();
			
			SMSCFrame.RecvArea.append("Create ISMG server MT Listener succeed .\n");
			SMSCFrame.RecvArea.append("Listen Server name = " + MOServerSocket.getInetAddress().getLocalHost().getHostName() +
			                          "  address = " + MOServerSocket.getInetAddress().getLocalHost().getHostAddress() +
			                          "  port = " + MTPort + "\n");
			return true;
		} catch (Exception e) {
			SMSCFrame.RecvArea.append("Create ISMG server exception, " +
			                          e.getMessage() + " .\n");
			return false;
		}
	}

	public boolean stopSMSCServer() {
		int i, nHostSize = 0;
		//String strConnID, strDest;
		Enumeration  AllHost = null;
		SMSCConnection tmpConn = null;

		try {
			svrLsnMOThread.interrupt();
		} catch(Exception ex)  {   }
		if(MOServerSocket != null) {
			try {
				MOServerSocket.close();
			} catch(Exception e) {   }
		}
		svrLsnMOThread = null;

		try {
			nHostSize = SMSCConnectionManager.myconnection.size();
		} catch(Exception e) {
			nHostSize = 0;
		}
		SMSCFrame.RecvArea.append("Total " + nHostSize + " SMSC connection .\n");
		if(nHostSize > 0) {
			try {
				AllHost = SMSCConnectionManager.myconnection.elements();
				//if(AllHost == null)
				//SMSCFrame.RecvArea.append("AllHost is null .\n");
				for(i=0; i<nHostSize; i++) {
					//strConnID = (String)AllHost.nextElement();
					tmpConn = (SMSCConnection)AllHost.nextElement();
					//SMSCFrame.RecvArea.append(strConnID + " disconnected .\n");
					//this.delConnection(strConnID);
					tmpConn.disconnect();
					delConnection(tmpConn.getMyID());
				}
			} catch(Exception eStop) {
				SMSCFrame.RecvArea.append("Stop SMSC exception, " + eStop.getMessage() + " .\n");
				return false;
			}
		}
		try {
			this.SmppHost.clear();
		} catch(Exception eHost)  {
			return false;
		}
		return true;
	}

	public boolean send(SmscShortMsg smpp, String smppID) {
		SMSCConnection smscConnection = null;
		try {
			smscConnection = route(smppID);
		} catch(Exception e) {
			smscConnection = null;
		}
		if ( smscConnection == null ) {
			if(Constants.SMSC_DEBUG)
				SMSCFrame.SendArea.append("SMSC manager return SMSC connection is null .\n");
			return false;
		}

		if ( !smscConnection.getStatus().equals(Constants.WORKING) ) {
			SMSCFrame.SendArea.append("The SMSC connection not bind, could not send message .\n");
			return false;
		}
		smscConnection.send(smpp);
		return true;
	}


	public boolean removeHost(String hostIP) {
		try {
			SmppHost.remove(hostIP);
			return true;
		} catch(Exception e) {
			return false;
		}
	}

	public boolean addHost(String host, String hostIP) {
		try {
			SmppHost.put(host, hostIP);
			//SMSCFrame.RecvArea.append("Add " + host + " to hash table succeed .\n");
			return true;
		} catch(Exception e) {
			SMSCFrame.RecvArea.append("Insert host address to hash table failed .\n");
			return false;
		}
	}

	public SMSCConnection route(String smppID) {
		try {
			return (SMSCConnection) myconnection.get(smppID);
		} catch(Exception e) {
			SMSCFrame.SendArea.append("SMSC manager could not find peer " + smppID + " .\n");
			return null;
		}
	}

	public void setConncection(String smppID, SMSCConnection sc) {
		try {
			myconnection.put(smppID, sc);
		} catch(Exception e) {
			SMSCFrame.RecvArea.append(" Insert SMSC connetion handle to hash table failed .\n");
		}
	}

	public void delConnection(String smppID) {
		try {
			myconnection.remove(smppID);
		} catch(Exception e)   {      }
	}

	public boolean isConncected(String smppID) {
		if( myconnection.containsKey(smppID) )
			return true;
		else
			return false;
	}

}

⌨️ 快捷键说明

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