Esempio n. 1
0
        private async Task PeerConnectionError(PeerConnection peer, Exception e)
        {
            _feedback.Error(nameof(BroadcastAsync), $"peer: {peer.Client.Host.AbsoluteUri}, message: {e.Message}");

            //TODO: Improve temporary errors by introducing some statistics
            await peer.DisconnectAsync();

            _peers.TryRemove(peer.Client.Host.AbsoluteUri, out var _);             //remove dead client
        }
        public SudokuConfiguration ParseConfiguration(string filePath)
        {
            if (!File.Exists(filePath))
            {
                feedback.Error($"File [{filePath}] not found!");
                return(null);
            }

            var values = ParseValues(filePath);

            if (values == null)
            {
                return(null);
            }

            var board = ConvertValuesIntoBoard(values);

            return(new SudokuConfiguration
            {
                InitialValues = board
            });
        }
Esempio n. 3
0
 public static void Execute(this IFeedback feedback, string operationName, Action operation, Func <string> logParameters = null)
 {
     try
     {
         feedback.Start(operationName, logParameters?.Invoke());
         var sw = Stopwatch.StartNew();
         operation();
         feedback.Stop(operationName, $" /{sw.ElapsedMilliseconds}ms");
     }
     catch (Exception e)
     {
         feedback.Error(operationName, e.Message);
     }
 }
Esempio n. 4
0
        private async Task BlockchainSync(Action syncCompletedAction)
        {
            try
            {
                await _network.BlockchainSync(_chainData);

                syncCompletedAction();
            }
            catch (Exception e)
            {
                _feedback.Error(nameof(BlockchainSync), e.Message);
            }
            _blockchainSync = null;
        }
Esempio n. 5
0
 public static T Execute <T>(this IFeedback feedback, string operationName, Func <T> operation, Func <string> logParameters = null)
 {
     try
     {
         feedback.Start(operationName, logParameters?.Invoke());
         var sw     = Stopwatch.StartNew();
         var result = operation();
         feedback.Stop(operationName, $"=> {result} /{sw.ElapsedMilliseconds}ms");
         return(result);
     }
     catch (Exception e)
     {
         feedback.Error(operationName, e.Message);
         throw;
     }
 }
        private void GetClients()
        {
            try
            {
                if (_clients == null || File.GetLastWriteTime(_file) != _lastwrite)
                {
                    string raw = File.ReadAllText(_file);

                    _clients = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <Guid, string> >(raw);

                    _lastwrite = File.GetLastWriteTime(_file);
                }
            }
            catch (Exception ex)
            {
                _feedback.Error(ex, true);
                _clients = _clients ?? new Dictionary <Guid, string>();
            }
        }
Esempio n. 7
0
        private bool ProcessRequest(ServerClient from, string raw)
        {
            _msgcnt++;

            try
            {
                BaseTransmission request = Newtonsoft.Json.JsonConvert.DeserializeObject <BaseTransmission>(raw);

                if (request.SessionId != from.SessionId)
                {
                    return(false);
                }

                ActionEnum action = (ActionEnum)request.Action;

                if (action == ActionEnum.Ping || action == ActionEnum.Pong)
                {
                    return(true);
                }

                ServerClient target = request.TargetServerId == Guid.Empty ? null : GetClientFromClientId(request.TargetServerId);

                if (action == ActionEnum.SendMessageToTarget)
                {
                    _feedback.WriteLine("SendMessageToTarget: {0} -> {1}", from, raw);

                    target?.Send(Newtonsoft.Json.JsonConvert.SerializeObject(new BaseTransmission
                    {
                        Action         = (int)ActionEnum.SendMessageToTarget,
                        Data           = request.Data,
                        SessionId      = target.SessionId,
                        Parameter      = request.Parameter,
                        TargetServerId = from.ServerId
                    }));
                }
                else if (action == ActionEnum.WebRequest)
                {
                    _feedback.WriteLine("WebRequest: {0} -> {1}", from, raw);

                    Dtos.WebRequest webRequest;

                    lock (_webrequests)
                    {
                        webRequest = _webrequests
                                     .Where(i => i.Response == null && i.Request.TargetServerId == request.TargetServerId)
                                     .OrderByDescending(i => i.Age)
                                     .FirstOrDefault();
                    }

                    if (webRequest != null)
                    {
                        webRequest.SetResponse(request);
                    }
                }
                else if (action == ActionEnum.GetAllConnectedClients)
                {
                    _feedback.WriteLine("GetAllConnectedClients: {0} -> {1}", from, raw);

                    Dictionary <string, object> clients = new Dictionary <string, object>();

                    foreach (var item in _clients.Where(i => i.Value.State == ClientState.Ok).Select(i => i.Value))
                    {
                        clients.Add(item.ServerId.ToString(), _validator.GetClientName(item.ServerId));
                    }

                    from.Send(Newtonsoft.Json.JsonConvert.SerializeObject(new BaseTransmission
                    {
                        Action         = (int)ActionEnum.GetAllConnectedClients,
                        Data           = clients,
                        SessionId      = from.SessionId,
                        Parameter      = request.Parameter,
                        TargetServerId = Guid.Empty
                    }));
                }
                else
                {
                    _feedback.WriteLine("Unknown request: {0} from {1}", action, from);
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                _feedback.Error(ex, false);
                return(false);
            }
        }