`

java模拟selector的附带功能

    博客分类:
  • java
阅读更多
package com.event;

import java.util.Map;

public interface EventListener {

void onEvent(Map event);
}



package com.socket;

import java.nio.ByteBuffer;
import java.util.HashMap;

import com.event.EventListener;
/*
* 预处理类
*/
public class BaseObject {

private boolean flag;
private ByteBuffer receivebuffer= ByteBuffer.allocate(20000);

public void print(){
System.out.println("hello!");
flag=true;
}

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}
public ByteBuffer getReceivebuffer() {
return receivebuffer;
}


public void invoke(EventListener event){

System.out.println("BaseObject invoke");
HashMap<String, String> map=new HashMap<String, String>();
map.put("key", "wellcome here");
event.onEvent(map);
}
}


package com.socket;


public class Client{


     /*
      * 对应的远程客户端
      */
public Client(){


}
public void run( ) {

System.out.println("client run");

}




}


package com.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Server {

private ServerSocketChannel channel;
private SocketController sc;

public Server() {
try {
sc = new SocketController();
sc.start();
channel = ServerSocketChannel.open();
channel.socket().bind(new InetSocketAddress(8080));
} catch (IOException e) {
e.printStackTrace();
}

}

private void start() {
try {
while (true) {
try {
SocketChannel socket = channel.accept();
socket.configureBlocking(false);
sc.registerSocket(socket);
} catch (IOException e) {
e.printStackTrace();
}

}
} catch (Exception e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
new Server().start();
}
}



package com.socket;

import java.nio.channels.SocketChannel;


public class SocketConnector {

private  SocketChannel socket;
private  Object attachment;


public SocketConnector(SocketChannel socket,Object attachment){
this.socket=socket;
this.attachment=attachment;
}

public  SocketChannel getSocket() {
return socket;
}

public void setSocket(SocketChannel socket){
this.socket=socket;
}
public  Object getAttachment() {
return attachment;
}

public void attach(Object attach){
this.attachment=attach;
}



}

package com.socket;

import java.io.IOException;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Map;

import com.event.EventListener;

/*
* 模拟selector的简单处理类
*/
public class SocketController extends Thread {

private ArrayList<SocketConnector> clients;
private ArrayList<SocketConnector> oldClients;
private ArrayList<SocketConnector> newClients;

private boolean change;
public static Object sync;

public SocketController() {
clients = new ArrayList<SocketConnector>();
oldClients = new ArrayList<SocketConnector>();
newClients = new ArrayList<SocketConnector>();
change = false;
sync = new Object();
}

public void run() {

while (true) {

if (change) {
synchronized (sync) {
clients.removeAll(oldClients);
clients.addAll(newClients);

newClients.clear();
oldClients.clear();
change = false;

}

}

for (int i = 0; i < clients.size(); i++) {

final SocketConnector sc = clients.get(i);
Object obj = sc.getAttachment();
final SocketChannel socket = sc.getSocket();

if (obj instanceof BaseObject) {

BaseObject bo = (BaseObject) obj;

if (!bo.isFlag()) {
bo.print();

} else {

try {

socket.read(bo.getReceivebuffer());
// 加入处理逻辑
bo.getReceivebuffer().clear();

bo.invoke(new EventListener() {

@Override
public void onEvent(Map event) {
System.out.println("invoke return:"
+ event.get("key"));
// 预处理通过,则生成对应的客户端类
Client client = new Client();

sc.attach(client);
}

});

} catch (IOException e) {
e.printStackTrace();
}

}
}

if (obj instanceof Client) {
Client client = (Client) obj;
client.run();
}

}
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

public void registerSocket(SocketChannel socket) {
BaseObject hsp = new BaseObject();
SocketConnector sc = new SocketConnector(socket, hsp);
synchronized (sync) {
newClients.add(sc);
change = true;
}

}

public void unRegisterSocket(SocketConnector sc){
synchronized (sync) {
oldClients.add(sc);
change = true;
}
}

}


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics