package buzzerproxy.bprot; import java.net.Socket; import buzzerproxy.Crypto; import buzzerproxy.util.ByteProtUtil; /** * @author M. Friedeboldt */ public class ByteProtComm extends Thread { private ByteProtUtil byteProtUtil; private SessionManager sessionManager; private boolean crypt = false; /** * @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(); } } /** * lesen des Schlüssels von Socket * @return */ private boolean readKey() throws Exception { byte key = byteProtUtil.getByte(); switch ( key ) { /* * Register / Session */ case (byte) 0xE1: long sessionID = sessionManager.createNewSession(); byteProtUtil.sendSession( sessionID ); break; /* * 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; /* * Question Ready ? */ case (byte) 0x02: // Session ID sid = byteProtUtil.readSession(); session = sessionManager.getSession( sid ); // Antwort byteProtUtil.sendByte( (byte) (session.isReady() ? 1 : 0) ); break; /* * Send Answer Count */ case (byte) 0x20: // Session ID sid = byteProtUtil.readSession(); session = sessionManager.getSession( sid ); // Antwort byteProtUtil.sendInt( session.getAnswerCount() ); break; /* * 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 ); // crypt if (crypt) ; // Lenght byteProtUtil.sendInt( answer.length() ); // Data byteProtUtil.sendData( answer ); break; /* * close */ case (byte) 0x04: // Session ID sid = byteProtUtil.readSession(); sessionManager.removeSession( sid ); return true; } return false; } }