Search in sources :

Example 1 with Z21Message

use of jmri.jmrix.roco.z21.Z21Message in project JMRI by JMRI.

the class Z21SimulatorAdapter method generateReply.

// end of run.
// generateReply is the heart of the simulation.  It translates an
// incoming XNetMessage into an outgoing XNetReply.
// document values for specific cases
@SuppressWarnings("fallthrough")
private Z21Reply generateReply(Z21Message m) throws Exception {
    log.debug("generate Reply called with message {}", m);
    Z21Reply reply;
    switch(m.getOpCode()) {
        case 0x0010:
            // request for serial number
            reply = getZ21SerialNumberReply();
            break;
        case 0x001a:
            // request for hardware version info.
            reply = getHardwareVersionReply();
            break;
        case 0x0040:
            // XPressNet tunnel message.
            XNetMessage xnm = getXNetMessage(m);
            log.debug("Received XNet Message: {}", m);
            XNetReply xnr = xnetadapter.generateReply(xnm);
            reply = getZ21ReplyFromXNet(xnr);
            break;
        case 0x0030:
            // to indicate this.
            throw (new Exception());
        case 0x0050:
            // set broadcast flags
            flags[0] = m.getElement(4) & 0xff;
            flags[1] = m.getElement(5) & 0xff;
            flags[2] = m.getElement(6) & 0xff;
            flags[3] = m.getElement(7) & 0xff;
            // per the protocol, no reply is generated.
            reply = null;
            break;
        case 0x0051:
            // get broadcast flags
            reply = getZ21BroadCastFlagsReply();
            break;
        case 0x0089:
            // Get Railcom Data
            reply = getZ21RailComDataChangedReply();
            break;
        case 0x0060:
        // get loco mode
        case 0x0061:
        // set loco mode
        case 0x0070:
        // get turnout mode
        case 0x0071:
        // set turnout mode
        case 0x0081:
        // get RMBus data
        case 0x0082:
        // program RMBus module
        case 0x0085:
        // get system state
        case 0x00A2:
        // loconet data from lan
        case 0x00A3:
        // loconet dispatch address
        case 0x00A4:
        // get loconet detector status
        default:
            reply = getXPressNetUnknownCommandReply();
    }
    return reply;
}
Also used : Z21Reply(jmri.jmrix.roco.z21.Z21Reply) XNetReply(jmri.jmrix.lenz.XNetReply) XNetMessage(jmri.jmrix.lenz.XNetMessage)

Example 2 with Z21Message

use of jmri.jmrix.roco.z21.Z21Message in project JMRI by JMRI.

the class Z21SimulatorAdapter method run.

@Override
public void run() {
    // try connecting to the server
    try (DatagramSocket s = new DatagramSocket(COMMUNICATION_UDP_PORT)) {
        log.debug("socket created, starting loop");
        while (true) {
            log.debug("simulation loop");
            // the server waits for a client to connect, then echos the data sent back.
            // input from network
            byte[] input = new byte[100];
            try {
                // to receive the data, we create a packet.
                DatagramPacket receivePacket = new DatagramPacket(input, 100);
                // and wait for the data to arrive.
                s.receive(receivePacket);
                Z21Message msg = new Z21Message(receivePacket.getLength());
                for (int i = 0; i < receivePacket.getLength(); i++) msg.setElement(i, receivePacket.getData()[i]);
                // to echo the data back, we need to find the IP and port to send
                // the data to.
                InetAddress IPAddress = receivePacket.getAddress();
                int port = receivePacket.getPort();
                log.debug("Received packet: {}, message: {}", receivePacket.getData(), msg);
                Z21Reply reply;
                // and then we create the return packet.
                try {
                    reply = generateReply(msg);
                } catch (java.lang.Exception e) {
                    // the simulation ends here. break out of the loop.
                    log.debug("error generated by generateReply, exiting simulation");
                    break;
                }
                if (reply != null) {
                    // only attempt to send a reply if there was actually
                    // a reply generated, since some messages don't do that.
                    byte[] ba = jmri.util.StringUtil.bytesFromHexString(reply.toString());
                    DatagramPacket sendPacket = new DatagramPacket(ba, ba.length, IPAddress, port);
                    // and send it back using our socket
                    s.send(sendPacket);
                }
            } catch (Exception ex3) {
                log.debug("IO Exception");
                ex3.printStackTrace();
            }
            log.debug("Client Disconnect");
        }
    } catch (Exception ex0) {
        log.error("Exception opening socket", ex0);
        // can't continue from this
        return;
    }
}
Also used : Z21Reply(jmri.jmrix.roco.z21.Z21Reply) Z21Message(jmri.jmrix.roco.z21.Z21Message) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

Aggregations

Z21Reply (jmri.jmrix.roco.z21.Z21Reply)2 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 XNetMessage (jmri.jmrix.lenz.XNetMessage)1 XNetReply (jmri.jmrix.lenz.XNetReply)1 Z21Message (jmri.jmrix.roco.z21.Z21Message)1