Beispiel #1
0
        internal async virtual void LogEventOccurred(LogEvent logEvent)
        {
            using (var dbContext = new CoreComContext(_dbContextOptions))
            {
                //Events
                switch (_coreComOptions.LogSettings.LogEventTarget)
                {
                case LogEventTargetEnum.Database:
                    await dbContext.LogEvents.AddAsync(logEvent);

                    break;

                case LogEventTargetEnum.TextFile:
                    await WriteEventLogtoFile(logEvent);

                    break;

                case LogEventTargetEnum.NoLoging:

                    break;

                default:
                    break;
                }

                await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
            EventHandler <LogEvent> handler = OnLogEventOccurred;

            if (handler != null)
            {
                handler(this, logEvent);
            }
        }
Beispiel #2
0
        private async Task ParseServerToClientMessage(CoreComMessageResponse request)
        {
            using (var dbContext = new CoreComContext(_dbContextOptions))
            {
                request.TransferStatus = (int)TransferStatusEnum.Recived;
                request.RecivedUtc     = Helpers.DateTimeConverter.DateTimeUtcNowToUnixTime();

                if (request.MessageSignature == CoreComInternalSignatures.CoreComInternal_PullQueue)
                {
                    await ParseCoreComFrameworkFromServerMessage(request).ConfigureAwait(false);

                    LogEventOccurred(dbContext, request);
                    return;
                }



                LogEventOccurred(dbContext, request);
                //System.Type type = Type.GetType(request.JsonObjectType);
                // erer = new CoreComMessagingCenter();
                Type type = CoreComMessagingCenter.GetMessageArgType(request.MessageSignature);
                if (type == null)
                {
                    CoreComMessagingCenter.Send(request.MessageSignature, null);
                }
                else
                {
                    var objectDeser = JsonSerializer.Deserialize(request.JsonObject, type);
                    CoreComMessagingCenter.Send(request.MessageSignature, null, objectDeser);
                }
                //CoreComUserInfo coreComUserInfo = new CoreComUserInfo { ClientId = request.ClientId };
                //if (string.IsNullOrEmpty(request.JsonObject))
                //{
                //    var funcToRun = _receiveDelegatesOneParm.FirstOrDefault(x => x.Item2 == request.MessageSignature);
                //    if (funcToRun != null)
                //    {
                //        await funcToRun.Item1.Invoke(coreComUserInfo).ConfigureAwait(false);
                //    }
                //    else
                //    {
                //        LogErrorOccurred("No function mapped to " + request.MessageSignature, request);
                //    }
                //}
                //else
                //{
                //    var funcToRun = _receiveDelegatesTwoParm.FirstOrDefault(x => x.Item2 == request.MessageSignature);
                //    if (funcToRun != null)
                //    {
                //        var objectDeser = JsonSerializer.Deserialize(request.JsonObject, funcToRun.Item3);
                //        await funcToRun.Item1.Invoke(objectDeser, coreComUserInfo).ConfigureAwait(false);
                //    }
                //    else
                //    {
                //        LogErrorOccurred("No function mapped to " + request.MessageSignature, request);
                //    }
                //}
            }
        }
Beispiel #3
0
        internal async virtual void LogErrorOccurred(string description, CoreComMessageResponse coreComMessage, Exception ex = null)
        {
            LogError logError = new LogError {
                Description = description
            };

            if (coreComMessage != null)
            {
                logError.ClientId = coreComMessage.ClientId;
                logError.TransactionIdentifier = coreComMessage.TransactionIdentifier;
                if (string.IsNullOrEmpty(description))
                {
                    logError.Description = coreComMessage.MessageSignature;
                }
            }
            if (ex != null)
            {
                logError.Stacktrace = ex.StackTrace;
            }

            using (var dbContext = new CoreComContext(_dbContextOptions))
            {
                //Messages
                switch (_coreComOptions.LogSettings.LogErrorTarget)
                {
                case LogErrorTargetEnum.Database:
                    dbContext.LogErros.Add(logError);
                    break;

                case LogErrorTargetEnum.TextFile:
                    //Create textfile log
                    // await WriteErrorLogtoFile(logError).ConfigureAwait(false);
                    break;

                case LogErrorTargetEnum.NoLoging:
                    //if (coreComMessage.TransferStatus != (int)TransferStatusEnum.New)
                    //    dbContext.IncomingMessages.Remove(coreComMessage);
                    break;

                default:
                    break;
                }



                await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
            EventHandler <LogError> handler = OnLogErrorOccurred;

            if (handler != null)
            {
                handler(this, logError);
            }
        }
Beispiel #4
0
        private async void _checkQueueTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _checkQueueTimer.Enabled = false;
            using (var dbContext = new CoreComContext(_dbContextOptions))
            {
                var outgoingMess = dbContext.OutgoingMessages.
                                   Where(x => x.TransferStatus < (int)TransferStatusEnum.Transferred).ToList();

                if (outgoingMess.Count == 0)
                {
                    await SendInternalAsync(null, CoreComInternalSignatures.CoreComInternal_PullQueue, false, false);
                }
                else
                {
                    await ProcessQueue().ConfigureAwait(false);
                }
            }
        }
Beispiel #5
0
        private async Task <bool> SendInternalAsync(object outgoingObject, string messageSignature, bool sendAuth, bool noDeadline)
        {
            string         jsonObject = string.Empty;
            CoreComMessage coreComMessage;

            try
            {
                //Turn of timmer for message queue as we get the queue from this call
                _checkQueueTimer.Enabled = false;
                //error report to client
                if (outgoingObject != null)
                {
                    jsonObject = JsonSerializer.Serialize(outgoingObject);
                }

                coreComMessage = new CoreComMessage
                {
                    CoreComMessageId      = Guid.NewGuid().ToString(),
                    TransactionIdentifier = Guid.NewGuid().ToString(),
                    NewUtc           = Helpers.DateTimeConverter.DateTimeUtcNowToUnixTime(),
                    MessageSignature = messageSignature,
                    JsonObject       = jsonObject,
                    SendAuth         = sendAuth
                };

                using (var dbContext = new CoreComContext(_dbContextOptions))
                {
                    dbContext.OutgoingMessages.Add(coreComMessage);
                    await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }


                await ProcessQueue(noDeadline).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                LogErrorOccurred(ex, new CoreComMessageResponse {
                    ClientId = _coreComOptions.ClientId
                });
            }
            return(true);
        }
Beispiel #6
0
        private async void RemoveHistory()
        {
            try
            {
                await Task.Delay(10000);

                if (_coreComOptions.DatabaseMode == DatabaseModeEnum.UseInMemory)
                {
                    return;
                }
                //remove
                using (var dbContext = new CoreComContext(_dbContextOptions))
                {
                    //Messages tables
                    var messagesDate = Helpers.DateTimeConverter.DateTimeUtcNowToUnixTime(DateTime.Now.AddDays(_coreComOptions.LogSettings.LogMessageHistoryDays * -1).ToUniversalTime());
                    await dbContext.DeleteRangeAsync <CoreComMessage>(b => b.RecivedUtc < messagesDate);

                    await dbContext.DeleteRangeAsync <CoreComMessageResponse>(b => b.RecivedUtc < messagesDate);

                    //LogEvent Table
                    var eventDate = DateTime.Now.AddDays(_coreComOptions.LogSettings.LogEventHistoryDays * -1).ToUniversalTime();
                    await dbContext.DeleteRangeAsync <LogEvent>(b => b.TimeStampUtc < eventDate);

                    //LogError Table
                    var errorDate = DateTime.Now.AddDays(_coreComOptions.LogSettings.LogErrorHistoryDays * -1).ToUniversalTime();
                    await dbContext.DeleteRangeAsync <LogError>(b => b.TimeStampUtc < errorDate);
                }
            }
            catch (Exception ex)
            {
                LogErrorOccurred(ex, new CoreComMessageResponse
                {
                    ClientId = _coreComOptions.ClientId
                });
            }
        }
Beispiel #7
0
        internal async virtual void LogEventOccurred(CoreComContext dbContext, CoreComMessageResponse coreComMessageResponse)
        {
            LogEvent logEvent = new LogEvent {
                Description = coreComMessageResponse.MessageSignature, TransferStatus = (TransferStatusEnum)coreComMessageResponse.TransferStatus, MessageSize = coreComMessageResponse.CalculateSize()
            };


            //Messages
            switch (_coreComOptions.LogSettings.LogMessageTarget)
            {
            case LogMessageTargetEnum.Database:
                //allways remove CoreComInternal from incomming table/ the massage is new so it does not exist in table
                if (coreComMessageResponse.MessageSignature != CoreComInternalSignatures.CoreComInternal_PullQueue)
                {     //add incomming message to db
                    coreComMessageResponse.CoreComMessageResponseId = Guid.NewGuid().ToString();
                    dbContext.IncomingMessages.Add(coreComMessageResponse);
                }
                break;

            case LogMessageTargetEnum.TextFile:
                if (coreComMessageResponse.MessageSignature != CoreComInternalSignatures.CoreComInternal_PullQueue)
                {     //add incomming message to file
                    await WriteIncommingMessagesLog(coreComMessageResponse);
                }
                break;

            case LogMessageTargetEnum.NoLoging:
                //dbContext.OutgoingMessages.Remove(coreComMessage);
                break;

            default:
                break;
            }

            //Events
            switch (_coreComOptions.LogSettings.LogEventTarget)
            {
            case LogEventTargetEnum.Database:
                if (coreComMessageResponse.TransferStatus != (int)TransferStatusEnum.New)
                {
                    await dbContext.LogEvents.AddAsync(logEvent);
                }

                break;

            case LogEventTargetEnum.TextFile:
                await WriteEventLogtoFile(logEvent);

                break;

            case LogEventTargetEnum.NoLoging:

                break;

            default:
                break;
            }
            await dbContext.SaveChangesAsync().ConfigureAwait(false);

            EventHandler <LogEvent> handler = OnLogEventOccurred;

            if (handler != null)
            {
                handler(this, logEvent);
            }
        }
Beispiel #8
0
        internal async virtual void LogEventOccurred(CoreComContext dbContext, CoreComMessage coreComMessage)
        {
            LogEvent logEvent = new LogEvent {
                Description = coreComMessage.MessageSignature, TransferStatus = (TransferStatusEnum)coreComMessage.TransferStatus, MessageSize = coreComMessage.CalculateSize()
            };


            //Messages
            switch (_coreComOptions.LogSettings.LogMessageTarget)
            {
            case LogMessageTargetEnum.Database:
                //allways remove CoreComInternal from outgoingmessage table
                if (coreComMessage.MessageSignature == CoreComInternalSignatures.CoreComInternal_PullQueue &&
                    coreComMessage.TransferStatus != (int)TransferStatusEnum.New)
                {
                    dbContext.OutgoingMessages.Remove(coreComMessage);
                }

                //it's allready in db just update status

                break;

            case LogMessageTargetEnum.TextFile:

                await WriteOutgoingMessagesLog(coreComMessage);

                break;

            case LogMessageTargetEnum.NoLoging:
                if (coreComMessage.TransferStatus != (int)TransferStatusEnum.New)
                {
                    dbContext.OutgoingMessages.Remove(coreComMessage);
                }
                break;

            default:
                break;
            }

            //Events
            switch (_coreComOptions.LogSettings.LogEventTarget)
            {
            case LogEventTargetEnum.Database:
                await dbContext.LogEvents.AddAsync(logEvent);

                break;

            case LogEventTargetEnum.TextFile:

                await WriteEventLogtoFile(logEvent);

                break;

            case LogEventTargetEnum.NoLoging:

                break;

            default:
                break;
            }

            await dbContext.SaveChangesAsync().ConfigureAwait(false);


            EventHandler <LogEvent> handler = OnLogEventOccurred;

            if (handler != null)
            {
                handler(this, logEvent);
            }
        }
Beispiel #9
0
        private async Task <bool> ProcessQueue(bool noDeadline = false)
        {
            if (_workingOnQueue || _connectionStatus != ConnectionStatusEnum.Connected)
            {
                return(true);
            }

            try
            {
                _workingOnQueue = true;
                using (var dbContext = new CoreComContext(_dbContextOptions))
                {
                    var outgoingMess = dbContext.OutgoingMessages.
                                       Where(x => x.TransferStatus < (int)TransferStatusEnum.Transferred).ToList();


                    foreach (var item in outgoingMess)
                    {
                        if (item.SendAuth)
                        {
                            item.ClientId = _coreComOptions.ClientId.ToString();
                            LogEventOccurred(dbContext, item);
                            using var streamingCall = _coreComClient.SubscribeServerToClientAuth(item, GetCallOptions(false, item.SendAuth, noDeadline));

                            //Now the outgoing messages is sent
                            await foreach (var returnMessage in streamingCall.ResponseStream.ReadAllAsync().ConfigureAwait(false))
                            {
                                await ParseServerToClientMessage(returnMessage);
                            }
                            if (item.TransferStatus == (int)TransferStatusEnum.New)
                            {
                                item.TransferStatus = (int)TransferStatusEnum.Transferred;
                                item.TransferredUtc = Helpers.DateTimeConverter.DateTimeUtcNowToUnixTime();
                                LogEventOccurred(dbContext, item);
                            }
                            streamingCall.Dispose();
                        }
                        else
                        {
                            item.ClientId = _coreComOptions.ClientId.ToString();
                            LogEventOccurred(dbContext, item);
                            using var streamingCall = _coreComClient.SubscribeServerToClient(item, GetCallOptions(false, item.SendAuth, noDeadline));

                            //Now the outgoing messages is sent
                            await foreach (var returnMessage in streamingCall.ResponseStream.ReadAllAsync().ConfigureAwait(false))
                            {
                                await ParseServerToClientMessage(returnMessage);
                            }
                            if (item.TransferStatus == (int)TransferStatusEnum.New)
                            {
                                item.TransferStatus = (int)TransferStatusEnum.Transferred;
                                item.TransferredUtc = Helpers.DateTimeConverter.DateTimeUtcNowToUnixTime();
                                LogEventOccurred(dbContext, item);
                            }
                            streamingCall.Dispose();
                        }
                    }
                }
            }
            catch (RpcException ex)
            {
                _workingOnQueue = false;

                LatestRpcExceptionChange(ex);
                switch (ex.StatusCode)
                {
                case StatusCode.DeadlineExceeded:
                    LogEventOccurred(new LogEvent {
                        Description = ex.Message, ConnectionStatus = _connectionStatus
                    });
                    _coreComOptions.GrpcOptions.MessageDeadlineSecMultiplay = _coreComOptions.GrpcOptions.MessageDeadlineSecMultiplay * 2;
                    await ProcessQueue().ConfigureAwait(false);

                    return(false);

                case StatusCode.Cancelled:
                    LogEventOccurred(new LogEvent {
                        Description = ex.Message, ConnectionStatus = _connectionStatus
                    });
                    Console.WriteLine("Stream cancelled.");
                    break;

                case StatusCode.PermissionDenied:
                case StatusCode.Unavailable:
                    ConnectionStatusChange(ConnectionStatusEnum.Disconnected);
                    LogEventOccurred(new LogEvent {
                        Description = ex.Message, ConnectionStatus = _connectionStatus
                    });
                    Console.WriteLine("PermissionDenied/Unavailable");
                    if (!_timer.Enabled)
                    {
                        _timer.Enabled = true;
                    }
                    break;

                case StatusCode.Unauthenticated:
                    Console.WriteLine("Unauthenticated.");
                    LogEventOccurred(new LogEvent {
                        Description = ex.Message, ConnectionStatus = _connectionStatus
                    });
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                LogErrorOccurred(ex, new CoreComMessageResponse {
                    ClientId = _coreComOptions.ClientId
                });
                _workingOnQueue = false;
            }

            _workingOnQueue = false;
            //Start timmer for check Queue server and client
            if (_coreComOptions.GrpcOptions.RequestServerQueueIntervalSec > 0)
            {
                _checkQueueTimer.Enabled = true;
            }

            return(true);
        }