public ClientView(String _username, ClientDetails _clientDetails)
        {
            InitializeComponent();
            this.Text = _username;
            username = _username;
            clientDetails = _clientDetails;

            if (File.Exists(username + ".txt"))
            {
                displayInUserDisplay(File.ReadAllText(username + ".txt"));
            }
            else
            {
                File.Create(username + ".txt");
            }
            fileSystemWatcher1.Path = Path.GetDirectoryName(Application.ExecutablePath);
            refreshThread = new Thread(new ThreadStart(this.refresh));
            refreshThread.Start();
        }
 public void writeToClient(ClientDetails client, String serverResponse, bool display=true)
 {
     try
     {
         NetworkStream networkStream = client.clientSocket.GetStream();
         networkStream.Flush();
         serverResponse += Environment.NewLine;
         if (display)
         {
             displayInMainForm("[Writing] " + serverResponse + " To " + client.userName);
         }
         byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
         networkStream.Write(sendBytes, 0, sendBytes.Length);
         networkStream.Flush();
     }
     catch (Exception ex)
     {
         displayInMainForm(ex.Message + " Thrown while writing to client");
     }
 }
 public void removeClientFromList(ClientDetails client)
 {
     try
     {
         if (frm_Main.InvokeRequired)
         {
             frm_Main.Invoke((MethodInvoker)delegate
             {
                 dispatcher.removeClientFromList(client);
             });
         }
         else
         {
             dispatcher.removeClientFromList(client);
         }
     }
     catch (Exception)
     { }
 }
        public void main()
        {
            string _ClientID = "";

            try
            {
                //Oculus backward compatible
                _ClientID = readFromClient();
                ClientID = new ClientDetails(_ClientID, clientSocket);

                userName = ClientID.userName;
                password = ClientID.password;
                displayInMainForm("[AUTHENTICATION PHASE] Client with User ID: " + userName + " requesting authentication");

                if (!Authentication(userName, password))
                {
                    writeToClient("OculusMessage:Authentication Failed$$");
                    displayInMainForm("[AUTHENTICATION PHASE] Client with User ID: " + userName + " Failed authentication");
                    return;
                }
                createDatabase(userName);
                writeToClient("OculusMessage:Logged In$$");
                ClientID.label.Text = ClientID.userName;
                displayInMainForm("[AUTHENTICATION PHASE] Client with User ID: " + ClientID.userName + " Logged in");
                displayInMainForm("[AUTHENTICATION PHASE] Client with User ID: " + ClientID.userName + " Logged in", userName + ".txt");
                addNewUser(ClientID.label);
                ClientID.server = this;
                authenticated = true;
                dispatcher.addClientToList(ClientID);
            }
            catch (Exception e)
            {
                displayInMainForm("[AUTHENTICATION PHASE] " + userName + " Disconnected due to " + e.Message);
                displayInMainForm("[AUTHENTICATION PHASE] " + userName + " Disconnected due to " + e.Message, userName + ".txt");
                return;
            }

            //Amaranthine compatible only

            try
            {
                while (true)
                {
                    String rawData = readFromClient();
                    String[] splitData = rawData.Split(c.ToCharArray());
                    //displayInMainForm("[rawdata] "+rawData);
                    foreach (String partJSON in splitData)
                    {
                        if (partJSON.Trim() == String.Empty)
                            continue;

                        dynamic dataFromClient = JsonConvert.DeserializeObject(partJSON);
                        if (dataFromClient.Action != (int)Mnemonics.Actions.datafromothers && dataFromClient.Action != (int)Mnemonics.Actions.clientlist)
                        {
                            displayInMainForm(rawData);
                            displayInMainForm(rawData, userName + ".txt");
                        }
                        int actionMnemonic = dataFromClient.Action;

                        switch (actionMnemonic)
                        {
                            case (int)Mnemonics.Actions.clientlist:
                                {
                                    //clientlist
                                    getClientList();
                                }
                                break;
                            case (int)Mnemonics.Actions.heartBeat:
                                {
                                    //disconnection test
                                    writeToClient(Mnemonics.Actions.heartBeat.ToString());
                                }
                                break;
                            //datafromothers
                            case (int)Mnemonics.Actions.datafromothers:
                                {
                                    dataFromOthers();
                                }
                                break;

                            //senddatanow:<from>:<to>:<message>
                            case (int)Mnemonics.Actions.senddatanow:
                                {
                                    sendDataNow(dataFromClient);
                                }
                                break;

                            //senddata:<from>:<to>:<message>
                            case (int)Mnemonics.Actions.senddata:
                                {//Parallel .foreach https://msdn.microsoft.com/en-us/library/dd460720%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
                                    foreach (ClientDetails client in listOfClients)
                                    {
                                        if (client.userName == dataFromClient.TargetUsername)
                                        {
                                            client.messages.Add(new MessageContainer(dataFromClient.SourceUsername, dataFromClient.Message));
                                            logChat(userName, userName, client.userName, dataFromClient.Message);
                                            logChat(client.userName, userName, client.userName, dataFromClient.Message);
                                            //serverResponse += "OculusMessage:Message sent to client";
                                        }
                                    }
                                    //writeToClient(serverResponse);

                                }
                                break;
                            case (int)Mnemonics.Actions.deviceType:
                                {
                                    writeToClient(getDeviceType((String)dataFromClient.TargetUsername));
                                }
                                break;

                            case (int)Mnemonics.Actions.extract:
                                {
                                    insertIntoWarehouse(dataFromClient[1]);
                                    writeToClient("OculusMessage:Accepted");
                                }
                                break;
                            case (int)Mnemonics.Actions.SaveDiaryContent:
                                {
                                    writeToClient("OculusMessage:LengthReceived");
                                    saveDiaryContents(dataFromClient[1], Convert.ToInt32(dataFromClient[2]));
                                    writeToClient("OculusMessage:Accepted");
                                }
                                break;
                            default:
                                {
                                    displayInMainForm("Unknown mnemonic " + dataFromClient[0] + " from client " + ClientID.userName);
                                }
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                displayInMainForm(userName + " Disconnected due to " + ex.Message);
                displayInMainForm(userName + " Disconnected due to " + ex.StackTrace, userName + ".txt");
                clientSocket.Close();
                dispatcher.removeClientFromList(ClientID);
                removeUser(ClientID.label);
            }
        }
 public void removeClientFromList(ClientDetails ClientID)
 {
     listOfClients.Remove(ClientID);
     updateClientListToAll();
 }
 public void addClientToList(ClientDetails ClientID)
 {
     listOfClients.Add(ClientID);
     updateClientListToAll();
 }