OrdersInformationUpdateResponseMessage Receive(GetOrdersInformationMessage message)
        {
            TracerHelper.TraceEntry();

            List <OrderInfo> result = new List <OrderInfo>();

            lock (this)
            {
                foreach (string id in message.OrderTickets)
                {
                    if (_orders.ContainsKey(id) && _orders[id].HasValue)
                    {
                        result.Add(_orders[id].Value);
                    }
                    else
                    {
                        _pendingOrdersInformations.Add(id);
                    }
                }
            }

            TracerHelper.TraceExit();

            OrdersInformationUpdateResponseMessage response = new OrdersInformationUpdateResponseMessage(_accountInfo, result.ToArray(), true);

            if (message.RequestResponse)
            {
                return(response);
            }

            SendToSubscribers(response);
            return(null);
        }
        /// <summary>
        /// OperationId is not mandatory - but is should be there when the update was requested by a special recepient.
        /// </summary>
        public void TradingValuesUpdate(string symbol, int operationId, double time, int period, int availableBarsCount,
                                        Int64[] times, decimal[] opens, decimal[] closes, decimal[] highs, decimal[] lows, decimal[] volumes)
        {
            TracerHelper.TraceEntry();

            CombinedDataSubscriptionInformation session = GetDataSession(symbol);

            if (session == null)
            {
                SystemMonitor.Error("Failed to find symbol session [" + symbol + "], quotes not sent.");
                return;
            }

            try
            {
                // History update.
                TimeSpan periodValue = TimeSpan.FromMinutes(period);

                DataHistoryUpdate update = new DataHistoryUpdate(periodValue,
                                                                 GenerateDataBars(periodValue, times, opens, closes, highs, lows, volumes));

                update.AvailableHistorySize = availableBarsCount;

                DataHistoryUpdateMessage message = new DataHistoryUpdateMessage(session.SessionInformation.Info, update, true);

                SendToDataSubscribers(session, null, message);
            }
            catch (Exception ex)
            {// Make sure we handle any possible unexpected exceptions, as otherwise they bring the
                // entire package (MT4 included) down with a bad error.
                SystemMonitor.Error(ex.Message);
            }

            TracerHelper.TraceExit();
        }
        /// <summary>
        ///
        /// </summary>
        public bool ClearTable <ItemType>()
            where ItemType : class, IDBPersistent
        {
            TracerHelper.TraceEntry(typeof(ItemType).Name);

            string tableName = _tablesTypeNames[typeof(ItemType)];

            _clearMutex.WaitOne();
            try
            {
                using (SQLiteConnection connection = GenerateConnection())
                {
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        connection.Open();

                        command.CommandText = "DELETE FROM " + tableName;
                        int result = command.ExecuteNonQuery();
                    }
                }
            }
            finally
            {
                _clearMutex.ReleaseMutex();
            }

            TracerHelper.TraceExit();
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        void SendToSubscriber(TransportMessage message)
        {
            TracerHelper.TraceEntry(message.GetType().Name);

            lock (this)
            {
                if (_subscriberTransportMessageInfo != null)
                {
                    this.SendResponding(_subscriberTransportMessageInfo, message);
                }
            }

            TracerHelper.TraceExit();
        }
        /// <summary>
        /// Needs to be on the same connection the insert was in.
        /// </summary>
        /// <returns></returns>
        long GetLastInsertRowId(SQLiteConnection connection, string tableName)
        {
            TracerHelper.TraceEntry(tableName);

            long value;

            using (SQLiteCommand command = new SQLiteCommand(connection))
            {
                command.CommandText = "SELECT last_insert_rowid() FROM " + tableName;
                value = (long)command.ExecuteScalar();
            }

            TracerHelper.TraceExit();
            return(value);
        }
        /// <summary>
        ///
        /// </summary>
        public long Count <ItemType>(MatchExpression matchExpression)
            where ItemType : class, IDBPersistent
        {
            TracerHelper.TraceEntry(typeof(ItemType).Name);

            string tableName = _tablesTypeNames[typeof(ItemType)];

            StringBuilder commandText = new StringBuilder();

            commandText.Append("SELECT count(*) FROM " + tableName);

            if (matchExpression != null && matchExpression.ClauseCount > 0)
            {
                commandText.Append(" WHERE ");
            }

            _countMutex.WaitOne();
            SQLiteCommand command;
            long          result = 0;

            try
            {
                using (SQLiteConnection connection = GenerateConnection())
                {
                    using (command = new SQLiteCommand(connection))
                    {
                        if (matchExpression != null)
                        {
                            matchExpression.SetupCommandParameters(command, commandText);
                        }

                        command.CommandText = commandText.ToString();

                        connection.Open();
                        result = (long)command.ExecuteScalar();
                    }
                }
            }
            finally
            {
                _countMutex.ReleaseMutex();
            }

            TracerHelper.TraceExit();
            return(result);
        }
