package com.abkj.platform.person.test;


import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.SocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.DatagramChannel;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.util.Iterator;


import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;


import com.abkj.platform.person.comm.command.Command;

import com.abkj.platform.person.comm.command.CommandMain;

import com.abkj.platform.person.domain.Station;

import com.abkj.platform.person.service.BaseService;

import com.abkj.platform.person.service.ExcuteService;

import com.abkj.platform.util.PublicTool;


public class UDPClientTest {

@Autowired

private static ExcuteService excuteService;

@Autowired

private static BaseService baseService;

static DatagramChannel channel;

static Selector selector;

SocketAddress sa;

static ByteBuffer bytebuffer1;

static ByteBuffer bytebuffer2 = ByteBuffer.allocate(100);

static byte[] stationid = {1,2,3,31};

static boolean clearposition = true;

static boolean clear = false;

static int max = 8;

static Command command = null;


static int i = 0;//分站数组控制


@Test

public void UDPTest() throws Exception {

run();

}

public static void run() {

Iterator<SelectionKey> iterator;

try {

channel = DatagramChannel.open();

channel.configureBlocking(false);

channel.socket().bind(new InetSocketAddress(10000));

selector = Selector.open();

channel.register(selector, SelectionKey.OP_WRITE);

while (true) {

Thread.sleep(1000l);

int n = selector.select(500l);

if (n == 0) {

channel.register(selector, SelectionKey.OP_WRITE);

continue;

}

iterator = selector.selectedKeys().iterator();

while (iterator.hasNext()) {

SelectionKey key = (SelectionKey) iterator.next();

iterator.remove();

if (key.isValid() && key.isReadable()) {

handleRead(key);

} else if (key.isValid() && key.isWritable()) {

handleWrite(key);

}

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

channel.close();

selector.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

private static void handleRead(SelectionKey key) throws IOException {

channel = (DatagramChannel) key.channel();

channel.receive(bytebuffer2);

byte[] all = bytebuffer2.array();

byte[] receivebuf = new byte[all[1]];

System.arraycopy(all, 0, receivebuf, 0, all[1]);

System.out.print("接收:");

PublicTool.printHexString(receivebuf);

bytebuffer2.clear();

channel.register(selector, SelectionKey.OP_WRITE);

}

private static void handleWrite(SelectionKey key) throws IOException {

channel = (DatagramChannel) key.channel();

byte[] ret = CommandMain.makeCommandWhole(stationid[i], clear,

clearposition, max, command);

i++;

if (i == stationid.length)i = 0;

bytebuffer1 = ByteBuffer.wrap(ret);

System.out.print("发送:");

PublicTool.printHexString(ret);

channel.send(bytebuffer1, new InetSocketAddress("192.168.1.200",

10000));

key.interestOps(SelectionKey.OP_READ);

}


}