アットウィキロゴ

test1

package test.nonblock;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.List;
 
public class TestServer {
 
    private static int port = 10000;
    private static String group;
    private static String name;
    private static List<String> types = Arrays.asList(new String[]{"aa", "bb"});
    /**
     * @param args
     */
    public static void main(String[] args) {
        ServerSocketChannel serverChannel = null;
        try {
            serverChannel = ServerSocketChannel.open();
            serverChannel.socket().bind(new InetSocketAddress(port));
            while (true) {
                SocketChannel channel = serverChannel.accept();
                SocketAddress addr = channel.socket().getRemoteSocketAddress();
                System.out.println(addr + ":[接続]");
                Child child = new Child(channel, addr);
                child.setParam(group, name, types);
                child.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverChannel != null && serverChannel.isOpen()) {
                try {
                    System.out.println("停止");
                    serverChannel.close();
                } catch (IOException e) {}
            }
        }
    }
 
 
    static class Child extends Thread {
        private static final short DT_TYPE = 0x01;
        private static final short S_TYPE = 0x03;
 
        private static final int BUF_SIZE = 8192;
        private SocketChannel channel = null;
        private SocketAddress address = null;
        private String group;
        private String name;
        private List<String> types;
 
        public Child(SocketChannel channel, SocketAddress addr) {
            this.channel = channel;
            this.address = addr;
        }
 
        public void setParam(String group, String name, List<String> types) {
            this.group = group;
            this.name = name;
            this.types = types;
        }
 
        public void run() {
            ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE);
            buf.order(ByteOrder.BIG_ENDIAN);
            try {                
                this.rcvAndSendSchmea();
 
                int headersize = 6;
                boolean init = true;
                while (true) {
                    if (init) {
                        buf.clear();
                        buf.limit(headersize);
                        if (channel.read(buf) <= 0) {
                            System.out.println("failed to get header:" + address);
                            return;
                        }
                        this.printData(buf);
                        buf.flip();
                        if (buf.getShort() != DT_TYPE) {
                            System.out.println("header 0-1 value is invalid:" + address);                            
                        }
                        int dtsize = 0;
                        if ((dtsize = buf.getShort()) < 0) {
                            System.out.println("header 2-3 value is invalid:" + address);                            
                        }
                        buf.clear();
                        buf.limit(dtsize);
                        // read data
                        if (channel.read(buf) <= 0) {
                            System.out.println("failed to get data:" + address);
                            return;
                        }
                        this.printData(buf);
                    }
                    if (channel.read(buf) <= 0) {
 
                    }
                    buf.flip();
                    buf.clear();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println(address + ":[切断しました]");
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (IOException e) {}
                }
            }
        }
 
        private void printData(ByteBuffer buf) {
            int size = buf.limit();
            System.out.print("[size=" + size + "]");
            byte[]  ba = buf.array();
            for (int i = 1; i < size; i++) {
                System.out.printf("%02x", ba[i]);                
            }
            System.out.println();
 
        }
 
        private void rcvAndSendSchmea() throws IOException {
            int rcvsize = 4;
            int cnt = 0;
            int rc;
            ByteBuffer buf = ByteBuffer.allocate(rcvsize);
            buf.order(ByteOrder.BIG_ENDIAN);
            if ((rc = channel.read(buf)) >= 0) {
                this.printData(buf);
                cnt += rc;
                if (cnt >= 4) {                    
                    this.sendSchema(buf);
                }
            } else {
                throw new IOException("already closed");
            }
        }
 
        private void sendSchema(ByteBuffer buf) throws IOException {
            buf.flip();
            short type = buf.getShort();
            short ext = buf.getShort();
            if (type != S_TYPE || ext != 0) {
                ///
            }
 
            ByteBuffer sndbuf = ByteBuffer.allocate(BUF_SIZE);
            sndbuf.order(ByteOrder.BIG_ENDIAN);
            ////
            byte[] ba = new byte[]{1,1,1,1,1,1,1};
            sndbuf.putShort((short) ba.length);
            sndbuf.put(ba);
            sndbuf.flip();
            channel.write(sndbuf);
        }
 
    }
 
}
 
最終更新:2014年08月23日 15:33