Ejemplo n.º 1
0
        public Common.ServiceStatus SetMoney(List<Efectivo> val)
        {
            Common.ServiceStatus response = new Common.ServiceStatus();
            response.error.HasError = true;
            response.error.Message = "Error recibido del PaymentDevice";
            ComClass clase = new ComClass();
            clase.funciones = ComClass.function.set_money;
            clase.Inventario = val;
            string Test = JsonConvert.SerializeObject(clase);
            using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
            {
                socket.Send(Test); // Sends some data
                var data = socket.Receive(); // Receives some data back (blocks execution)
                ComClass deserializedProduct = JsonConvert.DeserializeObject<ComClass>(data);
                if(deserializedProduct.result== ComClass.status_cash.ok)
                {
                    response.IsDone = true;
                    response.error.HasError = false;
                    return response;
                }
            }


            return response;




        }
Ejemplo n.º 2
0
        public void Start(string configPath)
        {
            AddLog("Starting a node...", LogType.Information);

            try
            {
                Config = NetworkNodeConfig.ParseConfig(configPath);
            }
            catch (Exception e)
            {
                AddLog($"Exception: {e.Message}", LogType.Error);
                return;
            }

            networkNodeRoutingTables.LoadTablesFromConfig(Config, Window);
            networkNodeRoutingTables.StartManagementAgent();

            ConnectToCloud();

            while (true)
            {
                while (ConnectedSocket == null || !ConnectedSocket.Connected)
                {
                    AddLog("Retrying connection to cable cloud...", LogType.Information);
                    ConnectToCloud();
                }

                try
                {
                    var package = ConnectedSocket.Receive();

                    AddLog($"Received package: {package} at port {package.Port}", LogType.Received);

                    Task.Run(() => HandlePackage(package));
                }
                catch (InvalidMPLSPackageException)
                {
                    AddLog("Received package was not a valid package.", LogType.Error);
                }
                catch (SocketException e)
                {
                    // ignore timeout exceptions
                    if (e.SocketErrorCode != SocketError.TimedOut)
                    {
                        if (e.SocketErrorCode == SocketError.Shutdown || e.SocketErrorCode == SocketError.ConnectionReset)
                        {
                            AddLog("Connection to Cloud broken!", LogType.Error);
                            continue;
                        }

                        else
                        {
                            AddLog($"{e.Source}: {e.SocketErrorCode}", LogType.Error);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void HandlePaymentCash(int quantity)
        {
            ComClass clase = new ComClass();
            clase.funciones = ComClass.function.cash_handling;
            clase.Value = quantity;
            string Test = JsonConvert.SerializeObject(clase);
            using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
            {
                socket.Send(Test); // Sends some data
                var data = socket.Receive(); // Receives some data back (blocks execution)
            }

        }
Ejemplo n.º 4
0
        private void button1_Click(object sender, EventArgs e)
        {
            ComClass clase = new ComClass();

            clase.funciones = ComClass.function.cash_handling;
            clase.Value     = 4500;
            string Test = JsonConvert.SerializeObject(clase);

            using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
            {
                socket.Send(Test);                                      // Sends some data
                var data = socket.Receive();                            // Receives some data back (blocks execution)
            }
        }
Ejemplo n.º 5
0
        public void Start()
        {
            while (true)
            {
                AddLog("Management agent starting...", LogType.Information);

                ConnectToMS();

                if (ConnectedSocket == null)
                {
                    // connection failed
                    // start over
                    continue;
                }

                while (true)
                {
                    try
                    {
                        byte[] buffer = new byte[512];
                        int    bytes  = ConnectedSocket.Receive(buffer);

                        var message = Encoding.ASCII.GetString(buffer, 0, bytes);

                        Task.Run(() => HandleMessage(message));
                    }
                    catch (SocketException e)
                    {
                        // ignore timeout exceptions
                        if (e.SocketErrorCode != SocketError.TimedOut)
                        {
                            if (e.SocketErrorCode == SocketError.Shutdown || e.SocketErrorCode == SocketError.ConnectionReset)
                            {
                                AddLog("Connection to MS broken!", LogType.Error);
                                break;
                            }

                            else
                            {
                                AddLog($"{e.Source}: {e.SocketErrorCode}", LogType.Error);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
		public string StartClient(string whatToSend)
		{
			// Connect to a remote device.
			try
			{
				// Establish the remote endpoint for the socket.
				IPAddress ipAddress = IPAddress.Parse(_serverIp);

				//IPAddress ipAddress = IPAddress.Parse("2.109.10.231");
				IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

				// Create a TCP/IP  socket.
				var sender = new ConnectedSocket(remoteEP); 

				// Connect the socket to the remote endpoint. Catch any errors.
				try
				{
					// Send the data through the socket.
					sender.Send(whatToSend);

					// Receive the response from the remote device.
					var received = "";
					do {
						received += sender.Receive(1024);
						Thread.Sleep(20);
					}
					while (sender.AnythingToReceive);

					Console.WriteLine(received);
					return received;

				}
				catch (Exception)
				{
					return JsonConvert.SerializeObject(new LoginResponseMsg("", false) { MessageInfo = "Error - Server did not respond\nMake sure server is started and Emil isn't nearby" }, _jsonSettings);
				}

			}
			catch (Exception)
			{
				return JsonConvert.SerializeObject(new LoginResponseMsg("", false) {MessageInfo = "Error - Server did not respond\nMake sure server is started and Emil isn't nearby"}, _jsonSettings);
			}
		}
Ejemplo n.º 7
0
        public List<Efectivo> GetInventario()
        {
            
                ComClass clase = new ComClass();
                clase.funciones = ComClass.function.get_money;

                string Test = JsonConvert.SerializeObject(clase);
                using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
                {
                    socket.Send(Test); // Sends some data
                    var data = socket.Receive(); // Receives some data back (blocks execution)
                    ComClass deserializedProduct = JsonConvert.DeserializeObject<ComClass>(data);
                    return deserializedProduct.Inventario;
                }


               

          

        }
Ejemplo n.º 8
0
        public List<Common.ServiceStatus> GetDevicesStatus()
        {

            try
            {
                ComClass clase = new ComClass();
                clase.funciones = ComClass.function.get_devices_status;

                string Test = JsonConvert.SerializeObject(clase);
                using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
                {
                    socket.Send(Test); // Sends some data
                    var data = socket.Receive(); // Receives some data back (blocks execution)
                    ComClass deserializedProduct = JsonConvert.DeserializeObject<ComClass>(data);
                    Helpers.Utilities.WriteLocalLog("[GetDevicesStatus] Devices loaded ok");
                    return deserializedProduct.DeviceStatus;
                }
            }catch(Exception e)
            {
                Helpers.Utilities.WriteLocalLog("[GetDevicesStatus] " + e.Message);
            }
            return null;   
        }
Ejemplo n.º 9
0
        public Common.ServiceStatus GetServiceStatus(ComClass.function function)
        {

            Common.ServiceStatus response = new Common.ServiceStatus();

            try
            {
                ComClass clase = new ComClass();
                clase.funciones = function;
                string Test = JsonConvert.SerializeObject(clase);
                using (var socket = new ConnectedSocket("127.0.0.1", 1337)) // Connects to 127.0.0.1 on port 1337
                {
                    socket.Send(Test); // Sends some data
                    var data = socket.Receive(); // Receives some data back (blocks execution)
                    ComClass deserializedProduct = JsonConvert.DeserializeObject<ComClass>(data);
                    if (function == ComClass.function.system_status)
                    {
                        if (deserializedProduct.status == ComClass.machine_status.idle)
                        {
                            response.IsDone = true;
                            response.error.HasError = false;
                            return response;
                        }
                        else
                        {
                            response.IsDone = false;
                            response.error.HasError = true;
                            response.error.Message = deserializedProduct.status.ToString();
                        }
                    }
                    else if (function == ComClass.function.operation_status)
                    {

                        if (deserializedProduct.result == ComClass.status_cash.ok)
                        {
                            response.IsDone = true;
                            response.error.HasError = false;

                            return response;
                        }
                        else if (deserializedProduct.result == ComClass.status_cash.time_out)
                        {
                            response.IsDone = false;
                            response.error.HasError = true;
                            response.error.Message = ComClass.status_cash.time_out.ToString();
                            return response;

                        }
                        else if (deserializedProduct.result == ComClass.status_cash.operation_error)
                        {
                            response.IsDone = false;
                            response.error.HasError = true;
                            response.error.Message = ComClass.status_cash.time_out.ToString();
                            return response;

                        }
                        else if (deserializedProduct.result == ComClass.status_cash.waiting)
                        {
                            response.IsDone = false;
                            response.error.HasError = false;
                            response.error.Message = "";
                            return response;

                        }
                    }

                }
            }catch (Exception e)
            {
                response.error.HasError = true;
                response.error.Message = e.Message;
                response.IsDone = true;
            }
                           
             return response;     

        }
Ejemplo n.º 10
0
        public void Receive()
        {
            if (ConnectedSocket != null)
            {
                while (true)
                {
                    try
                    {
                        byte[] head = new byte[9];
                        ConnectedSocket.Receive(head, head.Length, SocketFlags.None);

                        int len = BitConverter.ToInt32(head, 1);
                        if (head[0] == (byte)ChatType.Str)
                        {
                            byte[] buffer = new byte[len];
                            ConnectedSocket.Receive(buffer, len, SocketFlags.None);
                            OnReceive(ChatType.Str, UTF8.GetString(buffer));
                        }
                        else if (head[0] == (byte)ChatType.File)
                        {
                            if (!Directory.Exists(dirName))
                            {
                                Directory.CreateDirectory(dirName);
                            }

                            byte[] nameLen = new byte[4];
                            ConnectedSocket.Receive(nameLen, nameLen.Length, SocketFlags.None);

                            byte[] name = new byte[BitConverter.ToInt32(nameLen, 0)];
                            ConnectedSocket.Receive(name, name.Length, SocketFlags.None);

                            string fileName = UTF8.GetString(name);

                            int    readByte = 0;
                            int    count    = 0;
                            byte[] buffer   = new byte[1024 * 8];

                            string filePath = Path.Combine(dirName, fileName);
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                            }
                            using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                            {
                                while (count != len)
                                {
                                    int readLength = buffer.Length;
                                    if (len - count < readLength)
                                    {
                                        readLength = len - count;
                                    }
                                    readByte = ConnectedSocket.Receive(buffer, readLength, SocketFlags.None);
                                    fs.Write(buffer, 0, readByte);
                                    count += readByte;
                                }
                            }
                            OnReceive(ChatType.File, fileName);
                        }
                        else
                        {
                            // 未知类型
                        }
                    }
                    catch (Exception e)
                    {
                        OnDisconnect(e);
                        // 连接异常断开
                        if (AutoReConnect)
                        {
                            this.socket.Close();
                            this.socket = null;
                            this.Start();
                        }
                        break;
                    }
                }
            }
        }