Esempio n. 7
0
        /// <summary>
        /// Set operationContextSessionID to "*" to mark all clients.
        /// </summary>
        /// <param name="operationContextSessionID"></param>
        /// <param name="messageContainer"></param>
        public void SendMessageContainer(string operationContextSessionID, MessageContainer messageContainer)
        {
            TracerHelper.TraceEntry("[" + messageContainer.MessageStreamLength + "]");
            if (messageContainer.MessageStreamLength > MaximumAllowedMessageSize)
            {
                TracerHelper.TraceError("Message too bid. Operation failed.");
                return;
                //throw new Exception("Arbiter::MessageContainerTransportServer::MessageContainer requestMessage is too big. Message will not be sent.");
            }

            Dictionary <IMessageContainerTransport, OperationContext> clientsContexts;

            lock (_contextManager)
            {
                clientsContexts = new Dictionary <IMessageContainerTransport, OperationContext>(_contextManager.ClientsContextsUnsafe);
            }

            List <IMessageContainerTransport> badInterfaces = new List <IMessageContainerTransport>();

            foreach (IMessageContainerTransport clientCallback in clientsContexts.Keys)
            {
                // "*" indicates send to all clients
                if (operationContextSessionID == "*" ||
                    clientsContexts[clientCallback].SessionId == operationContextSessionID)
                {
                    try
                    {
                        TracerHelper.Trace("clientCallback [" + operationContextSessionID + "]");
                        clientCallback.ReceiveMessageContainer(messageContainer);
                        TracerHelper.Trace("clientCallback [" + operationContextSessionID + "] invoked");
                    }
                    catch (Exception ex)
                    {
                        TracerHelper.TraceError("Failed to invoke client interface [" + ex.ToString() + "]");
                        badInterfaces.Add(clientCallback);
                    }
                }
            }

            ProcessBadInterfaces(badInterfaces);

            TracerHelper.TraceExit();
        }
        /// <summary>
        ///
        /// </summary>
        public bool Delete <ItemType>(IEnumerable <ItemType> items)
            where ItemType : class, IDBPersistent
        {
            TracerHelper.TraceEntry(typeof(ItemType).Name);
            string tableName = _tablesTypeNames[typeof(ItemType)];

            _deleteMutex.WaitOne();
            try
            {
                using (SQLiteConnection connection = GenerateConnection())
                {
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        connection.Open();

                        using (SQLiteTransaction transaction = connection.BeginTransaction())
                        {
                            SQLiteParameter whereIdParam = new SQLiteParameter("@ParamId");
                            command.Parameters.Add(whereIdParam);

                            command.CommandText = "DELETE FROM " + tableName + " WHERE Id = " + whereIdParam.ParameterName;

                            foreach (IDBPersistent persistent in items)
                            {
                                SystemMonitor.CheckThrow(persistent.Id.HasValue, "Deleting an item with no ID already assigned.");

                                whereIdParam.Value = persistent.Id;
                                int result = command.ExecuteNonQuery();
                            }

                            transaction.Commit();
                        }
                    }
                }
            }
            finally
            {
                _deleteMutex.ReleaseMutex();
            }

            TracerHelper.TraceExit();
            return(true);
        }
