Diverse Änderungen am ChatServer
This commit is contained in:
@@ -1,15 +1,28 @@
|
||||
package tcpConnection;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import Database.DatabaseHandler;
|
||||
import beyondsoft.bewomitarbeiterapp.BeWoChatApplication;
|
||||
import beyondsoft.bewomitarbeiterapp.R;
|
||||
import entities.ChatEntity;
|
||||
import entities.ChatMessage;
|
||||
import entities.NotificationLog;
|
||||
|
||||
import soapConnection.DataIdentifier;
|
||||
import soapConnection.Packet;
|
||||
import soapConnection.SoapCalls;
|
||||
@@ -21,35 +34,49 @@ public class SocketManager {
|
||||
|
||||
private static final String TAG = "SOCKET_MANAGER";
|
||||
|
||||
private static DatabaseHandler databaseHandler = DatabaseHandler.getInstance(BeWoChatApplication.getInstance());
|
||||
|
||||
public static String tenant;
|
||||
public static Socket socket;
|
||||
public static Thread listenerThread;
|
||||
public static SocketListener socketListener;
|
||||
public static UIHandler uiHandler;
|
||||
public static LoginCallback loginCallback;
|
||||
public static DataOutputStream dataOutputStream;
|
||||
|
||||
public static ArrayList<Integer> onlinePersonOids = new ArrayList<>();
|
||||
public static ArrayList<ChatEntity> onlineEntities = new ArrayList<>();
|
||||
public static HashMap<ChatEntity, NotificationLog> notificationEntities = new HashMap<>();
|
||||
|
||||
public static void connectToServer(){
|
||||
new Thread(new OpenConnection()).start();
|
||||
new Thread(new ConnectionOpener()).start();
|
||||
}
|
||||
|
||||
public static void sendMessage(Packet packet) {
|
||||
new Thread(new MessageSender(packet.getDataStream())).start();
|
||||
}
|
||||
|
||||
public static class OpenConnection implements Runnable {
|
||||
private static class ConnectionOpener implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
if(socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
socket = null;
|
||||
socket = new Socket(SoapCalls.DESTINATION_ADDRESS, SoapCalls.DESTINATION_PORT);
|
||||
|
||||
loginOnServer();
|
||||
dataOutputStream = null;
|
||||
onlinePersonOids.clear();
|
||||
onlineEntities.clear();
|
||||
|
||||
dataOutputStream = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
socketListener = new SocketListener();
|
||||
listenerThread = new Thread(socketListener);
|
||||
listenerThread.start();
|
||||
|
||||
loginOnServer();
|
||||
} catch(Exception exception) {
|
||||
socket = null;
|
||||
try {
|
||||
@@ -68,17 +95,6 @@ public class SocketManager {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -97,9 +113,9 @@ public class SocketManager {
|
||||
public static void loginOnServer() {
|
||||
if(socket != null && socket.isConnected()) {
|
||||
try {
|
||||
Packet packet = new Packet();
|
||||
packet.Tenant = tenant;
|
||||
|
||||
Packet packet = new Packet();
|
||||
packet.Tenant = tenant;
|
||||
packet.ChatMessage = getMACAddress();
|
||||
packet.ChatDataIdentifier = DataIdentifier.LOGIN;
|
||||
|
||||
new Thread(new MessageSender(packet.getDataStream())).start();
|
||||
@@ -109,7 +125,7 @@ public class SocketManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConnectionClose implements Runnable{
|
||||
public static class ConnectionCloser implements Runnable{
|
||||
public void run(){
|
||||
try {
|
||||
if (dataOutputStream != null) {
|
||||
@@ -121,7 +137,7 @@ public class SocketManager {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
}catch(IOException se){
|
||||
} catch (IOException se) {
|
||||
se.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -136,7 +152,7 @@ public class SocketManager {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if(!socket.isConnected()) {
|
||||
if(socket == null || !socket.isConnected()) {
|
||||
Log.e("MESSAGE_SENDER", "Socket ist nicht verbunden");
|
||||
}
|
||||
|
||||
@@ -145,6 +161,21 @@ public class SocketManager {
|
||||
dataOutputStream.write(packetToSend);
|
||||
dataOutputStream.flush();
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
Packet packet = new Packet(new String(packetToSend, "UTF-8"));
|
||||
|
||||
if(packet.ChatDataIdentifier == DataIdentifier.MESSAGE || packet.ChatDataIdentifier == DataIdentifier.TEAM) {
|
||||
DatabaseHandler db = DatabaseHandler.getInstance(BeWoChatApplication.getInstance().getApplicationContext());
|
||||
ChatMessage undeliveredMessage = db.getChatMessageById(packet.ClientseitigeOid);
|
||||
if(undeliveredMessage != null) {
|
||||
undeliveredMessage.IsDelivered = false;
|
||||
int affectedRows = db.updateChatMessage(undeliveredMessage);
|
||||
}
|
||||
}
|
||||
} catch (UnsupportedEncodingException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -182,9 +213,63 @@ public class SocketManager {
|
||||
|
||||
try {
|
||||
chatPacket = new Packet(new String(message, "UTF-8"));
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Log.i(TAG, "Empfange Nachricht... " + chatPacket.ChatMessage);
|
||||
if(chatPacket.ChatDataIdentifier.equals(DataIdentifier.LOGIN_SUCCESS) && loginCallback != null) {
|
||||
Log.i(TAG, "Der Login war erfolgreich");
|
||||
loginCallback.doCallback();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
switch (chatPacket.ChatDataIdentifier) {
|
||||
case MESSAGE:
|
||||
|
||||
databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, false));
|
||||
|
||||
break;
|
||||
|
||||
case TEAM:
|
||||
|
||||
databaseHandler.addChatMessage(new ChatMessage(chatPacket.SenderPersonOid, chatPacket.RecipientPersonOid, chatPacket.MessageTimeStamp, chatPacket.ChatMessage, true, chatPacket.ServerseitigeOid, true));
|
||||
|
||||
break;
|
||||
|
||||
case MESSAGE_RESPONSE:
|
||||
ChatMessage cm = databaseHandler.getChatMessageById(chatPacket.ClientseitigeOid);
|
||||
|
||||
cm.ServerseitigeOid = chatPacket.ServerseitigeOid;
|
||||
cm.IsDelivered = true;
|
||||
|
||||
databaseHandler.updateChatMessage(cm);
|
||||
break;
|
||||
case STATUS_NOTIFICATION_LOGIN:
|
||||
|
||||
if(!SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) {
|
||||
SocketManager.onlinePersonOids.add(chatPacket.SenderPersonOid);
|
||||
}
|
||||
|
||||
break;
|
||||
case STATUS_NOTIFICATION_LOGOUT:
|
||||
|
||||
if(SocketManager.onlinePersonOids.contains(chatPacket.SenderPersonOid)) {
|
||||
SocketManager.onlinePersonOids.remove(Integer.valueOf(chatPacket.SenderPersonOid));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case STATUS_NOTIFICATION_LOGIN_BROADCAST:
|
||||
|
||||
String[] onlinePersons = chatPacket.ChatMessage.split(",");
|
||||
|
||||
for (String onlinePerson : onlinePersons) {
|
||||
SocketManager.onlinePersonOids.add(Integer.parseInt(onlinePerson));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -202,4 +287,36 @@ public class SocketManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMACAddress() {
|
||||
try {
|
||||
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
|
||||
for(NetworkInterface nif : all) {
|
||||
if(!nif.getName().equalsIgnoreCase("wlan0")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] macBytes = nif.getHardwareAddress();
|
||||
if(macBytes == null) {
|
||||
return "MAC-ADRESSE NICHT ERMITTELBAR";
|
||||
}
|
||||
|
||||
StringBuilder res1 = new StringBuilder();
|
||||
for(byte b : macBytes) {
|
||||
res1.append(Integer.toHexString(b & 0xFF) + ":");
|
||||
}
|
||||
|
||||
if(res1.length() > 0) {
|
||||
res1.deleteCharAt(res1.length() - 1);
|
||||
}
|
||||
|
||||
return res1.toString();
|
||||
}
|
||||
} catch(Exception ex) {
|
||||
Log.e("GET_MAC_ADDRESS", ex.getMessage());
|
||||
}
|
||||
|
||||
return "02:00:00:00:00:00";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user