public Message deserialize( DirectByteBuffer data, byte version ) throws MessageException {
Map root = MessagingUtil.convertBencodedByteStreamToPayload( data, 100, getID() );
byte[] id = (byte[])root.get( "identity" );
if( id == null ) throw new MessageException( "id == null" );
if( id.length != 20 ) throw new MessageException( "id.length != 20: " +id.length );
byte[] session = (byte[])root.get("session");
byte[] reconnect = (byte[])root.get("reconn");
byte[] raw_name = (byte[])root.get( "client" );
if( raw_name == null ) throw new MessageException( "raw_name == null" );
String name = new String( raw_name );
byte[] raw_ver = (byte[])root.get( "version" );
if( raw_ver == null ) throw new MessageException( "raw_ver == null" );
String client_version = new String( raw_ver );
Long tcp_lport = (Long)root.get( "tcp_port" );
if( tcp_lport == null ) { //old handshake
tcp_lport = new Long( 0 );
}
Long udp_lport = (Long)root.get( "udp_port" );
if( udp_lport == null ) { //old handshake
udp_lport = new Long( 0 );
}
Long udp2_lport = (Long)root.get( "udp2_port" );
if( udp2_lport == null ) { //old handshake
udp2_lport = udp_lport;
}
Long h_type = (Long)root.get( "handshake_type" );
if( h_type == null ) { //only 2307+ send type
h_type = new Long( HANDSHAKE_TYPE_PLAIN );
}
InetAddress ipv6 = null;
if(root.get("ipv6") instanceof byte[])
{
try
{
InetAddress.getByAddress((byte[]) root.get("ipv6"));
} catch (Exception e)
{
}
}
List raw_msgs = (List) root.get("messages");
if (raw_msgs == null) throw new MessageException("raw_msgs == null");
String[] ids = new String[raw_msgs.size()];
byte[] vers = new byte[raw_msgs.size()];
int pos = 0;
for (Iterator i = raw_msgs.iterator(); i.hasNext();) {
Map msg = (Map) i.next();
byte[] mid = (byte[]) msg.get("id");
if (mid == null) throw new MessageException("mid == null");
ids[pos] = new String(mid);
byte[] ver = (byte[]) msg.get("ver");
if (ver == null) throw new MessageException("ver == null");
if (ver.length != 1) throw new MessageException("ver.length != 1");
vers[pos] = ver[0];
pos++;
}