BeWoMitarbeiterapp: Socket-Management überarbeitet

This commit is contained in:
staccatomamba
2016-08-05 16:23:58 +02:00
parent 83d1267e54
commit 28b2eb9510
13 changed files with 418 additions and 707 deletions

View File

@@ -0,0 +1,217 @@
package tcpConnection;
import android.util.Log;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import soapConnection.DataIdentifier;
import soapConnection.Packet;
import soapConnection.SoapCalls;
/**
* Created by JettenM on 04.08.2016.
*/
public class SocketManager {
private static final String TAG = "SOCKET_MANAGER";
public static String tenant;
public static Socket socket;
public static Thread listenerThread;
public static SocketListener socketListener;
public static UIHandler uiHandler;
public static DataOutputStream dataOutputStream;
public static void connectToServer(){
new Thread(new OpenConnection()).start();
}
public static void sendMessage(Packet packet) {
new Thread(new MessageSender(packet.getDataStream())).start();
}
public static class OpenConnection implements Runnable {
public void run() {
try {
socket = new Socket(SoapCalls.DESTINATION_ADDRESS, SoapCalls.DESTINATION_PORT);
LoginOnServer();
dataOutputStream = new DataOutputStream(socket.getOutputStream());
socketListener = new SocketListener();
listenerThread = new Thread(socketListener);
listenerThread.start();
} catch(Exception exception) {
socket = null;
try {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
connectToServer();
}
}, 30000);
} catch(Exception exception2) {
exception2.printStackTrace();
}
exception.printStackTrace();
}
}
}
public static void closeSocketListener() {
if(listenerThread != null && listenerThread.isAlive()) {
try {
socketListener.terminate();
listenerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void Logout() {
if(socket != null && socket.isConnected()) {
try {
Packet packet = new Packet();
packet.ChatDataIdentifier = DataIdentifier.LOGOUT;
new Thread(new MessageSender(packet.getDataStream())).start();
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
public static void LoginOnServer() {
if(socket != null && socket.isConnected()) {
try {
Packet packet = new Packet();
packet.Tenant = tenant;
packet.ChatDataIdentifier = DataIdentifier.LOGIN;
new Thread(new MessageSender(packet.getDataStream())).start();
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
public static class ConnectionClose implements Runnable{
public void run(){
try {
if (dataOutputStream != null) {
dataOutputStream.flush();
dataOutputStream.close();
}
if (socket != null) {
socket.close();
}
}catch(IOException se){
se.printStackTrace();
}
}
}
public static class MessageSender implements Runnable {
byte[] packetToSend;
public MessageSender(byte[] packet2Send) {
packetToSend = packet2Send;
}
@Override
public void run() {
if(!socket.isConnected()) {
Log.e("MESSAGE_SENDER", "Socket ist nicht verbunden");
}
if(socket != null && dataOutputStream != null) {
try {
dataOutputStream.write(packetToSend);
dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static class SocketListener implements Runnable {
Packet chatPacket;
private volatile boolean isRunning = true;
public void terminate() {
isRunning = false;
}
@Override
public void run() {
try {
while (isRunning) {
if (socket != null) {
if (socket.isConnected()) {
InputStream stream = socket.getInputStream();
int size = stream.available();
if(size == 0) {
continue;
}
StringBuilder total = new StringBuilder(stream.available());
int counter = 0;
byte[] message = new byte[size];
stream.read(message, 0, size);
// while(counter < size) {
//
// char c = (char) stream.read();
//
// total.append(c);
//
// counter++;
// }
try {
//chatPacket = new Packet(total.toString());
chatPacket = new Packet(new String(message, "UTF-8"));
Log.i(TAG, "Empfange Nachricht... " + chatPacket.ChatMessage);
} catch(Exception e) {
e.printStackTrace();
}
if(uiHandler != null) {
uiHandler.updateUserInterface(chatPacket);
} else {
Log.e("ERROR_SOCKETLISTENER", "uiHandler ist null");
}
}
}
}
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
}