Merge branch 'ConnectionController' of https://sourcecode.socialcoding.bosch.com/scm/~hpl2fe/polychat into ConnectionController

This commit is contained in:
Patrick Hellebrand
2021-09-22 16:21:53 +02:00
3 changed files with 45 additions and 13 deletions

View File

@@ -39,7 +39,7 @@ namespace PolyChat
public void Connect(string ip)
{
Debug.WriteLine("--- Controller.Connect ---");
Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data)));
Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat));
}
private void Serve()
@@ -61,7 +61,7 @@ namespace PolyChat
Debug.WriteLine("--- initial packet received ---");
string ForeignIp = data[0].ToString();
//Todo deserialize inital packet and extract ip address
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data)));
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat));
UIController.OnIncomingConnection(ForeignIp);
});
});
@@ -91,6 +91,15 @@ namespace PolyChat
else Debug.WriteLine("Undefined: " + data);
}
public void CloseChat(string IP, bool wasConnected = true)
{
Connections[IP].Close();
Connections.Remove(IP);
UIController.OnChatPartnerDeleted(IP);
if(!wasConnected)
UIController.ShowConnectionError(IP, $"Connection to {IP} failed...");
}
public string getIP()
{
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());

View File

@@ -36,15 +36,22 @@ namespace PolyChat
updateSendButtonEnabled();
}
public async void ShowConnectionError(string message)
public async void ShowConnectionError(string code, string message)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ConnectionFailedDialog dialog = new ConnectionFailedDialog(message);
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
//retry
Controller.Connect(code);
Partners.Add(new ChatPartner(
"Connecting...",
code
));
updateNoChatsPlaceholder();
}
// else abort -> del chat
});
}
// EVENTS
@@ -113,10 +120,22 @@ namespace PolyChat
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
{
Controller.CloseChat(selectedPartner.Code);
Partners.Remove(selectedPartner);
updateNoChatsPlaceholder();
updateNoChatSelected();
}
public async void OnChatPartnerDeleted(string code)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Partners.Remove(Partners.First(p => p.Code == code));
selectedPartner = null;
updateNoChatsPlaceholder();
updateNoChatSelected();
});
}
public void OnChatPartnerSelected(object sender, RoutedEventArgs e)
{
string code = ((RadioButton)sender).Tag.ToString();

View File

@@ -15,11 +15,13 @@ namespace PolyChat.Models
private SocketIOSocket Socket;
private bool Connected = false;
private readonly string IP;
private Action<string, bool> DeleteConnection;
public Connection(string ip, ushort port, Action<JToken[]> onMessage)
public Connection(string ip, ushort port, Action<JToken[]> onMessage, Action<string, bool> onClose)
{
Debug.WriteLine("! CONNECTING TO SERVER !");
IP = ip;
DeleteConnection = onClose;
// establish connection
Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port));
Client.Connect();
@@ -30,9 +32,10 @@ namespace PolyChat.Models
Client.On("message", (Action<JToken[]>) onMessage);
}
public Connection(SocketIOSocket socket, Action<JToken[]> onMessage)
public Connection(SocketIOSocket socket, Action<JToken[]> onMessage, Action<string, bool> onClose)
{
Socket = socket;
DeleteConnection = onClose;
Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect);
Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data));
Socket.On("message", (Action<JToken[]>) onMessage);
@@ -62,8 +65,9 @@ namespace PolyChat.Models
private void OnDisconnect()
{
Debug.WriteLine("--- Disconnected! ---");
Debug.WriteLine($"--- Deleting Connection with IP: {IP}---");
DeleteConnection(IP, IsConnected());
Connected = false;
Close();
}
private void OnError(JToken[] data)
{