📄 serviceserver.java~60~
字号:
import Utilities.Engine;
import java.net.*;
import Services.*;
import java.io.*;
import XML.*;
import Config;
/**
* <p>Title: InHand SMS platform XMLRouter</p>
* <p>Description: InHand 短信服务平台服务路由器</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: 北京映翰通网络技术有限公司</p>
* @author 韩传俊 shock2000@21cn.com
* @version 1.0
*/
public class ServiceServer extends Engine {
ServerSocket server=null;
public ServiceServer() {
super("ServiceServer");
}
public void run(){
try{
this.server=new ServerSocket(Config.local_port);
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage()+"\r\nwhile ServiceServer is listening on port "+Config.local_port+"!");
Config.log.log(e,"while ServiceServer is listening on port "+Config.local_port+"!");
System.out.println("System exit ...");
Config.log.log("System exit ...");
System.exit(0);
}
Socket con=null;
System.out.println("ServiceServer is listening on port "+Config.local_port+"!");
Config.log.log("ServiceServer is listening on port "+Config.local_port+"!");
while(isRunning()){
try{
con=server.accept();
con.setSoTimeout(15000);
con.setSendBufferSize(4096);
con.setReceiveBufferSize(4096);
if(this.checkValid(con)){
if(!registerConn(con)){
con.close();
System.out.println("the connection from ip:"+con.getInetAddress().toString()+" module_id is not right!");
Config.log.log("the connection from ip:"+con.getInetAddress().toString()+" module_id is not right!");
}else{
System.out.println("the connection from ip:"+con.getInetAddress().toString()+" success!");
Config.log.log("the connection from ip:"+con.getInetAddress().toString()+" success!");
}
}else{
con.close();
}
nap();
}catch(Exception e){
Config.log.log(e,"While ServiceServer is listening at port 9000");
e.printStackTrace();
}
}
}
public boolean registerConn(Socket conn){
try{
ServiceInfo serviceInfo=null;
String host=conn.getInetAddress().toString();
System.out.println("rigistered connecet form "+host);
int offset=host.lastIndexOf('/');
host=host.substring(offset+1);
InputStream dataIn=conn.getInputStream();
OutputStream dataOut=conn.getOutputStream();
DataInputStream in=new DataInputStream(dataIn);
String xmlStr ="";
long now=System.currentTimeMillis();
while((xmlStr=in.readLine())==null){
if (System.currentTimeMillis()-now>10000){
//this.shutdown();
Config.log.log("no input for a long period,close the connection");
//this.serviceInfo.clientOutput.shutdown();
//this.serviceInfo.is_connect=false;
}
}
//in.close();
ParseXML xml=new ParseXML();
System.out.println(xmlStr);
xml.parse(xmlStr);
int id=xml.getIntValue("/get_gateway_status.id");
for(int i=0;i<Config.subcode_item.size();i++){
serviceInfo=(ServiceInfo)Config.subcode_item.elementAt(i);
if (serviceInfo.id==id && serviceInfo.client_host.equalsIgnoreCase(host) && !serviceInfo.is_connect){
//ClientOutput clientOutput=new ClientOutput(serviceInfo,dataOut);
//clientOutput.startup();
//ClientInput clientInput=new ClientInput(serviceInfo,in);
//clientInput.startup();
serviceInfo.setConnect(conn,in);
//serviceInfo.clientInput=clientInput;
//serviceInfo.clientOutput=clientOutput;
Config.log.log("the conn from "+host+" is connected module "+serviceInfo.id);
System.out.println("the conn from "+host+" is connected module "+serviceInfo.id);
Config.send_queue.push(xmlStr);
return true;
}
}
for(int i=0;i<Config.service_id_item.size();i++){
serviceInfo=(ServiceInfo)Config.service_id_item.elementAt(i);
if (serviceInfo.id==id && serviceInfo.client_host.equalsIgnoreCase(host) && !serviceInfo.is_connect){
//ClientOutput clientOutput=new ClientOutput(serviceInfo,dataOut);
//clientOutput.startup();
//ClientInput clientInput=new ClientInput(serviceInfo,in);
//clientInput.startup();
serviceInfo.setConnect(conn,in);
//serviceInfo.clientInput=clientInput;
//serviceInfo.clientOutput=clientOutput;
Config.log.log("the conn from "+host+" is connected module "+serviceInfo.id);
System.out.println("the conn from "+host+" is connected module "+serviceInfo.id);
return true;
}
}
//conn.close();
}catch(Exception e){
e.printStackTrace();
Config.log.log(e,"While ServiceServer.checkValid() is checking the module_id");
}
return false;
}
public boolean checkValid(Socket con){
String ip=con.getInetAddress().toString();
int offset=ip.lastIndexOf('/');
ip=ip.substring(offset+1);
//System.out.println("@@@@@@ "+ip);
try{
for (int i=0;i<Config.trustIP.size();i++){
//System.out.println((String)Config.trustIP.elementAt(i));
if(ip.equalsIgnoreCase((String)Config.trustIP.elementAt(i))){
return true;
}
}
//con.close();
}catch(Exception e){
Config.log.log(e,"While ServiceServer.checkValid() is checking the connection from"+ip);
e.printStackTrace();
}
System.out.println("in reviceserver.run:the connection form "+ip+" is not trusting!");
Config.log.log("in reviceserver.run:the connection form "+ip+" is not trusting!");
return false;
}
public static void main(String[] args) {
ServiceServer serviceServer1 = new ServiceServer();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -