Files
ErgometerDoctorApplication/ErgometerDoctorApplication/MainClient.cs
T
2015-10-20 11:19:08 +02:00

271 lines
8.6 KiB
C#

using ErgometerLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ErgometerDoctorApplication
{
class MainClient
{
public static TcpClient Server { get; set; }
public static bool loggedin;
private static Thread t;
private static bool running;
public static int Session;
public static string HOST = "127.0.0.1";
public static int PORT = 8888;
//Server information
public static List<ClientThread> clients;
public static Dictionary<string, string> users;
public static List<int> oldsessions;
public static void RemoveActiveClient(ClientThread clientThread)
{
clients.Remove(clientThread);
}
public static Dictionary<int, string> activesessions;
static MainClient()
{
Server = new TcpClient();
loggedin = false;
clients = new List<ClientThread>();
users = new Dictionary<string, string>();
oldsessions = new List<int>();
activesessions = new Dictionary<int, string>();
}
public static bool Connect(string password, out string error)
{
error = "Succes";
if (Server == null || !Server.Connected)
{
if (Server == null)
Server = new TcpClient();
try
{
Server.Connect(HOST, PORT);
}
catch (Exception e)
{
error = "Server is niet online.";
return false;
}
NetCommand net = NetHelper.ReadNetCommand(Server);
if (net.Type == NetCommand.CommandType.SESSION)
Session = net.Session;
else
throw new Exception("Session not assigned");
running = true;
t = new Thread(run);
t.IsBackground = true;
t.Start();
}
if (!loggedin)
{
NetCommand command = new NetCommand("Doctor0tVfW", true, password, Session);
NetHelper.SendNetCommand(Server, command);
NetCommand response = NetHelper.ReadNetCommand(Server);
if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
{
loggedin = false;
error = "Het wachtwoord is onjuist.";
return false;
}
loggedin = true;
}
SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, Session));
SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session));
return true;
}
public static void Disconnect()
{
if (Server.Connected)
{
NetHelper.SendNetCommand(Server, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
loggedin = false;
running = false;
Server.Close();
Server = null;
}
}
public static void run()
{
while (running)
{
if (loggedin && Server.Connected && Server.Available > 0)
{
NetCommand command = NetHelper.ReadNetCommand(Server);
Console.WriteLine(command.Type.ToString() + " # " + command);
HandleNetCommand(command);
}
}
Server.Close();
}
private static bool UsersBeingSent = false;
private static int UsersSent = 0;
private static int UsersLength = 0;
private static bool SessionsBeingSent = false;
private static int SessionsSent = 0;
private static int SessionsLength = 0;
private static bool ActiveSessionsBeingSent = false;
private static int ActiveSessionsSent = 0;
private static int ActiveSessionsLength = 0;
private static void HandleNetCommand(NetCommand command)
{
switch (command.Type)
{
case NetCommand.CommandType.LENGTH:
switch (command.Length)
{
case NetCommand.LengthType.USERS:
users.Clear();
UsersBeingSent = true;
UsersSent = 0;
UsersLength = command.LengthValue;
break;
case NetCommand.LengthType.SESSIONS:
oldsessions.Clear();
SessionsBeingSent = true;
SessionsSent = 0;
SessionsLength = command.LengthValue;
break;
case NetCommand.LengthType.SESSIONDATA:
activesessions.Clear();
ActiveSessionsBeingSent = true;
ActiveSessionsSent = 0;
ActiveSessionsLength = command.LengthValue;
break;
default:
throw new FormatException("Error in NetCommand: Length type is not recognised");
}
break;
case NetCommand.CommandType.ERROR:
Console.WriteLine("An error occured, ignoring");
break;
case NetCommand.CommandType.USER:
if(UsersBeingSent)
{
users.Add(command.DisplayName, command.Password);
UsersSent++;
if (UsersSent >= UsersLength)
UsersBeingSent = false;
}
break;
case NetCommand.CommandType.SESSION:
if (SessionsBeingSent)
{
oldsessions.Add(command.Session);
SessionsSent++;
if (SessionsSent >= SessionsLength)
SessionsBeingSent = false;
}
break;
case NetCommand.CommandType.SESSIONDATA:
if (ActiveSessionsBeingSent)
{
activesessions.Add(command.Session, command.DisplayName);
ActiveSessionsSent++;
if (ActiveSessionsSent >= ActiveSessionsLength)
ActiveSessionsBeingSent = false;
}
break;
default:
HandToClient(command);
break;
}
}
private static void HandToClient(NetCommand command)
{
foreach (ClientThread cl in clients)
{
if (cl.Session == command.Session)
{
cl.HandleCommand(command);
}
}
}
public static void SendNetCommand(NetCommand command)
{
NetHelper.SendNetCommand(Server, command);
}
private static bool IsSessionRunning(int session)
{
foreach (ClientThread cl in clients)
{
if (cl.Session == session)
return true;
}
return false;
}
public static void StartNewCLient(string name, int session)
{
if (IsSessionRunning(session))
return;
//Start new client
ClientThread cl = new ClientThread(name, session, false);
clients.Add(cl);
//Run client on new thread
Thread thread = new Thread(new ThreadStart(cl.run));
thread.IsBackground = true;
thread.Start();
}
public static void StartOldCLient(string name, int session)
{
if (IsSessionRunning(session))
return;
SendNetCommand(new NetCommand(NetCommand.RequestType.OLDDATA, session));
//Start new client
ClientThread cl = new ClientThread(name, session, true);
clients.Add(cl);
//Run client on new thread
Thread thread = new Thread(new ThreadStart(cl.run));
thread.IsBackground = true;
thread.Start();
}
}
}