public override void DoRequest(DataPacket packet, PBCaGw.Workers.WorkerChain chain, DataPacketDelegate sendData)
 {
     /*DataPacket newPacket = (DataPacket)packet.Clone();
     newPacket.Destination = packet.Sender;
     newPacket.DataType = 0;
     newPacket.DataCount = Gateway.CA_PROTO_VERSION;
     SendData(newPacket);*/
 }
 static void splitter_ReceiveData(PBCaGw.DataPacket packet)
 {
     Console.WriteLine("Got packet of " + packet.BufferSize + " / " + packet.Data.Length + ", cmd: " + packet.Command);
 }
 /// <summary>
 /// Used to answer to a response
 /// </summary>
 /// <param name="command"></param>
 /// <param name="packet"></param>
 /// <param name="chain"></param>
 /// <param name="send"> </param>
 /// <returns></returns>
 public static void ExecuteResponseHandler(UInt16 command, DataPacket packet, PBCaGw.Workers.WorkerChain chain, DataPacketDelegate send)
 {
     if (!(command >= handlers.Length || handlers[command] == null))
         handlers[command].DoResponse(packet, chain, send);
 }
        public override void DoRequest(DataPacket packet, PBCaGw.Workers.WorkerChain chain, DataPacketDelegate sendData)
        {
            // From ourself? Skip it.
            if (packet.Sender.Equals(chain.ClientEndPoint))
                    return;

            /*// Not coming from one of the allowed destination
            if (chain.Destinations.Any(i => CompareNetC(i.Address, packet.Sender.Address)))
                return;*/

            if (chain.Destinations == null)
                return;

            if (packet.Sender == null)
                return;
            if (Log.WillDisplay(TraceEventType.Verbose))
                Log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, chain.ChainId, "Search from: " + packet.Sender);

            //if(chain.Gateway.Configuration.LocalSideA.Port == chain.Gateway.Configuration.LocalSideB.Port)

            // It's a response
            //if (packet.PayloadSize <= 8 && packet.Chain.Gateway.Configuration.ConfigurationType == ConfigurationType.BIDIRECTIONAL)
            if (packet.PayloadSize == 8)
            {
                DoResponse(packet, chain, sendData);
                return;
            }

            DiagnosticServer.NbSearches++;
            DataPacket newPacket = (DataPacket)packet.Clone();
            string channelName = packet.GetDataAsString();

            if (chain.Side == Workers.ChainSide.SIDE_A)
            {
                if (!chain.Gateway.Configuration.Security.EvaluateSideA(channelName, null, null, packet.Sender.Address.ToString()).Has(SecurityAccess.READ))
                    return;
            }
            else
            {
                if (!chain.Gateway.Configuration.Security.EvaluateSideB(channelName, null, null, packet.Sender.Address.ToString()).Has(SecurityAccess.READ))
                    return;
            }

            Record record;

            // Maybe this request is known.
            if (InfoService.ChannelEndPoint.Knows(channelName))
            {
                record = InfoService.ChannelEndPoint[channelName];
                bool knownChannel = false;
                if (chain.Side == ChainSide.SIDE_A && record.knownFromSideA == true)
                    knownChannel=true;
                else if (chain.Side == ChainSide.SIDE_B && record.knownFromSideB == true)
                    knownChannel=true;

                if(knownChannel)
                {
                    if (Log.WillDisplay(TraceEventType.Information))
                        Log.TraceEvent(TraceEventType.Information, chain.ChainId, "Cached search " + channelName);
                    newPacket = DataPacket.Create(8, packet.Chain);
                    newPacket.ReverseAnswer = true;
                    newPacket.Command = 6;
                    newPacket.Parameter1 = 0xffffffff;
                    newPacket.Parameter2 = packet.Parameter1;
                    if (chain.Side == Workers.ChainSide.SIDE_A)
                        newPacket.DataType = (UInt16)chain.Gateway.Configuration.LocalSideA.Port;
                    else
                        newPacket.DataType = (UInt16)chain.Gateway.Configuration.LocalSideB.Port;
                    newPacket.DataCount = 0;
                    newPacket.SetUInt16(16, Gateway.CA_PROTO_VERSION);

                    newPacket.Destination = packet.Sender;
                    sendData(newPacket);
                    return;
                }
            }

            if (chain.Side == ChainSide.SIDE_A)
                record = InfoService.SearchChannelEndPointA[channelName];
            else
                record = InfoService.SearchChannelEndPointB[channelName];

            // We have the info stored in the channel end point? Yes let's use it then.
            /*if (record == null
                && InfoService.ChannelEndPoint.Knows(channelName))
            {
                record = InfoService.ChannelEndPoint[channelName];
            }*/

            // First time, or never got answer, let's ask to the IOCS
            // Step 1
            if (record == null)
            {
                record = InfoService.SearchChannel.Create();
                // ReSharper disable PossibleInvalidOperationException
                uint gwcid = record.GWCID.Value;
                // ReSharper restore PossibleInvalidOperationException
                record.CID = packet.Parameter1;
                record.Client = packet.Sender;
                record.Channel = channelName;

                // Diagnostic search
                newPacket = (DataPacket)packet.Clone();
                newPacket.Parameter1 = gwcid;
                newPacket.Parameter2 = gwcid;
                newPacket.Destination = new IPEndPoint(chain.Gateway.Configuration.LocalSideB.Address, 7890);
                if (chain.Side == Workers.ChainSide.SIDE_B)
                    newPacket.ReverseAnswer = true;
                sendData(newPacket);

                foreach (IPEndPoint dest in chain.Destinations)
                {
                    newPacket = (DataPacket)packet.Clone();
                    newPacket.Parameter1 = gwcid;
                    newPacket.Parameter2 = gwcid;
                    newPacket.Destination = dest;
                    sendData(newPacket);
                }
            }
            // We have the info, therefore use the stored info to answer (cached)
            else
            {
                newPacket = DataPacket.Create(8, packet.Chain);
                newPacket.ReverseAnswer = true;
                newPacket.Command = 6;
                newPacket.Parameter1 = 0xffffffff;
                newPacket.Parameter2 = packet.Parameter1;
                if (chain.Side == Workers.ChainSide.SIDE_A)
                    newPacket.DataType = (UInt16)chain.Gateway.Configuration.LocalSideA.Port;
                else
                    newPacket.DataType = (UInt16)chain.Gateway.Configuration.LocalSideB.Port;
                newPacket.DataCount = 0;
                newPacket.SetUInt16(16, Gateway.CA_PROTO_VERSION);

                newPacket.Destination = packet.Sender;
                sendData(newPacket);
            }
        }