001package jmri.jmrix.ipocs.protocol.packets;
002
003import java.nio.ByteBuffer;
004
005/**
006 * Client sends this when initiating a connection.
007 *
008 * @author Fredrik Elestedt Copyright (C) 2020
009 * @since 4.21.2
010 */
011@org.openide.util.lookup.ServiceProvider(service = Packet.class)
012public class ConnectionRequestPacket extends Packet {
013  public final static byte IDENT = 1;
014  private short protocolVersion;
015  private String siteDataVersion = null;
016
017  @Override
018  public byte getId() {
019    return IDENT;
020  }
021
022  @Override
023  protected byte[] serializeSpecific() {
024    ByteBuffer buffer = ByteBuffer.allocate(2 + siteDataVersion.length() + 1);
025    buffer.putShort(protocolVersion);
026    buffer.put(siteDataVersion.getBytes());
027    buffer.put((byte)0);
028    buffer.rewind();
029    return buffer.array();
030  }
031
032  @Override
033  protected void parseSpecific(ByteBuffer buffer) {
034    protocolVersion = buffer.getShort();
035    // Get object name
036    StringBuilder sb = new StringBuilder();
037    byte last;
038    while ((last = buffer.get()) != 0x00) {
039      sb.append((char) last);
040    }
041    siteDataVersion = sb.toString();
042  }
043
044  public short getProtocolVersion() {
045    return protocolVersion;
046  }
047
048  public void setProtocolVersion(short protocolVersion) {
049    this.protocolVersion = protocolVersion;
050  }
051
052  public String getSiteDataVersion() {
053    return siteDataVersion;
054  }
055
056  public void setSiteDataVersion(String siteDataVersion) {
057    this.siteDataVersion = siteDataVersion;
058  }
059}