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

📄 serverhandler.java

📁 给新学mina的同志一个很好的练习例子
💻 JAVA
字号:
package mina.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoSession;
import org.apache.mina.handler.StreamIoHandler;

public class ServerHandler extends StreamIoHandler {

	List serverList = new ArrayList();
	
	ServerHandler(){
		serverList.add(new RpcServerImpl());
	}
	
	public void messageReceived(IoSession session, Object buf) {
		SocketAddress adr = session.getRemoteAddress();
		System.out.println("remote address is =" + adr.toString());
		System.out.println("buf="+buf.toString());
		if(buf instanceof ByteBuffer){
			ByteBuffer bb = (ByteBuffer)buf;
			System.out.println("bbb==="+bb);
			try {
				Properties prop = (Properties) bb.getObject();
				System.out.println("prop=="+prop);
				
				String interfaceName = (String) prop.get("interface");
				Iterator it = serverList.iterator();
				while(it.hasNext()){//查找实例
					Object serobj = it.next();
					Class[] clazz = serobj.getClass().getInterfaces();
					if(isContains(clazz,interfaceName)){//找到相应实例
						System.out.println("find.."+interfaceName);
						int argc = Integer.parseInt(prop.getProperty("argc"));
						Class[] types = null;
						Object[] args = null;
						
						if(argc!=Integer.MAX_VALUE){
							types = new Class[argc];
							args = new Object[argc];
							for(int i=0;i<argc;i++){
								
								//types[i] = Class.forName(prop.getProperty("type"+i));
								Object typeObj = prop.get("type"+i);
								Object argObj = prop.get("arg"+i);
								System.out.println("arg###="+argObj+"   type@@@="+typeObj);
								System.out.println("arg class="+argObj.getClass());
								System.out.println("type class="+typeObj.getClass());
								
								args[i] = argObj;
								types[i] = (Class)typeObj;
								
								
							}
						}
						String methodName = prop.getProperty("method");
								
						Method method = serobj.getClass().getMethod(methodName,types);
						Object resultObject = method.invoke(serobj,args);
						
						ByteBuffer rb = ByteBuffer.allocate(16);
						rb.setAutoExpand(true);
						rb.putObject(resultObject);
						rb.flip();
						session.write(rb);
					}					
				}
				
				
//				String returnMessage = "OOOOOOOOOOOOOKKKKKKKKKKKK";
//				ByteBuffer rb = ByteBuffer.allocate(16);
//				rb.setAutoExpand(true);
//				rb.putObject(returnMessage);
//				rb.flip();
//				session.write(rb);
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

/*		
	
		SocketAddress adr = session.getRemoteAddress();
		System.out.println("remote address is =" + adr.toString());
		System.out.println("buf="+buf.toString());
		if(buf instanceof ByteBuffer){
			ByteBuffer bb = (ByteBuffer)buf;
			System.out.println("bbb==="+bb);
			try {
				Properties prop = (Properties) bb.getObject();
				System.out.println("prop=="+prop);
				
				Apple a = (Apple) prop.get("apple");
				System.out.println("apple date = "+a.getPdate());
				
				Vector v = (Vector)prop.get("multi");
				for(int i = 0 ;i<v.size();i++){
					System.out.println(v.get(i).toString());
					if(v.get(i) instanceof Apple){
						Apple app = (Apple) v.get(i);
						System.out.println("app date===="+app.getPdate());
					}
				}
				
				String returnMessage = "OOOOOOOOOOOOOKKKKKKKKKKKK";
				ByteBuffer rb = ByteBuffer.allocate(16);
				rb.setAutoExpand(true);
				rb.putObject(returnMessage);
				rb.flip();
				session.write(rb);
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
*/		
		
	}
	
	protected void processStreamIo(IoSession session, InputStream ins,
			OutputStream ous) {

		System.out.println("processStreamIo is called.");
		
//		SocketAddress adr = session.getRemoteAddress();
//		System.out.println("remote address is =" + adr.toString());
//		try {
//			ObjectInputStream ois = new ObjectInputStream(ins);
//			Object obj = ois.readObject();
//			if(obj!=null){
//				Properties prop = (Properties)obj;//cast
//				System.out.println("properties == "+prop);
//				String interfaceName = (String) prop.get("interface");
//				Iterator it = serverList.iterator();
//				while(it.hasNext()){
//					Object serobj = it.next();
//					Class[] clazz = serobj.getClass().getInterfaces();
//					
//					if(isContains(clazz,interfaceName)){//找到相应实例
//						Method m = serobj.getClass().getMethod(prop.getProperty("method"));
//						int args = Integer.parseInt(prop.getProperty("args"));
//						Object[] argvs = new Object[args];
//						for(int i=0;i<args;i++){
//							argvs[i] = prop.getProperty("arg"+i);
//						}
//						
//					}
//					
//				}
//				
//				
//			}
//			
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SecurityException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (NoSuchMethodException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		
	}

	private boolean isContains(Class[] clazz,String ifName){
		for(int i=0;i<clazz.length;i++){
			if(clazz[i].getName().equals(ifName))
				return true;
		}
		return false;
	}
	
	
	
	public void sessionOpened(IoSession ssn) {
		System.out.println("session open for " + ssn.getRemoteAddress());
	}

	public void exceptionCaught(IoSession ssn, Throwable cause) {
		cause.printStackTrace();
		ssn.close();
	}

	public void sessionClosed(IoSession ssn) throws Exception {
		System.out.println("session closed from " + ssn.getRemoteAddress());
	}

}

⌨️ 快捷键说明

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