Esempio n. 9
0
        public void VerifyClientConnections(object param)
        {
            Dictionary <IMessageContainerTransport, OperationContext> clientsContexts;

            lock (_contextManager)
            {
                clientsContexts = new Dictionary <IMessageContainerTransport, OperationContext>(_contextManager.ClientsContextsUnsafe);
            }

            List <IMessageContainerTransport> badInterfaces = new List <IMessageContainerTransport>();

            foreach (IMessageContainerTransport clientCallback in clientsContexts.Keys)
            {
                try
                {
                    if (clientsContexts[clientCallback].Channel.State == CommunicationState.Opened)
                    {
                        clientCallback.Ping();
                    }
                    else
                    {
                        if (clientsContexts[clientCallback].Channel.State == CommunicationState.Faulted ||
                            clientsContexts[clientCallback].Channel.State == CommunicationState.Closed ||
                            clientsContexts[clientCallback].Channel.State == CommunicationState.Closing)
                        {
                            TracerHelper.Trace("Bad interface established, enlisting for removal [" + clientsContexts[clientCallback].Channel.SessionId + "].");
                            badInterfaces.Add(clientCallback);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TracerHelper.Trace("Bad interface established [" + ex.Message + "], enlisting for removal.");
                    badInterfaces.Add(clientCallback);
                }
            }

            ProcessBadInterfaces(badInterfaces);

            TracerHelper.TraceExit();
        }
Esempio n. 10
0
        protected override bool OnInitialize(Platform platform)
        {
            TracerHelper.TraceEntry();

            if (base.OnInitialize(platform) == false)
            {
                return(false);
            }

            string expertName = this.Name + ".expert";

            // Clean expertname since we might be sending it trough command line.
            expertName = expertName.Replace(" ", "_");
            expertName = expertName.Replace("\"", "");

            lock (this)
            {
                if (_expert == null)
                {// Create the expert.
                    SystemMonitor.CheckThrow(_expertType.IsSubclassOf(typeof(Expert)), "Invalid expert type passed in.");
                    ConstructorInfo constructor = _expertType.GetConstructor(new Type[] { typeof(ISourceAndExpertSessionManager), typeof(string) });

                    if (constructor == null)
                    {// Try the second option for construction.
                        constructor = _expertType.GetConstructor(new Type[] { typeof(ISourceAndExpertSessionManager) });
                    }

                    if (constructor == null)
                    {
                        SystemMonitor.Error("Failed to find corresponding constructor for expert type [" + _expertType.ToString() + "].");
                        return(false);
                    }

                    if (constructor.GetParameters().Length == 2)
                    {
                        _expert = (Expert)constructor.Invoke(new object[] { this, expertName });
                    }
                    else
                    {
                        _expert = (Expert)constructor.Invoke(new object[] { this });
                    }
                }

                if (_expert.Initialize() == false)
                {
                    SystemMonitor.Error("Expert host failed to connect to platform.");
                    return(false);
                }

                if (_expert != null)
                {
                    _expert.PersistenceDataUpdateEvent += new Expert.ExpertUpdateDelegate(_expert_PersistenceDataUpdateEvent);
                }
            }

            foreach (PlatformExpertSession session in SessionsArray)
            {
                if (session.Initialize(null) == false)
                {
                    SystemMonitor.OperationWarning("Failed to initialize session.");
                }
            }

            ChangeOperationalState(OperationalStateEnum.Operational);

            TracerHelper.TraceExit();
            return(true);
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        void worker_DoWork(ExecutionEntity entity)
        {
            lock (this)
            {
                if (_clientsRunningExecutioners.ContainsKey(entity.ReceiverID) == false)
                {
                    _clientsRunningExecutioners[entity.ReceiverID] = 1;
                }
                else
                {
                    _clientsRunningExecutioners[entity.ReceiverID] = _clientsRunningExecutioners[entity.ReceiverID] + 1;
                }
            }

            TracerHelper.Trace(" Enlisted entity at [" + entity.ReceiverID.Id.Name + "]");

            DateTime startTime = DateTime.Now;

            TracerHelper.TraceEntry("entity starting [" + entity.Message.GetType().Name + ", " +
                                    entity.ReceiverID.Id.Name + "], total count [" + _threadPool.ActiveRunningThreadsCount.ToString() + "]");

            // Notify executor we are running this entity.
            entity.Conversation.EntityExecutionStarted(entity);

            try
            {
                IArbiterClient receiver = _arbiter.GetClientByID(entity.ReceiverID);
                if (receiver != null)
                {
                    SystemMonitor.CheckError(((TransportMessage)entity.Message).TransportInfo.TransportInfoCount > 0);

                    // Do the entity.
                    if (entity is ExecutionEntityWithReply)
                    {
                        ExecutionEntityWithReply replyEntity = (ExecutionEntityWithReply)entity;
                        SystemMonitor.CheckError(replyEntity.ReplyMessage == null && replyEntity.ReplyTimeOut == TimeSpan.Zero);
                        receiver.ReceiveExecutionWithReply(replyEntity);
                    }
                    else
                    {
                        SystemMonitor.CheckError(entity.GetType() == typeof(ExecutionEntity));
                        receiver.ReceiveExecution(entity);
                    }
                    entity.Conversation.EntityExecutionFinished(entity);
                }
            }
            catch (TargetInvocationException exception)
            {
                if (exception.InnerException is ThreadInterruptedException)
                {// ThreadInterruptedException's are OK, since we use them to awake sleeping threads when closing down.
                    SystemMonitor.Report(exception.ToString() + "[" + exception.InnerException.Message + "]");
                    entity.Conversation.EntityExecutionFailed(entity, exception);
                }
                else
                {
                    SystemMonitor.OperationError(exception.ToString() + "[" + exception.InnerException.Message + "]");
                    entity.Conversation.EntityExecutionFailed(entity, exception);
                }
            }
            catch (ThreadInterruptedException exception)
            {
                // ThreadInterruptedException's are OK, since we use them to awake sleeping threads when closing down.
                SystemMonitor.Report(exception.ToString() + "[" + exception.Message + "]");
                entity.Conversation.EntityExecutionFailed(entity, exception);
            }
            catch (Exception exception)
            {
                SystemMonitor.Error(exception.ToString());
                entity.Conversation.EntityExecutionFailed(entity, exception);
            }
            finally
            {
                entity.Die();
            }

            lock (this)
            {
                if (_clientsRunningExecutioners.ContainsKey(entity.ReceiverID))
                {
                    int newClientsValue = _clientsRunningExecutioners[entity.ReceiverID] - 1;
                    if (newClientsValue <= 0)
                    {
                        _clientsRunningExecutioners.Remove(entity.ReceiverID);
                    }
                    else
                    {
                        _clientsRunningExecutioners[entity.ReceiverID] = newClientsValue;
                    }
                }
                else
                {
                    if (IsDisposed == false)
                    {
                        SystemMonitor.Error("ClientsRunningExecutioners not properly maintained.");
                    }
                }

                TracerHelper.TraceExit("entity finished for [" + (DateTime.Now - startTime).Milliseconds + "]ms [" + entity.Message.GetType().Name + ", " + entity.ReceiverID.Id.Name + "], total count [" + _threadPool.ActiveRunningThreadsCount.ToString() + "]");
            }

            // Continue execution chain.
            UpdatePendingExecution();
        }
        /// <summary>
        /// Slow (?!), using reflection (Invokes). Invokes take about 20% of the time.
        /// Can be sped up using this approach:
        /// http://blog.lab49.com/archives/446
        /// </summary>
        public bool UpdateToDB <ItemType>(IEnumerable <ItemType> items,
                                          Dictionary <string, object> fixedValues)
            where ItemType : class, IDBPersistent
        {
            TracerHelper.TraceEntry(typeof(ItemType).Name);
            string tableName = _tablesTypeNames[typeof(ItemType)];

            List <PropertyInfo> infos = GetTypePersistableProperties(typeof(ItemType), false, true, false);

            StringBuilder commandText = new StringBuilder("UPDATE " + tableName + " ");

            List <string> additionalValuesNames = new List <string>();

            if (fixedValues != null)
            {
                additionalValuesNames.AddRange(fixedValues.Keys);
            }

            foreach (PropertyInfo info in infos)
            {
                additionalValuesNames.Remove(info.Name);
            }


            List <SQLiteParameter> parameters      = new List <SQLiteParameter>();
            List <PropertyInfo>    parametersInfos = new List <PropertyInfo>();

            SQLiteParameter objectParameter = null;

            _updateMutex.WaitOne();

            try
            {
                using (SQLiteConnection connection = GenerateConnection())
                {
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        {// Column names.
                            commandText.Append(" SET ");

                            for (int i = 0; i < infos.Count; i++)
                            {
                                string name = infos[i].Name;

                                if (fixedValues != null && fixedValues.ContainsKey(name) && fixedValues[name] == null)
                                {
                                    continue;
                                }

                                commandText.Append(name + " = ");

                                string paramName = "@Param" + infos[i].Name;

                                commandText.Append(paramName + ",");
                                SQLiteParameter parameter = new SQLiteParameter(paramName);
                                command.Parameters.Add(parameter);
                                parameters.Add(parameter);
                                parametersInfos.Add(infos[i]);
                            }

                            // Additional fixed values (Fixed values may contain additional values)
                            foreach (string name in additionalValuesNames)
                            {
                                commandText.Append(name + " = ");
                                commandText.Append("@ParamFixed" + name + ",");
                                command.Parameters.Add(new SQLiteParameter("@ParamFixed" + name, fixedValues[name]));
                            }

                            if (commandText[commandText.Length - 1] == ',')
                            {// Fix final comma (if there is one).
                                commandText.Remove(commandText.Length - 1, 1);
                            }

                            //commandText.Append(" ) ");
                        }

                        SQLiteParameter whereIdParam = new SQLiteParameter("@ParamId");
                        command.Parameters.Add(whereIdParam);
                        commandText.Append(" WHERE Id = " + whereIdParam.ParameterName);

                        command.CommandText = commandText.ToString();
                        connection.Open();
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
                        {
                            //for (int i = 0; i < items.Count; i++)
                            foreach (ItemType item in items)
                            {
                                SystemMonitor.CheckThrow(item.Id.HasValue, "Updating an item with no ID already assigned.");

                                for (int j = 0; j < parameters.Count; j++)
                                {
                                    parameters[j].Value = HandleGetValue(item, parametersInfos[j]);
                                }

                                if (objectParameter != null)
                                {
                                    using (MemoryStream stream = new MemoryStream())
                                    {
                                        SerializationHelper.Serialize(stream, item);
                                        objectParameter.Value = stream.GetBuffer();
                                    }
                                }

                                whereIdParam.Value = item.Id;
                                if (command.ExecuteNonQuery() != 1)
                                {
                                    SystemMonitor.Error("Command query execution error.");
                                }
                            }

                            transaction.Commit();
                        }
                    }
                }
            }
            finally
            {
                _updateMutex.ReleaseMutex();
            }

            TracerHelper.TraceExit();
            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        public DataSet Select(string[] tablesNames, MatchExpression matchExpression, int?limit)
        {
            string names = "";

            for (int i = 0; i < tablesNames.Length; i++)
            {
                names += tablesNames[i];
                if (i != tablesNames.Length - 1)
                {
                    names += ",";
                }
            }

            TracerHelper.TraceEntry(names);

            StringBuilder commandText = new StringBuilder();

            commandText.Append("SELECT * FROM " + names);

            if (matchExpression != null && matchExpression.ClauseCount > 0)
            {
                commandText.Append(" WHERE ");
            }

            _selectMutex.WaitOne();

            DataSet set = new DataSet();

            try
            {
                using (SQLiteConnection connection = GenerateConnection())
                {
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        if (matchExpression != null)
                        {
                            matchExpression.SetupCommandParameters(command, commandText);
                        }

                        if (limit.HasValue)
                        {
                            commandText.Append(" LIMIT " + limit.Value);
                        }

                        SQLiteDataAdapter adapter = new SQLiteDataAdapter();

                        command.CommandText = commandText.ToString();
                        connection.Open();

                        adapter.SelectCommand = command;
                        adapter.Fill(set);
                    }
                }
            }
            finally
            {
                _selectMutex.ReleaseMutex();
            }

            TracerHelper.TraceExit();
            return(set);
        }
        /// <summary>
        /// Central sending function.
        /// </summary>
        /// <param name="receiverID">The ID of the receiver module. Can be <b>null</b> and this sends to all in the Arbiter.</param>
        protected TransportMessage[] DoSendCustom(bool isRequest, Guid sessionGuid,
                                                  Type expectedResponceMessageClassType, int responcesRequired,
                                                  ArbiterClientId?receiverId, ArbiterClientId?senderId, TransportMessage message, TimeSpan timeOut)
        {
            TracerHelper.TraceEntry();

            SessionResults session = null;

            if (receiverId.HasValue && receiverId.Value.IsEmpty /*receiverId.Value.CompareTo(ArbiterClientId.Empty) == 0*/)
            {
                SystemMonitor.Error("Can not send an item to empty receiver. Use null to specify broadcast.");
                return(null);
            }

            // Preliminary verification.
            if (Arbiter == null)
            {
                SystemMonitor.OperationWarning("Using a client [" + this.GetType().Name + ":" + senderId.Value.ClientName + " to " + (receiverId.HasValue ? receiverId.Value.Id.Name : string.Empty) + " , " + message.GetType().Name + "] with no Arbiter assigned.");
                return(null);
            }

            message.IsRequest = isRequest;

            TransportInfoUnit infoUnit = new TransportInfoUnit(sessionGuid, senderId, receiverId);

            message.TransportInfo.AddTransportInfoUnit(infoUnit);

            bool sessionEventResult = false;

            if (expectedResponceMessageClassType != null)
            {// Responce waiting session.
                session = new SessionResults(responcesRequired, expectedResponceMessageClassType);
                lock (_communicationSessions)
                {// Register the session.
                    _communicationSessions.Add(sessionGuid, session);
                }
            }

            SystemMonitor.CheckError(message.TransportInfo.CurrentTransportInfo != null);
            Conversation conversation;

            if (receiverId == null)
            {
                // We shall not use the next level time out mechanism.
                conversation = Arbiter.CreateConversation(senderId.Value, message, TimeSpan.Zero);
            }
            else
            {// Addressed conversation.
                // We shall not use the next level time out mechanism.
                conversation = Arbiter.CreateConversation(senderId.Value, receiverId.Value, message, TimeSpan.Zero);
            }

            if (conversation != null && expectedResponceMessageClassType != null)
            {     // Responce waiting session (only if conversation was properly created).
                if (timeOut == TimeSpan.Zero)
                { // Wait forever.
                    sessionEventResult = session.SessionEndEvent.WaitOne();
                }
                else
                {// Wait given period.
                    sessionEventResult = session.SessionEndEvent.WaitOne(timeOut, false);
                }

                lock (_communicationSessions)
                {// Remote the session.
                    _communicationSessions.Remove(sessionGuid);
                }
            }

            message.TransportInfo.PopTransportInfo();

            if (expectedResponceMessageClassType == null)
            {// No responce waiting, just return.
                TracerHelper.TraceExit();
                return(null);
            }

            // Responce waiting session.
            if (sessionEventResult == false)
            {// Timed out - only send and receives can time out, as the other ones do not have sessions!!
                TracerHelper.TraceError("Session has timed out [" + message.GetType().Name + "].");
                return(null);
            }

            TracerHelper.TraceExit();
            return(session.Responces.ToArray());
        }