<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using VectorClock.Commander.Helper;
using VectorClock.Common;

namespace VectorClock.Commander.ViewModel
{
    public class MainViewModel : PropertyChangedBase
    {
        private string title = "VectorClock";
        public string Title
        {
            get { return title; } 
            set { title = value; OnNotifyPropertyChanged(); }
        }

        private string textBoxContent = "";
        public string TextBoxContent
        {
            get { return textBoxContent; }
            set { textBoxContent = value; OnNotifyPropertyChanged(); }
        }


        public NodeViewModel Node1 { get; set; }
        public NodeViewModel Node2 { get; set; }
        public NodeViewModel Node3 { get; set; }

        //public NodeViewModel NodeLokal { get; set; }

        public MainViewModel()
        {
            //Node1 = new NodeViewModel("mjverteil01", System.Net.IPAddress.Parse("10.10.29.21"));
            //Node2 = new NodeViewModel("mjverteil02", System.Net.IPAddress.Parse("10.10.29.142"));
            //Node3 = new NodeViewModel("mjverteil03", System.Net.IPAddress.Parse("10.10.29.67"));

            // this is for one-machine testing only
            Node1 = new NodeViewModel("mjverteil01", new IPEndPoint(IPAddress.Loopback, 1337));
            Node2 = new NodeViewModel("mjverteil02", new IPEndPoint(IPAddress.Loopback, 1338));
            Node3 = new NodeViewModel("mjverteil03", new IPEndPoint(IPAddress.Loopback, 1339));

            //NodeLokal = new NodeViewModel("lokal", System.Net.IPAddress.Loopback);

            CheckNodeConnectivities(); //TODO: Timer

            Task listenToNodeTask = new Task(async () => await ListenToNode());
            listenToNodeTask.Start();
        }

        public async Task ListenToNode()
        {
            using (UdpClient client = new UdpClient(1340, AddressFamily.InterNetwork))
            {
                while (true)
                {
                    UdpReceiveResult result = await client.ReceiveAsync();
                               
                    Message msg = MessageDeserializer.Deserialize(result.Buffer);
                    TextBoxContent += $"Answer from: { result.RemoteEndPoint.Address}:{result.RemoteEndPoint.Port}. Message: {msg.controlBlock.Command} \n Balance: {msg.communicationBlock.payload.balance} Clock: {msg.communicationBlock.clock}\n" ;
                }
            }
        }

        public void CheckNodeConnectivities()
        {
            Task.Run(async () => await Node1.CheckConnectivity());
            Task.Run(async () => await Node2.CheckConnectivity());
            Task.Run(async () => await Node3.CheckConnectivity());
        }

        private RelayCommand testCommand;
        public RelayCommand TestCommand
        {
            get
            {
                if (testCommand == null)
                {
                    testCommand = new RelayCommand(async () =>
                    {
                        Message msg = new Message();
<commit_msg>Added local time to textbox output<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using VectorClock.Commander.Helper;
using VectorClock.Common;

namespace VectorClock.Commander.ViewModel
{
    public class MainViewModel : PropertyChangedBase
    {
        private string title = "VectorClock";
        public string Title
        {
            get { return title; } 
            set { title = value; OnNotifyPropertyChanged(); }
        }

        private string textBoxContent = "";
        public string TextBoxContent
        {
            get { return textBoxContent; }
            set { textBoxContent = value; OnNotifyPropertyChanged(); }
        }


        public NodeViewModel Node1 { get; set; }
        public NodeViewModel Node2 { get; set; }
        public NodeViewModel Node3 { get; set; }

        //public NodeViewModel NodeLokal { get; set; }

        public MainViewModel()
        {
            //Node1 = new NodeViewModel("mjverteil01", System.Net.IPAddress.Parse("10.10.29.21"));
            //Node2 = new NodeViewModel("mjverteil02", System.Net.IPAddress.Parse("10.10.29.142"));
            //Node3 = new NodeViewModel("mjverteil03", System.Net.IPAddress.Parse("10.10.29.67"));

            // this is for one-machine testing only
            Node1 = new NodeViewModel("mjverteil01", new IPEndPoint(IPAddress.Loopback, 1337));
            Node2 = new NodeViewModel("mjverteil02", new IPEndPoint(IPAddress.Loopback, 1338));
            Node3 = new NodeViewModel("mjverteil03", new IPEndPoint(IPAddress.Loopback, 1339));

            //NodeLokal = new NodeViewModel("lokal", System.Net.IPAddress.Loopback);

            CheckNodeConnectivities(); //TODO: Timer

            Task listenToNodeTask = new Task(async () => await ListenToNode());
            listenToNodeTask.Start();
        }

        public async Task ListenToNode()
        {
            using (UdpClient client = new UdpClient(1340, AddressFamily.InterNetwork))
            {
                while (true)
                {
                    UdpReceiveResult result = await client.ReceiveAsync();
                               
                    Message msg = MessageDeserializer.Deserialize(result.Buffer);

                    TextBoxContent += string.Format("{0:HH:mm:ss}: Answer from {1}. Message: {2}\n Balance: {3} Clock: {4}\n", 
                        DateTime.Now, 
                        result.RemoteEndPoint, 
                        msg.controlBlock.Command, 
                        msg.communicationBlock.payload.balance,
                        msg.communicationBlock.clock);
                }
            }
        }

        public void CheckNodeConnectivities()
        {
            Task.Run(async () => await Node1.CheckConnectivity());
            Task.Run(async () => await Node2.CheckConnectivity());
            Task.Run(async () => await Node3.CheckConnectivity());
        }

        private RelayCommand testCommand;
        public RelayCommand TestCommand
        {
            get
            {
                if (testCommand == null)
                {
                    testCommand = new RelayCommand(async () =>
                    {
                        Message msg = new Message();

{'fraction_non_alphanumeric': 0.10123017637468505, 'fraction_numerical': 0.020008892841262782, 'mean_word_length': 1.968763748350198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7313229', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1687, 'n_words': 390}