del Controller and ClientHandler / msg send to ui
This commit is contained in:
@@ -1,88 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Net;
|
|
||||||
using EngineIOSharp.Common.Enum;
|
|
||||||
using PolyChat.Models;
|
|
||||||
using PolyChat.Models.Exceptions;
|
|
||||||
using SocketIOSharp.Client;
|
|
||||||
using SocketIOSharp.Common;
|
|
||||||
using SocketIOSharp.Common.Packet;
|
|
||||||
using SocketIOSharp.Server.Client;
|
|
||||||
|
|
||||||
namespace PolyChat
|
|
||||||
{
|
|
||||||
public class Controller
|
|
||||||
{
|
|
||||||
public static string ip;
|
|
||||||
private readonly MainPage UIController;
|
|
||||||
|
|
||||||
private ClientHandler clientHandler;
|
|
||||||
/*
|
|
||||||
public Controller(MainPage uiController)
|
|
||||||
{
|
|
||||||
UIController = uiController;
|
|
||||||
clientHandler = new ClientHandler();
|
|
||||||
Socket s = new Socket(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Connect(string ip)
|
|
||||||
{
|
|
||||||
clientHandler.connectNewClient(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendMessage(String ip, String name, String msg)
|
|
||||||
{
|
|
||||||
clientHandler.getClient(ip).send Message(SendCode.Message, msg, DateTime.Now);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/// <summary>
|
|
||||||
/// prints out ip. on server side automatticaly finds 10.... ip (which is the correct one)
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>users ip</returns>
|
|
||||||
private String getIP()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
string input = Console.ReadLine();
|
|
||||||
if (input.Equals("server"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("starting as server...");
|
|
||||||
for (short i = 0; i < GetIPs().Length; i++)
|
|
||||||
{
|
|
||||||
if (GetIPs()[i].ToString().Substring(0, 3).Equals("10."))
|
|
||||||
{
|
|
||||||
Controller.ip = GetIPs()[i];
|
|
||||||
Console.WriteLine(GetIPs()[i]);
|
|
||||||
//get ip as byte array
|
|
||||||
byte[] ba = System.Text.Encoding.ASCII.GetBytes(GetIPs()[i].ToString());
|
|
||||||
foreach (var item in ba)
|
|
||||||
{
|
|
||||||
Console.Write(item.ToString() + ",");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (input.Equals("client"))
|
|
||||||
{
|
|
||||||
ClientHandler cl = new ClientHandler();
|
|
||||||
Console.WriteLine("Enter IP:");
|
|
||||||
cl.connectNewClient(Console.ReadLine());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static string[] GetIPs()
|
|
||||||
{
|
|
||||||
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
|
||||||
IPAddress[] addr = ipEntry.AddressList;
|
|
||||||
string[] ips = new string[addr.Length];
|
|
||||||
for (int i = 0; i < addr.Length; i++)
|
|
||||||
{
|
|
||||||
ips[i] = addr.ToString();
|
|
||||||
}
|
|
||||||
return ips;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,18 +16,18 @@ namespace PolyChat.Models
|
|||||||
private Boolean connected = true;
|
private Boolean connected = true;
|
||||||
private String ipSelf;
|
private String ipSelf;
|
||||||
|
|
||||||
public Client(SocketIOClient connection, String ip)
|
public Client(SocketIOClient connection, String ip, MainPage uiController)
|
||||||
{
|
{
|
||||||
this.ipSelf = ip;
|
this.ipSelf = ip;
|
||||||
this.connection_client = connection;
|
this.connection_client = connection;
|
||||||
InitEventHandlers(this, connection);
|
InitEventHandlers(this, connection, uiController);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Client(SocketIOSocket connection, String ip)
|
public Client(SocketIOSocket connection, String ip, MainPage uiController)
|
||||||
{
|
{
|
||||||
this.ipSelf = ip;
|
this.ipSelf = ip;
|
||||||
this.connection_server = connection;
|
this.connection_server = connection;
|
||||||
InitEventHandlers(this, connection);
|
InitEventHandlers(this, connection, uiController);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sending
|
//Sending
|
||||||
@@ -47,7 +47,7 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
Debug.WriteLine($"connected is {connected}");
|
Debug.WriteLine($"connected is {connected}");
|
||||||
//create msg
|
//create msg
|
||||||
Message msg = new Message(chatMessage, false, Controller.ip);
|
Message msg = new Message(chatMessage, false, ipSelf);
|
||||||
|
|
||||||
//convert msg
|
//convert msg
|
||||||
String petJson = JsonNet.Serialize(msg);
|
String petJson = JsonNet.Serialize(msg);
|
||||||
@@ -73,48 +73,30 @@ namespace PolyChat.Models
|
|||||||
}
|
}
|
||||||
}).Start();
|
}).Start();
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
/// <summary>
|
|
||||||
/// Sends Message with new name
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="code"></param>
|
|
||||||
/// <param name="nameChange"></param>
|
|
||||||
/// <param name="timestamp"></param>
|
|
||||||
public void sendNameChange(SendCode code, String nameChange)
|
|
||||||
{
|
|
||||||
new Thread(() =>
|
|
||||||
{
|
|
||||||
//create msg
|
|
||||||
Message msg = new Message( Controller.ip);
|
|
||||||
|
|
||||||
//convert msg
|
|
||||||
String petJson = JsonNet.Serialize(msg);
|
|
||||||
|
|
||||||
//send msg
|
|
||||||
connection_client.Emit(code.ToString(), petJson);
|
|
||||||
}).Start();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//==================================================================================
|
//==================================================================================
|
||||||
//EventHandeling
|
//EventHandeling
|
||||||
//===================================================================================
|
//===================================================================================
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// handles all events of client to server communiation
|
/// handles all events of client
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="client">self</param>
|
/// <param name="client">self</param>
|
||||||
/// <param name="connection"></param>
|
/// <param name="connection"></param>
|
||||||
private static void InitEventHandlers(Client client, SocketIOClient connection)
|
private static void InitEventHandlers(Client client, SocketIOClient connection, MainPage uiController)
|
||||||
{
|
{
|
||||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||||
{
|
{
|
||||||
Message pet = new Message(Data[0]);
|
Message msg = new Message(Data[0]);
|
||||||
|
uiController.OnIncomingMessage(msg);
|
||||||
|
|
||||||
//TODO: send message to GUI
|
//TODO: send message to GUI
|
||||||
});
|
});
|
||||||
|
|
||||||
connection.On(SendCode.Command.ToString(), (Data) =>
|
connection.On(SendCode.Command.ToString(), (Data) =>
|
||||||
{
|
{
|
||||||
Console.WriteLine("Command recieved!" + Data[0]);
|
Console.WriteLine("Command recieved!" + Data[0]);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
connection.On(SocketIOEvent.CONNECTION, () =>
|
connection.On(SocketIOEvent.CONNECTION, () =>
|
||||||
@@ -124,15 +106,16 @@ namespace PolyChat.Models
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// handles all events of server to client communiation
|
/// handles all events of server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="client">self</param>
|
/// <param name="client">self</param>
|
||||||
/// <param name="connection"></param>
|
/// <param name="connection"></param>
|
||||||
private static void InitEventHandlers(Client client, SocketIOSocket connection)
|
private static void InitEventHandlers(Client client, SocketIOSocket connection, MainPage uiController)
|
||||||
{
|
{
|
||||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||||
{
|
{
|
||||||
Message pet = new Message(Data[0]);
|
Message msg = new Message(Data[0]);
|
||||||
|
uiController.OnIncomingMessage(msg);
|
||||||
//TODO: send message to GUI
|
//TODO: send message to GUI
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using SocketIOSharp.Common;
|
|
||||||
using SocketIOSharp.Server;
|
|
||||||
using SocketIOSharp.Client;
|
|
||||||
using SocketIOSharp.Server.Client;
|
|
||||||
using EngineIOSharp.Common.Enum;
|
|
||||||
using Json.Net;
|
|
||||||
|
|
||||||
namespace PolyChat.Models
|
|
||||||
{
|
|
||||||
class ClientHandler
|
|
||||||
{
|
|
||||||
public List<Client> clients = new List<Client>();
|
|
||||||
public ClientHandler()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// connects new clients and saves them in list
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="clientCode">ip adress of parter</param>
|
|
||||||
public void connectNewClient(String clientCode)
|
|
||||||
{
|
|
||||||
//Todo: convert code into ip
|
|
||||||
|
|
||||||
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, clientCode, 8050));
|
|
||||||
connection.Connect();
|
|
||||||
clients.Add(new Client(connection, clientCode));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// returns client that fits to ip adress
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ip"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public Client getClient(String ip)
|
|
||||||
{
|
|
||||||
foreach (Client cl in clients)
|
|
||||||
{
|
|
||||||
if (cl.getIP().Equals(ip))
|
|
||||||
{
|
|
||||||
return cl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -27,11 +27,7 @@ namespace PolyChat.Models
|
|||||||
this.uiController = uiController;
|
this.uiController = uiController;
|
||||||
this.Port = Port;
|
this.Port = Port;
|
||||||
ownIP = getIP();
|
ownIP = getIP();
|
||||||
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
startServer();
|
||||||
Server.OnConnection((socket) => connectNewClient(socket));
|
|
||||||
Server.Start();
|
|
||||||
Debug.WriteLine($"Your ip is: {ownIP}");
|
|
||||||
Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//EXTERNAL METHODS
|
//EXTERNAL METHODS
|
||||||
@@ -45,7 +41,7 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050));
|
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050));
|
||||||
connection.Connect();
|
connection.Connect();
|
||||||
clients.Add(new Client(connection, ip));
|
clients.Add(new Client(connection, ip, uiController));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -56,8 +52,9 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
socket.On(SendCode.Initial.ToString(), (JToken[] Data) =>
|
socket.On(SendCode.Initial.ToString(), (JToken[] Data) =>
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("Client connected!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||||
Message m = new Message(Data[0]);
|
Message m = new Message(Data[0]);
|
||||||
clients.Add(new Client(socket,m.Ip));
|
clients.Add(new Client(socket,m.Ip, uiController));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,24 +87,23 @@ namespace PolyChat.Models
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
public MainPage getUIController()
|
||||||
/// <summary>
|
|
||||||
/// changes name of self and sends new name to all chats
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="newName"></param>
|
|
||||||
public void changeName(String newName)
|
|
||||||
{
|
{
|
||||||
this.ownName = newName;
|
return this.uiController;
|
||||||
foreach(Client cl in clients)
|
|
||||||
{
|
|
||||||
cl.sendNameChange(SendCode.NameChange, newName);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//=========================================================================================================================================================================================
|
//=========================================================================================================================================================================================
|
||||||
//INTERNAL METHODS
|
//INTERNAL METHODS
|
||||||
//=========================================================================================================================================================================================
|
//=========================================================================================================================================================================================
|
||||||
|
private void startServer()
|
||||||
|
{
|
||||||
|
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||||
|
Server.OnConnection((socket) => connectNewClient(socket));
|
||||||
|
Server.Start();
|
||||||
|
Debug.WriteLine($"Your ip is: {ownIP}");
|
||||||
|
Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// returns client that fit to ip address
|
/// returns client that fit to ip address
|
||||||
|
|||||||
@@ -127,10 +127,8 @@
|
|||||||
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
|
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
|
||||||
<Compile Include="Models\NetworkingController.cs" />
|
<Compile Include="Models\NetworkingController.cs" />
|
||||||
<Compile Include="Models\Client.cs" />
|
<Compile Include="Models\Client.cs" />
|
||||||
<Compile Include="Models\ClientHandler.cs" />
|
|
||||||
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
|
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
|
||||||
<Compile Include="Models\SendCode.cs" />
|
<Compile Include="Models\SendCode.cs" />
|
||||||
<Compile Include="Controller.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Util\IP.cs" />
|
<Compile Include="Util\IP.cs" />
|
||||||
<Compile Include="Views\ConnectionFailedDialog.xaml.cs">
|
<Compile Include="Views\ConnectionFailedDialog.xaml.cs">
|
||||||
|
|||||||
Reference in New Issue
Block a user