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

📄 rcrssprotocolsocket.java

📁 2004年robotcup世界冠军源代码
💻 JAVA
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.package yab.io;import java.io.*;import java.net.*;import java.util.*;public class RCRSSProtocolSocket implements ProtocolConstants {    //private final LongUDPSocket m_ludpSkt;    private final LinkedList m_blocks = new LinkedList();    private final TCPIO m_tcpio;    private static final boolean SHARE_INITIAL_MAP = true;    private static ObjectElement[] m_initialMap = null;    public RCRSSProtocolSocket(InetAddress kernelAddress, int kernelPort) {        //m_ludpSkt = new LongUDPSocket(kernelAddress, kernelPort);        m_tcpio = new TCPIO (kernelAddress, kernelPort);    }    public Object receive() {        if (m_blocks.isEmpty()) {            //byte[] data = m_ludpSkt.receive();            byte[] data = m_tcpio.receive ();            ByteArrayInputStream bais = new ByteArrayInputStream(data);            DataInputStream dis = new DataInputStream(bais);            try {                while (dis.available() > 4) // 4: sizeof(HEADER_NULL)                    m_blocks.addLast(getBlock(dis));            } catch(IOException ioe) { throw new Error(ioe); }        }        return m_blocks.removeFirst();    }    private Object getBlock(DataInputStream dis) throws IOException {        int header = dis.readInt();        dis.readInt();  // body length        switch (header) {        case KA_SENSE:         return new KaSense(dis);        case KA_HEAR:          return new KaHear(dis);        case KA_HEAR_SAY:      return new KaHearSay (dis);		case KA_HEAR_TELL:     return new KaHearTell (dis);        case KA_CONNECT_ERROR: return new KaConnectError(dis);        case KA_CONNECT_OK:            //m_ludpSkt.setLastReceivedPackeSourceToDestination();            return getKaConnectOkBlock(dis);        default: throw new Error("received illegal header:" + header);        }    }    private KaConnectOk getKaConnectOkBlock(DataInputStream dis)        throws IOException {        if (!SHARE_INITIAL_MAP)            return new KaConnectOk(dis);        if (m_initialMap != null)            return new KaConnectOk(dis, m_initialMap);        KaConnectOk ok = new KaConnectOk(dis);        m_initialMap = ok.map();        return ok;    }    public void send(int header, Object[] elements) {        //ByteArrayOutputStream baos = new ByteArrayOutputStream();        //DataOutputStream dos = new DataOutputStream(baos);        try {            byte[] body = RCRSSProtocol.body(elements);            m_tcpio.send (header, body);            //dos.writeInt(header);            //dos.writeInt(body.length);            //dos.write(body, 0, body.length);            //dos.writeInt(HEADER_NULL);            //m_ludpSkt.send(baos.toByteArray());            //dos.close();            //baos.close();        } catch (IOException ioe) { throw new Error(ioe); }    }    public static void clearUpAfterInitialization() { m_initialMap = null; }    /** This method is used to establish the connection as fast as     *  possible by adjusting the version of an AK_CONNECT block.     */    public void akConnect(int temporaryId, int agentType) {        int version = (SHARE_INITIAL_MAP  &&  m_initialMap != null) ? 1 : 0;        akConnect(temporaryId, version, agentType);    }    public void akConnect(int temporaryId, int version, int agentType) {        send(AK_CONNECT, new Object[] {            new Integer(temporaryId),            new Integer(version),            new Integer(agentType)});    }    public void akAcknowledge(int selfId) {        send(AK_ACKNOWLEDGE, new Object[] {            new Integer(selfId)});    }    public void akMove (int selfId, int[] routePlan)    {//        ;//System.out.println ("origin " + routePlan[0]);//        send (AK_MOVE, new Object[]{//            new Integer (selfId),//            routePlan});        //49--------------------------------------        send(AK_MOVE, new Object[] {            new Integer(selfId), new Integer(routePlan.length),            routePlan});    }    public void akRescue(int selfId, int targtId) {        send(AK_RESCUE, new Object[] {            new Integer(selfId),            new Integer(targtId)});    }    public void akLoad(int selfId, int targetId) {        send(AK_LOAD, new Object[] {            new Integer(selfId),            new Integer(targetId)});    }    public void akUnload(int selfId) {        send(AK_UNLOAD, new Object[] {            new Integer(selfId)});    }    public void akExtinguish(int selfId, NozzleElement[] nozzles) {        Object[] elements            = new Object[1 + nozzles.length * NozzleElement.NUM_ELEMENTS + 1];        elements[0] = new Integer(selfId);        for (int i = 0;  i < nozzles.length;  i ++)            for (int j = 0;  j < NozzleElement.NUM_ELEMENTS;  j ++)                elements[1 + i + j] = new Integer(nozzles[i].elements[j]);        elements[elements.length - 1] = new Integer(0);        send(AK_EXTINGUISH, elements);    }    public void akClear(int selfId, int targetId) {        send(AK_CLEAR, new Object[] {            new Integer(selfId),            new Integer(targetId)});    }    public void akRest(int selfId) {        send(AK_REST, new Object[] {            new Integer(selfId)});    }    public void akSay(int selfId, String message) {        send(AK_SAY, new Object[] {            new Integer(selfId),            message});    }    public void akTell(int selfId, String message) {        send(AK_TELL, new Object[] {            new Integer(selfId),            message});    }    public void akTell (int selfId, byte[] message)    {        send (AK_TELL, new Object[]{new Integer (selfId), message});//        ByteArrayOutputStream baos = new ByteArrayOutputStream();//        DataOutputStream dos = new DataOutputStream(baos);//        ByteArrayOutputStream body = new ByteArrayOutputStream ();//        DataOutputStream bodyout = new DataOutputStream (body);//        try {//            bodyout.writeInt (selfId);//            bodyout.writeInt (message.length);//            bodyout.write (message, 0, message.length);//            int size = (message.length + 3) & ~3;//            for (int i = message.length;  i <= size;  i ++)//                bodyout.write(0);//            dos.writeInt(AK_TELL);//            dos.writeInt(bodyout.size ());//            dos.write(body.toByteArray (), 0, body.size ());//            dos.writeInt(HEADER_NULL);//            m_ludpSkt.send(baos.toByteArray());//            dos.close();//            baos.close();//            bodyout.close ();//            body.close ();//        } catch (IOException ioe) { throw new Error(ioe); }    }}

⌨️ 快捷键说明

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