package buzzerproxy.bprot;
import java.net.Socket;
import buzzerproxy.Constant;
import buzzerproxy.Crypto;
import buzzerproxy.IO;
import buzzerproxy.util.ByteProtUtil;
/**
* @author M. Friedeboldt
*/
public class ByteProtComm extends Thread {
private ByteProtUtil byteProtUtil;
private SessionManager sessionManager;
/**
* @param sock
*/
public ByteProtComm(Socket sock, SessionManager sessionManager) {
this.byteProtUtil = new ByteProtUtil( sock );
this.sessionManager = sessionManager;
}
@Override
public void run() {
try {
while ( !readKey() )
;
} catch ( Exception e ) {
e.printStackTrace();
}
}
/**
* [0xE1] Register + Session
* [0x50] Register + Session + Crypto
* [0x01] Question
* [0x02] Question Ready ?
* [0x20] Send Answer Count
* [0x10] Send Answer + index - max
* [0x11] Erfragen der Config Version [0x12] Holen der Config Version (XML)
* [0x04] close
* [0x22] PING
* [0x33] Update Andoid ausliefern - Basti
* lesen des Schlüssels von Socket
*
* @return
*/
private boolean readKey() throws Exception {
byte key = byteProtUtil.getByte();
switch ( key ) {
/*
* [0xE1] Register + Session
*/
case (byte) 0xE1:
long sessionID = sessionManager.createNewSession();
byteProtUtil.sendSession( sessionID );
break;
/*
* [0xE1] Register + Session + Crypto
*/
case (byte) 0x50:
sessionID = sessionManager.createNewSession();
byteProtUtil.sendSession( sessionID );
byte[] cKey = Crypto.generateSessionID( 16 );
byteProtUtil.sendDataW( cKey );
byteProtUtil.setCrypt( new Crypto( cKey ) );
break;
/*
* [0x01] Question
*/
case (byte) 0x01:
// Session ID
long sid = byteProtUtil.readSession();
Session session = sessionManager.getSession( sid );
int anz = byteProtUtil.getInt();
session.setQuestion( byteProtUtil.readData( anz ) );
break;
/*
* [0x02] Question Ready ?
*/
case (byte) 0x02:
// Session ID
sid = byteProtUtil.readSession();
session = sessionManager.getSession( sid );
// Antwort
byteProtUtil.sendByte( (byte) (session.isReady() ? 1 : 0) );
break;
/*
* [0x20] Send Answer Count
*/
case (byte) 0x20:
// Session ID
sid = byteProtUtil.readSession();
session = sessionManager.getSession( sid );
// Antwort
byteProtUtil.sendInt( session.getAnswerCount() );
break;
/*
* [0x10] Send Answer + index - max
*/
case (byte) 0x10:
// Session ID
sid = byteProtUtil.readSession();
session = sessionManager.getSession( sid );
int index = byteProtUtil.getInt();
int max = byteProtUtil.getInt();
String answer = session.getResult( index, max );
// Send Data
byteProtUtil.sendDataW( answer );
break;
/*
* [0x11] Erfragen der Config Version
*/
case (byte) 0x11:
// Session ID
sid = byteProtUtil.readSession();
session = sessionManager.getSession( sid );
// Send Data
byteProtUtil.sendDataW( session.getConfigXMLVersion() );
break;
/*
* [0x12] Holen der Config Version (XML)
*/
case (byte) 0x12:
// Session ID
sid = byteProtUtil.readSession();
session = sessionManager.getSession( sid );
// Send Data
byteProtUtil.sendDataW( session.getConfigurationXML() );
break;
/*
* [0x04] ping + Antwort Ping
*/
case (byte) 0x22:
byteProtUtil.sendByte( (byte) 0x22 );
break;
/*
* [0x04] close
*/
case (byte) 0x04:
// Session ID
sid = byteProtUtil.readSession();
sessionManager.removeSession( sid );
return true;
/**
* [0x33] Andoid Update ausliefern - Basti
*/
case (byte) 0x33:
// Andoid Update ausliefern
IO io = new IO();
byte[] updateData = io.readStreamAsByteArray(Constant.UPDATE_ANDROID);
if ( updateData.length>0 )
byteProtUtil.sendData(updateData);
else
byteProtUtil.sendData( "-1" ); // im fehlerfall etwas zurücksenden
break;
}
return false;
}
}