private static TcpResponse RegisterPodRequestHandler(TcpRequest request)
        {
            Console.WriteLine($"Request: {request.url}, {request.payload}");

            var pod = JsonConvert.DeserializeObject <RegisterPodRequest>(request.payload);

            if (_registeredPods == null)
            {
                _registeredPods = new Dictionary <string, RegisterPodRequest>();
            }

            if (_registeredPods.ContainsKey(pod.id))
            {
                return(new TcpResponse()
                {
                    code = (int)FmsStatusCode.PodIsAlreadyRegistered,
                    payload = "Pod is already registered"
                });
            }

            _registeredPods.Add(pod.id, pod);

            return(new TcpResponse()
            {
                code = (int)FmsStatusCode.Ok,
                payload = "Pod is registered"
            });
        }
Пример #2
0
        public void SendRequest(TcpRequest request, Action <TcpResponse> callback)
        {
            var requestJson = JsonConvert.SerializeObject(request);

            void OnResponse(string json)
            {
                var response = JsonConvert.DeserializeObject <TcpResponse>(json);

                callback?.Invoke(response);
            }

            var task = SendRequest(requestJson, OnResponse);
        }
        private static TcpResponse RequestPodRequestHandler(TcpRequest request)
        {
            Console.WriteLine($"Request: {request.url}, {request.payload}");

            if (_registeredPods == null || _registeredPods.Count == 0)
            {
                return new TcpResponse()
                       {
                           code    = (int)FmsStatusCode.NoPodAvailable,
                           payload = "No pod available"
                       }
            }
            ;

            return(new TcpResponse()
            {
                code = (int)FmsStatusCode.Ok,
                payload = "{" + $"'p2pUrl':{_registeredPods.First().Value.p2pUrl}" + "}"
            });
        }
        public static void Start()
        {
            OneAppTcpClient client = new OneAppTcpClient(CommunicationUtils.GetMyIpAddress(), 8080);

            var data = new
            {
                id      = "test",
                message = "hello from client"
            };

            var request = new TcpRequest()
            {
                url     = FmsUrl.RegisterPod,
                payload = JsonConvert.SerializeObject(data)
            };

            void OnResponse(TcpResponse response)
            {
                Console.WriteLine($"Response: {response.payload}");
            }

            client.SendRequest(request, OnResponse);
        }