internal static IFreeformEntity_Msg InitKeyExchangeEndG2H_SIM(this FreeformSecurityTableCollection secTables,
                                                                      IFreeformEntity_Msg request)
        {
            // store the partial key of the host
            FFTgt_B2B_Security_KeyExchange_PartialKey tgt = request.EntityPrimaryTarget as FFTgt_B2B_Security_KeyExchange_PartialKey;
            SECURITY_KEY_INDEX keyIndex = FreeformEncryptionHelper.GetSecurityKeyIndex(request);

            byte[] gmuPartialKey = secTables.CreatePartialKeyGmu(keyIndex);
            secTables.StoreOtherPartialKeyGmu(keyIndex, tgt.PartialKey);
            secTables.CreateCommonKey(keyIndex);

            // send the partial key of the gmu
            IFreeformEntity_Msg response = request.CopyTo(FF_FlowDirection.G2H, new FFCreateEntityRequest_G2H_ResponseRequired()
            {
                MessageType       = FF_AppId_G2H_MessageTypes.FreeForm,
                Command           = FF_AppId_G2H_Commands.ResponseRequest,
                SkipTransactionId = true,
            });
            FFTgt_B2B_Security tgt2 = new FFTgt_B2B_Security()
            {
                SecurityData = new FFTgt_B2B_Security_KeyExchange_End()
                {
                    PartialKey = gmuPartialKey,
                }
            };

            response.AddTarget(tgt2);
            return(response);
        }
        internal static IFreeformEntity_Msg InitKeyExchangePartialKeyH2G_GMU(this FreeformSecurityTableCollection secTables,
                                                                             IFreeformEntity_Msg request)
        {
            SECURITY_KEY_INDEX keyIndex = FreeformEncryptionHelper.GetSecurityKeyIndex(request);

            byte[] hostPartialKey       = secTables.CreatePartialKeyHost(keyIndex);
            IFreeformEntity_Msg message = request.CopyTo(FF_FlowDirection.H2G, new FFCreateEntityRequest_H2G_ResponseRequired()
            {
                PollCode = FF_AppId_H2G_PollCodes.FreeformResponse,
            });

            InitSecurityData(message,
                             new FFTgt_B2B_Security_KeyExchange_PartialKey()
            {
                PartialKey = hostPartialKey,
            });
            return(message);
        }
        internal static IFreeformEntity_Msg InitKeyExchangeStatusH2G_GMU(this FreeformSecurityTableCollection secTables,
                                                                         IFreeformEntity_Msg request)
        {
            // store the partial key of the gmu
            FFTgt_B2B_Security_KeyExchange_End tgt = request.EntityPrimaryTarget as FFTgt_B2B_Security_KeyExchange_End;
            SECURITY_KEY_INDEX keyIndex            = FreeformEncryptionHelper.GetSecurityKeyIndex(request);

            secTables.StoreOtherPartialKeyHost(keyIndex, tgt.PartialKey);
            secTables.CreateCommonKey(keyIndex);

            IFreeformEntity_Msg message = request.CopyTo(FF_FlowDirection.H2G, new FFCreateEntityRequest_H2G_ResponseRequired()
            {
                PollCode = FF_AppId_H2G_PollCodes.FreeformNoResponse,
            });

            InitSecurityData(message,
                             new FFTgt_B2B_Security_KeyExchange_Status()
            {
                Status = (request.EntityPrimaryTarget is FFTgt_B2B_Security_PartialKey ?
                          FF_AppId_ResponseStatus_Types.Success :
                          FF_AppId_ResponseStatus_Types.Fail),
            });
            return(message);
        }
        public bool Execute(IFreeformEntity_Msg request)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "ProcessMessage(G2H)"))
            {
                bool   result    = default(bool);
                string ipAddress = string.Empty;

                try
                {
                    using (FFTgtExecutionContext context = new FFTgtExecutionContext(request))
                    {
                        Stack <IFreeformEntity> st = new Stack <IFreeformEntity>();
                        request.CopyTo(st);

                        // push all the grandchildren into stack and process again
                        while (st.Count != 0)
                        {
                            IFreeformEntity_MsgTgt target = st.Pop() as IFreeformEntity_MsgTgt;
                            if (target == null || target.IsLeafNode)
                            {
                                continue;
                            }

                            _requestResponseMappings.Persist(request, target);
                            string targetKey = CreateSessionTargetKey((int)request.SessionID, target.TypeKey);
                            if (_targetHandlers.ContainsKey(targetKey))
                            {
                                _HandlerInfo info = _targetHandlers[targetKey];
                                context.HandlerAttribute = info.HandlerAttribute;
                                info.Handler.Execute(context, target);
                            }

                            target.CopyTo(st);
                        }

                        if (request.TransactionID <= 0)
                        {
                            request.TransactionID = FFMsgHandlerFactory.NewTransactionId;
                        }

                        if (request.SessionID != FF_AppId_SessionIds.Internal)
                        {
                            // ok everything processed, now do the external or internal processing
                            if (request.FlowDirection == FF_FlowDirection.G2H)
                            {
                                // add the monitor meters
                                if (context.MonitorMeters.IsValueCreated)
                                {
                                    context.MonitorTargets.Value.Add(context.MonitorMeters.Value);
                                }
                                _msgTransmitter.ProcessMessage(request as FFMsg_G2H, context.MonitorTargets.Value);
                            }
                            else if (request.FlowDirection == FF_FlowDirection.H2G)
                            {
                                _msgTransmitter.ProcessMessage(request as FFMsg_H2G);
                            }

                            // process the internal messages
                            if (context.FreeformTargets != null &&
                                context.FreeformTargets.Count > 0)
                            {
                                FFMsg_H2G h2gMessage = null;
                                foreach (var freeformEntity in context.FreeformTargets)
                                {
                                    if (freeformEntity is IFreeformEntity_Msg)
                                    {
                                        FFMsgHandlerFactory.Current.Execute(freeformEntity as IFreeformEntity_Msg);
                                    }
                                    else if (freeformEntity is IFreeformEntity_MsgTgt)
                                    {
                                        if (h2gMessage == null)
                                        {
                                            h2gMessage = FreeformEntityFactory.CreateEntity <FFMsg_H2G>(FF_FlowDirection.H2G,
                                                                                                        new FFCreateEntityRequest_H2G()
                                            {
                                                IPAddress     = request.IpAddress,
                                                PollCode      = FF_AppId_H2G_PollCodes.FreeformNoResponse,
                                                SessionID     = request.SessionID,
                                                TransactionID = request.TransactionID,
                                            });
                                        }
                                        h2gMessage.Targets.Add(freeformEntity as IFreeformEntity_MsgTgt);
                                    }
                                }

                                if (h2gMessage != null)
                                {
                                    FFMsgHandlerFactory.Current.Execute(h2gMessage);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
        public bool Execute(IFreeformEntity_Msg request)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "ProcessMessage(G2H)"))
            {
                bool result = default(bool);
                string ipAddress = string.Empty;

                try
                {
                    using (FFTgtExecutionContext context = new FFTgtExecutionContext(request))
                    {
                        Stack<IFreeformEntity> st = new Stack<IFreeformEntity>();
                        request.CopyTo(st);

                        // push all the grandchildren into stack and process again
                        while (st.Count != 0)
                        {
                            IFreeformEntity_MsgTgt target = st.Pop() as IFreeformEntity_MsgTgt;
                            if (target == null || target.IsLeafNode) continue;

                            _requestResponseMappings.Persist(request, target);
                            string targetKey = CreateSessionTargetKey((int)request.SessionID, target.TypeKey);
                            if (_targetHandlers.ContainsKey(targetKey))
                            {
                                _HandlerInfo info = _targetHandlers[targetKey];
                                context.HandlerAttribute = info.HandlerAttribute;
                                info.Handler.Execute(context, target);
                            }

                            target.CopyTo(st);
                        }

                        if (request.TransactionID <= 0)
                        {
                            request.TransactionID = FFMsgHandlerFactory.NewTransactionId;
                        }

                        if (request.SessionID != FF_AppId_SessionIds.Internal)
                        {
                            // ok everything processed, now do the external or internal processing
                            if (request.FlowDirection == FF_FlowDirection.G2H)
                            {
                                // add the monitor meters
                                if (context.MonitorMeters.IsValueCreated)
                                {
                                    context.MonitorTargets.Value.Add(context.MonitorMeters.Value);
                                }
                                _msgTransmitter.ProcessMessage(request as FFMsg_G2H, context.MonitorTargets.Value);
                            }
                            else if (request.FlowDirection == FF_FlowDirection.H2G)
                            {
                                _msgTransmitter.ProcessMessage(request as FFMsg_H2G);
                            }

                            // process the internal messages
                            if (context.FreeformTargets != null &&
                                context.FreeformTargets.Count > 0)
                            {
                                FFMsg_H2G h2gMessage = null;
                                foreach (var freeformEntity in context.FreeformTargets)
                                {
                                    if (freeformEntity is IFreeformEntity_Msg)
                                    {
                                        FFMsgHandlerFactory.Current.Execute(freeformEntity as IFreeformEntity_Msg);
                                    }
                                    else if (freeformEntity is IFreeformEntity_MsgTgt)
                                    {
                                        if (h2gMessage == null)
                                        {
                                            h2gMessage = FreeformEntityFactory.CreateEntity<FFMsg_H2G>(FF_FlowDirection.H2G,
                                                new FFCreateEntityRequest_H2G()
                                                {
                                                    IPAddress = request.IpAddress,
                                                    PollCode = FF_AppId_H2G_PollCodes.FreeformNoResponse,
                                                    SessionID = request.SessionID,
                                                    TransactionID = request.TransactionID,
                                                });
                                        }
                                        h2gMessage.Targets.Add(freeformEntity as IFreeformEntity_MsgTgt);
                                    }
                                }

                                if (h2gMessage != null)
                                {
                                    FFMsgHandlerFactory.Current.Execute(h2gMessage);
                                }

                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return result;
            }
        }