package de.se.buzzer.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Arrays;
import android.util.Log;
/**
*
* @author M. Friedeboldt
*/
public class MConnection implements Constants {
/** Port to Buzzer Server */
private static final int PORT = ###########;
/** Host to Buzzer Server */
private static final String HOST = "192.168.178.20";
/** Socket to Buzzer Server */
private Socket outputLine;
/**
* Construktor
*/
public MConnection() throws UnknownHostException, IOException {
// Connect
connect();
}
/**
* @throws IOException
* @throws UnknownHostException
*/
public void connect() throws UnknownHostException, IOException {
Log.d( "MConnection", "Create Connect to (" + HOST + ":" + PORT + ")" );
outputLine = new Socket( HOST, PORT );
Log.d( "MConnection", "OK Create Connect to (" + PORT + ")" );
}
/**
* Create Session and register on Buzzer Server
* - CMD_HELLO >> SEND
* - Session ID << READ
* @return
* @throws Exception
*/
public long createSession() throws Exception {
// Anfrage
Log.d( "MConnection", "Send cmd Hello" );
sendByte( CMD_HELLO );
Log.d( "MConnection", "OK Send cmd Hello" );
// Empfang Session ID
Log.d( "MConnection", "Read Session ID" );
long t = readSession();
Log.d( "MConnection", "OK Read Session ID[" + t + "]" );
return t;
}
/**
* Send Question to Server.
* - CMD_QESTION >> SEND
* - Session ID >> SEND
* - Lenght Message >> SEND
* - Message >> SEND
* @param sid
* @param question
* @throws Exception
*/
public void sendQuestion(long sid, String question) throws Exception {
// Anfrage Byte
Log.d( "MConnection", "Send cmd Question" );
sendByte( CMD_QESTION );
Log.d( "MConnection", "OK Send cmd Question" );
// Session
sendSession( sid );
// Lenght
sendInt( question.length() );
// Question
sendData( question );
}
/**
* Is Question Ready on Buzzer Server.
* - CMD_ANSWER_READY >> SEND
* - byte << READ
* @param sid
* @return
*/
public boolean isQuestionReady(long sid) throws Exception {
// Send CMD Byte
Log.d( "MConnection", "Send cmd CMD_ANSWER_READY" );
sendByte( CMD_ANSWER_READY );
Log.d( "MConnection", "Send cmd CMD_ANSWER_READY OK" );
// Session
sendSession( sid );
// Ergebnis
byte b = getByte();
Log.d( "MConnection", "Read CMD_ANSWER_READY [" + b + "]" );
return b == 1;
}
/**
* Get the count from question
* - CMD_CONTENT >> SEND
* - Session ID >> SEND
* - count << READ
* @param sid
* @return
* @throws Exception
*/
public int readAnswerCount(long sid) throws Exception {
// Send CMD Byte
Log.d( "MConnection", "Send cmd CMD_ANSWER_COUNT" );
sendByte( CMD_ANSWER_COUNT );
Log.d( "MConnection", "Send cmd CMD_ANSWER_COUNT OK" );
// Send Session
sendSession( sid );
// Count
return getInt();
}
/**
* Read Content from Server.
* - CMD_CONTENT >> SEND
* - Session ID >> SEND
* - index >> SEND
* - msg lenght << READ
* - msg << READ
* @param sid
* @param index
* @return
* @throws Exception
*/
public String readContent(long sid, int index, int max) throws Exception {
// Send CMD Byte
Log.d( "MConnection", "Send cmd CMD_CONTENT" );
sendByte( CMD_CONTENT );
Log.d( "MConnection", "Send cmd CMD_CONTENT OK" );
// Send Session
sendSession( sid );
// Send Index + Max
sendInt( index );
sendInt( max );
// Read anz + Content
int anz = getInt();
return readData( anz );
}
/**
* Close the Session.
* - CMD_CLOSE >> SEND
* - Session ID >> SEND
* @param sid
*/
public void closeSession(long sid) {
// Send CMD Byte
Log.d( "MConnection", "Send cmd CMD_CLOSE" );
sendByte( CMD_CLOSE );
Log.d( "MConnection", "Send cmd CMD_CLOSE OK" );
// Send Session
sendSession( sid );
}
//*******************************************
//*******************************************
// IO - HELPER
//*******************************************
//*******************************************
/**
* sending Session ID
* @param i
*/
private void sendSession(long l) {
if ( outputLine != null && outputLine.isConnected() ) {
try {
byte[] ba = new byte[8];
for ( int i = 0; i < 64; i += 8 ) {
ba[i >> 3] = new Long( (l & (255L << i)) >> i ).byteValue();
}
outputLine.getOutputStream().write( ba );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
* @return
* @throws Exception
*/
private long readSession() throws Exception {
try {
InputStream in = outputLine.getInputStream();
byte[] ba = new byte[8];
int anz = in.read( ba );
long l = 0;
for ( int i = 0; (i < ba.length) && (i < 8); i++ ) {
l |= (((long) ba[i]) << (i << 3)) & (255L << (i << 3));
}
return l;
} catch ( SocketTimeoutException e ) {
throw new Exception();
}
}
/**
* @return
* @throws Exception
*/
private int getInt() throws Exception {
try {
InputStream in = outputLine.getInputStream();
byte[] ba = new byte[4];
int anz = in.read( ba );
return (ba[0] << 24) + ((ba[1] & 0xFF) << 16) + ((ba[2] & 0xFF) << 8) + (ba[3] & 0xFF);
} catch ( SocketTimeoutException e ) {
}
return 0;
}
/**
* @return
* @throws Exception
*/
private byte getByte() throws Exception {
try {
InputStream in = outputLine.getInputStream();
byte[] b = new byte[1];
in.read( b );
return b[0];
} catch ( SocketTimeoutException e ) {
}
return 0;
}
/**
* @return
*/
public String readData(int anz) {
String res = "";
if ( outputLine != null && outputLine.isConnected() ) {
try {
InputStream g = outputLine.getInputStream();
// länge des Strings
if ( anz > 0 ) {
byte[] in = new byte[anz];
g.read( in );
res = new String( in, "ISO-8859-1" );
}
return res;
} catch ( Exception e ) {
e.printStackTrace();
return null;
}
}
return null;
}
/**
* Send Byte
* @param by
*/
private void sendByte(byte by) {
if ( outputLine != null && outputLine.isConnected() ) {
try {
outputLine.getOutputStream().write( new byte[] { by } );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
* Send Integer
* @param in
*/
private void sendInt(int in) {
if ( outputLine != null && outputLine.isConnected() ) {
try {
outputLine.getOutputStream().write( new byte[] { (byte) (in >>> 24), (byte) (in >>> 16), (byte) (in >>> 8), (byte) in } );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
* Send Data (String)
* @param data
*/
public void sendData(String data) {
if ( outputLine != null && outputLine.isConnected() ) {
try {
OutputStream os = outputLine.getOutputStream();
os.write( data.getBytes() );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}