// Main query processing method
        public void ProcessQuery(QueryInfo query)
        {
            // Object where the data obtained after query execution is placed
            // This object will be sent to the DB
            AnswerInfo answer;

            // Get a name of a symbol if it's CQGTimedBarsRequestClass object's request
            // and show it in MiniMonitor form
            if (query.ObjectType == "CQGTimedBarsRequestClass")
            {
                object qObj      = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);
                string instrName = (string)qObj.GetType().InvokeMember("Symbol",
                                                                       BindingFlags.GetProperty, null, qObj, null);
                if (!DCMiniMonitor.symbolsList.Contains(instrName))
                {
                    DCMiniMonitor.symbolsList.Add(instrName);
                    Program.MiniMonitor.SymbolsListsUpdate();
                }
            }

            // Handling of the request depending on its kind
            switch (query.QueryType)
            {
            case QueryType.CallCtor:
            {
                try
                {
                    string key;

                    // Handling of the ctor calling request and creation of an object depending on its type
                    switch (query.MemberName)
                    {
                    // Handling of CQGCEL ctor calling request
                    case "CQG.CQGCELClass":
                        key = CqgDataManagement.CEL_key;
                        break;

                    // Common case
                    default:
                        object[] args = Core.GetArgsIntoArrayFromTwoDicts(query.ArgKeys, query.ArgValues);
                        object   qObj = CQGAssm.CreateInstance(query.MemberName, false,
                                                               BindingFlags.CreateInstance, null, args, null, null);
                        key = Core.CreateUniqueKey();

                        ServerDictionaries.PutObjectToTheDictionary(key, qObj);
                        UsedObjs.Add(key, qObj);
                        break;
                    }

                    answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, valueKey: key);
                    PushAnswerAndDeleteQuery(answer);
                }
                catch
                {
                    try
                    {
                        AutoGenQueryProcessing(query);
                    }
                    catch (Exception ex)
                    {
                        answer = CreateExceptionAnswer(ex, query);
                        PushAnswerAndDeleteQuery(answer);
                    }
                }
            }
            break;

            case QueryType.CallDtor:
            {
                if (query.ObjectKey != CqgDataManagement.CEL_key)
                {
                    UsedObjs.Remove(query.ObjectKey);
                    ServerDictionaries.RemoveObjectFromTheDictionary(query.ObjectKey);
                }

                // Remove name of a symbol if it's CQG.CQGTimedBarsRequest object deleting
                // from MiniMonitor form
                if (query.MemberName == "CQG.CQGTimedBarsRequest")
                {
                    object qObj     = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);
                    string symbName = (string)qObj.GetType().InvokeMember("Symbol",
                                                                          BindingFlags.GetProperty, null, qObj, null);
                    DCMiniMonitor.symbolsList.Remove(symbName);
                    Program.MiniMonitor.SymbolsListsUpdate();
                }

                answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, value: true);

                PushAnswerAndDeleteQuery(answer);
            }
            break;

            case QueryType.GetProperty:
            {
                object qObj = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);

                object[] args = Core.GetArgsIntoArrayFromTwoDicts(query.ArgKeys, query.ArgValues);

                try
                {
                    // Getting of property value
                    var propV = qObj.GetType().InvokeMember(query.MemberName, BindingFlags.GetProperty, null, qObj, args);

                    // Checking type of property value and returning value or value key
                    // (second, if it's not able to be transmitted through the database)
                    answer = Core.IsSerializableType(propV.GetType()) ?
                             CreateValAnswer(query.QueryKey, query.ObjectKey, query.MemberName, propV) :
                             CreateKeyAnswer(query.QueryKey, query.ObjectKey, query.MemberName, propV);

                    PushAnswerAndDeleteQuery(answer);
                }
                catch
                {
                    try
                    {
                        AutoGenQueryProcessing(query);
                    }
                    catch (Exception ex)
                    {
                        answer = CreateExceptionAnswer(ex, query);
                        PushAnswerAndDeleteQuery(answer);
                    }
                }
            }
            break;

            case QueryType.SetProperty:
            {
                object qObj = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);

                object[] args = Core.GetArgsIntoArrayFromTwoDicts(query.ArgKeys, query.ArgValues);

                if (string.Concat(qObj.GetType()) == "CQG.CQGTimedBarsRequest" && query.MemberName == "Symbol")
                {
                    DCMiniMonitor.symbolsList.Add(string.Concat(args[0]));
                    Program.MiniMonitor.SymbolsListsUpdate();
                }

                try
                {
                    // Setting of property value
                    qObj.GetType().InvokeMember(query.MemberName, BindingFlags.SetProperty, null, qObj, args);
                    answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, value: true);

                    PushAnswerAndDeleteQuery(answer);
                }
                catch
                {
                    try
                    {
                        AutoGenQueryProcessing(query);
                    }
                    catch (Exception ex)
                    {
                        answer = CreateExceptionAnswer(ex, query);
                        PushAnswerAndDeleteQuery(answer);
                    }
                }
            }
            break;

            case QueryType.CallMethod:
            {
                // Handling of Shutdown method calling by CQGCEL object. This method has not be called from client applications
                if (query.MemberName == "Shutdown")
                {
                    var returnKey = "true";
                    answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, valueKey: returnKey);
                    PushAnswerAndDeleteQuery(answer);
                    break;
                }

                object qObj = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);

                object[] args = Core.GetArgsIntoArrayFromTwoDicts(query.ArgKeys, query.ArgValues);

                try
                {
                    object returnV;

                    bool isGetter = query.MemberName.StartsWith("get_");
                    bool isSetter = query.MemberName.StartsWith("set_");
                    if (isGetter || isSetter)
                    {
                        // Access property instead of calling method
                        string       propName   = query.MemberName.Substring(4);
                        BindingFlags invokeAttr = isGetter ? BindingFlags.GetProperty : BindingFlags.SetProperty;
                        returnV = qObj.GetType().InvokeMember(propName, invokeAttr, null, qObj, args);
                    }
                    else
                    {
                        returnV = qObj.GetType().InvokeMember(query.MemberName, BindingFlags.InvokeMethod, null, qObj, args);
                    }

                    if (!object.ReferenceEquals(returnV, null))
                    {
                        // Handling method call request depending of return value type
                        answer = Core.IsSerializableType(returnV.GetType()) ?
                                 CreateValAnswer(query.QueryKey, query.ObjectKey, query.MemberName, returnV) :
                                 CreateKeyAnswer(query.QueryKey, query.ObjectKey, query.MemberName, returnV);
                    }
                    else
                    {
                        // Handling void method call
                        var returnKey = "true";
                        answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, valueKey: returnKey);
                    }

                    PushAnswerAndDeleteQuery(answer);
                }
                catch
                {
                    try
                    {
                        AutoGenQueryProcessing(query);
                    }
                    catch (Exception ex)
                    {
                        answer = CreateExceptionAnswer(ex, query);
                        PushAnswerAndDeleteQuery(answer);
                    }
                }
            }
            break;

            case QueryType.SubscribeToEvent:
            case QueryType.UnsubscribeFromEvent:
            {
                object qObj = UsedObjs.Contains(query.ObjectKey) ? UsedObjs[query.ObjectKey] : ServerDictionaries.GetObjectFromTheDictionary(query.ObjectKey);

                try
                {
                    System.Reflection.EventInfo ei = qObj.GetType().GetEvent(query.MemberName);

                    if (EventHandler.EventAppsSubscribersNum.ContainsKey(query.MemberName))
                    {
                        EventHandler.EventAppsSubscribersNum[query.MemberName] =
                            query.QueryType == QueryType.SubscribeToEvent ?
                            EventHandler.EventAppsSubscribersNum[query.MemberName] + 1 :
                            EventHandler.EventAppsSubscribersNum[query.MemberName] - 1;
                    }
                    else
                    {
                        EventHandler.EventAppsSubscribersNum.Add(query.MemberName, 1);
                    }

                    // Find corresponding CQG delegate
                    Type delType = FindDelegateType(CQGAssm, query.MemberName);

                    // Instantiate the delegate with our own handler
                    var        eventHandlersMethods = typeof(CQGEventHandlers).GetMethods();
                    MethodInfo handlerInfo          = null;

                    for (int i = 0; i < eventHandlersMethods.Length; i++)
                    {
                        if (eventHandlersMethods[i].Name.Contains(query.MemberName))
                        {
                            handlerInfo = eventHandlersMethods[i];
                        }
                    }

                    Delegate d = Delegate.CreateDelegate(delType, handlerInfo);

                    if (query.QueryType == QueryType.SubscribeToEvent)
                    {
                        // Subscribe our handler to CQG event
                        ei.AddEventHandler(qObj, d);
                    }
                    else if (query.QueryType == QueryType.UnsubscribeFromEvent)
                    {
                        // Unsubscribe our handler from CQG event
                        ei.RemoveEventHandler(qObj, d);
                    }

                    answer = new AnswerInfo(query.QueryKey, query.ObjectKey, query.MemberName, value: true);

                    PushAnswerAndDeleteQuery(answer);
                }
                catch
                {
                    try
                    {
                        AutoGenQueryProcessing(query);
                    }
                    catch (Exception ex)
                    {
                        answer = CreateExceptionAnswer(ex, query);
                        PushAnswerAndDeleteQuery(answer);
                    }
                }

                if (query.QueryType == QueryType.SubscribeToEvent &&
                    query.MemberName == "DataConnectionStatusChanged")
                {
                    // Fire this event explicitly, because data collector connects to real CQG beforehand and does not fire it anymore
                    CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(CqgDataManagement.currConnStat);
                }
            }
            break;

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }
Beispiel #2
0
        private void m_CEL_CELDataConnectionChg(eConnectionStatus new_status)
        {
            StringBuilder connStatusString      = new StringBuilder();
            StringBuilder connStatusShortString = new StringBuilder();
            Color         connColor             = Color.Red;

            try
            {
                if (m_CEL.IsStarted)
                {
                    connStatusString.Append("CQG API:");
                    connStatusString.Append(m_CEL.Environment.CELVersion);
                    connStatusShortString.Append("CQG:");

                    if (new_status != eConnectionStatus.csConnectionUp)
                    {
                        if (new_status == eConnectionStatus.csConnectionDelayed)
                        {
                            connColor = Color.BlanchedAlmond;
                            connStatusString.Append(" - CONNECTION IS DELAYED");
                            connStatusShortString.Append("DELAYED");

                            currConnStat = eConnectionStatus.csConnectionDelayed;
                            CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionDelayed);
                        }
                        else
                        {
                            connStatusString.Append(" - CONNECTION IS DOWN");
                            connStatusShortString.Append("DOWN");

                            currConnStat = eConnectionStatus.csConnectionDown;
                            CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionDelayed);
                        }
                    }
                    else
                    {
                        connColor = Color.LawnGreen;
                        connStatusString.Append(" - CONNECTION IS UP");
                        connStatusShortString.Append("UP");

                        currConnStat = eConnectionStatus.csConnectionUp;
                        CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionUp);
                    }
                }
                else
                {
                    connStatusString.Append("WAITING FOR API CONNECTION");

                    connStatusShortString.Append("WAITING");
                }

                if (mainForm != null)
                {
                    mainForm.UpdateConnectionStatus(
                        connStatusString.ToString(), connColor);
                }
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut(Convert.ToString(this), ex);
            }
        }