コード例 #1
0
        private void MoveTexi(object sender, RoutedEventArgs e)
        {
            if (this.texiHighlight.Stroke != null)
            {
                try
                {
                    int moveFromRow = Grid.GetRow(this.texiHighlight);
                    int moveFromCol = Grid.GetColumn(this.texiHighlight);
                    int moveToRow   = int.Parse(this.moveToRow.Text);
                    int moveToCol   = int.Parse(this.moveToCol.Text);

                    Location destination = new Location(moveToRow, moveToCol);
                    Texi     texi        = this.center.Layout[moveFromRow][moveFromCol].Texi;

                    this.texiHighlight.Stroke = null;
                    texi.Move(destination);
                }
                catch (ArgumentNullException)
                {
                    this.PrintTexiMessage("No coordinates.");
                }
                catch (FormatException)
                {
                    this.PrintTexiMessage($"Coordinates number range is between 1 and {this.center.Size.Col}.");
                }
                catch (OverflowException)
                {
                    this.PrintTexiMessage("Coordinates number range is between 1 and {this.center.Size.Col}.");
                }
            }
            else
            {
                this.PrintTexiMessage("No texi was selected.");
            }
        }
コード例 #2
0
        private void UpdateClient(Socket recSoc, Employee employee, Texi texi, BinaryFormatter bf)
        {
            // Convert employee and texi to serializable objects.
            SerializableEmployee serEmp  = employee.ToSerializable();
            SerializableTexi     serTexi = texi.ToSerializable();

            // Serialize and send the employee object.
            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, serEmp);
                recSoc.Send(ms.ToArray());
            }

            // Serialize and send the texi object.
            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, serTexi);
                recSoc.Send(ms.ToArray());
            }

            // If the employee has reached his detination remove thred from globalTimer.
            if (employee.Location == employee.Destination)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, Encoding.ASCII.GetBytes("Destination reached."));
                    recSoc.Send(ms.ToArray());
                }

                this.globalTimer.Tick -= (o, arg) => this.UpdateClient(recSoc, employee, texi, bf);
            }
        }
コード例 #3
0
        private void SelectTexi(object sender, SelectedCellsChangedEventArgs e)
        {
            if (this.texiList.SelectedItem != null)
            {
                // Get the texi as an object from the list.
                Texi texi = this.texiList.SelectedItem as Texi;
                int  row  = texi.Row;
                int  col  = texi.Col;

                // Color the texiHighlight black.
                if (this.texiHighlight.Stroke == null)
                {
                    this.texiHighlight.Stroke = new SolidColorBrush(Colors.Black);
                }

                // Move the texiHighlight to the selected texi.
                Grid.SetRow(this.texiHighlight, row);
                Grid.SetColumn(this.texiHighlight, col);

                this.center.SelectedTexis = this.texiList.SelectedItems;
            }
        }
コード例 #4
0
        private void ClientConnectionWorker(object sender, DoWorkEventArgs e)
        {
            Socket          recSoc   = (Socket)e.Argument; // Connection socket.
            BinaryFormatter bf       = new BinaryFormatter();
            Employee        employee = null;               // Thread employee.

            // Log
            Application.Current.Dispatcher.Invoke(() => this.icLog.Items.Add(new { message = "+ Client connection received..." }));

            // Keep listen to data from client.
            while (true)
            {
                byte[] data = new byte[8192];
                int    len;

                try
                {
                    len = recSoc.Receive(data);  // Receive client data.
                    Array.Resize(ref data, len); // Resize data array from 8192 to the datas' real size.

                    // Dserialize the received object.
                    var deserializedData = this.DeserializeClientData(data, bf, employee);

                    if (deserializedData.GetType() == typeof(string))
                    {
                        // Send layout size.
                        if ((string)deserializedData == "Send layout size please.")
                        {
                            using (MemoryStream sizeStrem = new MemoryStream())
                            {
                                // Serialize the size object.
                                bf.Serialize(sizeStrem, this.center.Size);
                                // Send the data.
                                recSoc.Send(sizeStrem.ToArray());

                                // Log
                                this.Log("+ Sending layout size to client.");
                            }
                        }
                    }
                    else
                    {
                        // Add an new employee.
                        if (deserializedData.GetType() == typeof(SerializableEmployee))
                        {
                            employee = new Employee((SerializableEmployee)deserializedData, this.center);
                            this.center.AddEmployee(employee);

                            this.dispatcher.Invoke(() => this.icLog.Items.Add(new { message = "+ Received employee information: Id " + employee.Id })); // Log
                        }
                        // Call a texi for the employee.
                        else if (deserializedData.GetType() == typeof(Location))
                        {
                            Texi texi = this.dispatcher.Invoke(() => this.center.CallTexi(employee, (Location)deserializedData));

                            if (texi != null)
                            {
                                this.globalTimer.Tick -= this.UpdateLayoutView;
                                this.globalTimer.Tick += (o, arg) => this.UpdateClient(recSoc, employee, texi, bf);
                                this.globalTimer.Tick += this.UpdateLayoutView;
                            }
                            else
                            {
                                // TODO
                            }
                        }
                    }
                }
                // Client data is null.
                catch (NullReferenceException)
                {
                    Application.Current.Dispatcher.Invoke(() => this.icLog.Items.Add(new { message = "- Employee " + employee.Id + "- Client data is null." })); // Log
                }
                // If the socket connection ended close the connection.
                catch (SocketException)
                {
                    this.center.RemoveEmployee(employee);                                                                                                    // Remove employee from employees list.
                    Application.Current.Dispatcher.Invoke(() => this.icLog.Items.Add(new { message = "- Employee " + employee.Id + " has disconnected." })); // Log
                    Thread.CurrentThread.Abort();                                                                                                            // Close thread.
                }
            }
        }