예제 #1
0
        /**
         *  Sends a SIF_Event
         *  @param zone The zone to send the sifEvent to
         */
        public SIF_Ack SifEvent(IZone zone, Event sifEvent, String destinationId, String sifMsgId)
        {
            if (sifEvent.Data == null || sifEvent.Data.Available == false)
            {
                throw new AdkException("The sifEvent has no SIFDataObjects", zone);
            }

            SIF_ObjectData od = new SIF_ObjectData();

            //  Fill out the SIF_ObjectData
            IDataObjectInputStream inStr = sifEvent.Data;
            SifDataObject          data  = inStr.ReadDataObject();

            SifVersion msgVersion = data.EffectiveSIFVersion;

            SIF_EventObject eo = new SIF_EventObject();

            od.SIF_EventObject = eo;
            eo.Action          = sifEvent.ActionString;
            eo.ObjectName      = data.ElementDef.Tag(msgVersion);

            // Create the SIF_Event object
            SIF_Event msg = new SIF_Event(msgVersion);

            msg.SIF_ObjectData = od;

            SIF_Header msgHdr = msg.Header;

            //	Assign SIF_DestinationId if applicable
            if (destinationId != null)
            {
                msgHdr.SIF_DestinationId = destinationId;
            }

            while (data != null)
            {
                eo.Attach(data);
                data = inStr.ReadDataObject();
            }

            if (sifMsgId != null)
            {
                msgHdr.SIF_MsgId = sifMsgId;
            }

            SifContext[] contexts = sifEvent.Contexts;
            if (contexts == null)
            {
                contexts = new SifContext[] { SifContext.DEFAULT };
            }

            SIF_Contexts msgContexts = new SIF_Contexts();

            foreach (SifContext context in contexts)
            {
                msgContexts.AddSIF_Context(context.Name);
            }
            msgHdr.SIF_Contexts = msgContexts;
            return(((ZoneImpl)zone).Dispatcher.send(msg));
        }
예제 #2
0
        // TODO: Implement

        /*
         *
         * private SIF_Request createSIF_Request( ElementDef objectType, String refId, Zone zone )
         * {
         * SIF_Request request = new SIF_Request();
         * request.getHeader().setSIF_MsgId( MSG_GUID );
         * request.getHeader().setSIF_SourceId( "foo" );
         * request.setSIF_MaxBufferSize("32768");
         * request.setSIF_Version( ADK.getSIFVersion().toString() );
         * Query query = new Query(objectType);
         * query.addCondition(SifDtd.SIF_REPORTOBJECT_REFID, Condition.EQ, refId);
         *
         * SIF_Query q = SIFPrimitives.createSIF_Query(query, zone);
         * SIF_QueryObject sqo = new SIF_QueryObject();
         * sqo.setObjectName( objectType.name() );
         *
         * q.setSIF_QueryObject(sqo);
         * request.setSIF_Query(q);
         *
         * return request;
         * }
         *
         */


        private SIF_Response createSIF_Response(IElementDef objType, bool storeInRequestCache, ISerializable stateObject)
        {
            SIF_Request req = createSIF_Request(objType);

            if (storeInRequestCache)
            {
                Query q = new Query(objType);
                q.UserData = stateObject;
                RequestCache.GetInstance(fAgent).StoreRequestInfo(req, q, fZone);
            }

            SIF_Response resp = new SIF_Response();

            resp.SIF_RequestMsgId = req.Header.SIF_MsgId;
            SIF_ObjectData sod = new SIF_ObjectData();

            resp.SIF_ObjectData = sod;

            Object responseObject = null;

            try
            {
                responseObject = ClassFactory.CreateInstance(objType.FQClassName, false);
            }
            catch (Exception cfe)
            {
                throw new AdkException("Unable to create instance of " + objType.Name, fZone, cfe);
            }

            sod.AddChild((SifElement)responseObject);
            return(resp);
        }
예제 #3
0
        private SIF_Event createSIF_Event(IElementDef objType)
        {
            SIF_Event evnt = new SIF_Event();

            evnt.Header.SIF_SourceId = "foo";
            SIF_ObjectData  sod = new SIF_ObjectData();
            SIF_EventObject obj = new SIF_EventObject();

            obj.ObjectName      = objType.Name;
            sod.SIF_EventObject = obj;
            evnt.SIF_ObjectData = sod;

            obj.Action = EventAction.Add.ToString();

            Object eventObject = null;

            try
            {
                eventObject = ClassFactory.CreateInstance(objType.FQClassName);
            }
            catch (Exception cfe)
            {
                throw new AdkException("Unable to create instance of " + objType.Name, fZone, cfe);
            }


            obj.AddChild(objType, (SifElement)eventObject);

            return(evnt);
        }
예제 #4
0
        public static SifMessagePayload MakeSIFMessagePayload(SifElement payload)
        {
            if (payload is SIF_Response)
            {
                return((SIF_Response)payload);
            }

            SIF_Response rsp = new SIF_Response();

            rsp.SetSIF_MorePackets(YesNo.NO);
            rsp.SIF_RequestMsgId = Adk.MakeGuid();
            rsp.SIF_PacketNumber = 1;

            SIF_Header hdr = rsp.Header;

            hdr.SIF_Timestamp = DateTime.Now;
            hdr.SIF_MsgId     = Adk.MakeGuid();
            hdr.SIF_SourceId  = "ADK Unit Tests";
            hdr.SetSIF_Security(new SIF_SecureChannel(AuthenticationLevel.ZERO,
                                                      EncryptionLevel.ZERO));
            hdr.SIF_DestinationId = "Schema Validator";
            if (payload is SifDataObject)
            {
                SIF_ObjectData data = new SIF_ObjectData();
                data.AddChild(payload);
                rsp.SIF_ObjectData = data;
            }
            else if (payload is SIF_ObjectData)
            {
                rsp.SIF_ObjectData = (SIF_ObjectData)payload;
            }
            else
            {
                throw new ArgumentException("Unable to use payload: "
                                            + payload.ElementDef.Name);
            }

            return(rsp);
        }
예제 #5
0
        /// <summary>  Calculate the size of a SIF_Response minus the SIF_ObjectData content.</summary>
        protected virtual long CalcEnvelopeSize(ZoneImpl zone)
        {
            long size = 400;

            try {
                SIF_Response rsp = new SIF_Response();
                rsp.SIF_MorePackets  = "Yes";
                rsp.SIF_RequestMsgId = fReqId;
                rsp.SIF_PacketNumber = 100000000;
                SIF_ObjectData data = new SIF_ObjectData();
                data.TextValue     = " ";
                rsp.SIF_ObjectData = data;

                SIF_Header hdr = rsp.Header;

                hdr.SIF_Timestamp     = DateTime.Now;
                hdr.SIF_MsgId         = "012345678901234567890123456789012";
                hdr.SIF_SourceId      = zone.Agent.Id;
                hdr.SIF_Security      = zone.Dispatcher.secureChannel();
                hdr.SIF_DestinationId = fDestId;

                using (MemoryStream buffer = new MemoryStream()) {
                    SifWriter writer = new SifWriter(buffer);
                    writer.Write(rsp);
                    writer.Flush();
                    size = buffer.Length + 10;
                    writer.Close();
                    buffer.Close();
                }
            }
            catch (Exception ex) {
                zone.Log.Warn("Error calculating packet size: " + ex, ex);
            }

            return(size);
        }
예제 #6
0
        private void RunLengthTest(Stream[] payloads, bool error, bool replace, SifVersion version)
        {
            // This test loosely emulates what ResponseDelivery does when it builds up the MessageStreamer class
            //  Prepare SIF_Response
            SIF_Response rsp = new SIF_Response();

            rsp.SIF_MorePackets  = "No";
            rsp.SIF_RequestMsgId = "12345123451234512345";
            rsp.SIF_PacketNumber = 1;

            rsp.SifVersion = version;

            //  Write an empty "<SIF_ObjectData> </SIFObjectData>" for the
            //  MessageStreamer to fill in. If this is an errorPacket, the empty
            //  element is required per SIF Specifications.
            SIF_ObjectData placeholder = new SIF_ObjectData();

            placeholder.TextValue = " ";
            rsp.SIF_ObjectData    = placeholder;

            if (error)
            {
                SIF_Error err = new SIF_Error();
                err.TextValue = " ";
                rsp.SIF_Error = err;
            }


            //  Assign values to message header - this is usually done by
            //  MessageDispatcher.send() but because we're preparing a SIF_Response
            //  output stream we need to do it manually
            SIF_Header hdr = rsp.Header;

            hdr.SIF_Timestamp     = DateTime.Now;
            hdr.SIF_MsgId         = SifFormatter.GuidToSifRefID(Guid.NewGuid());
            hdr.SIF_SourceId      = "UnitTest";
            hdr.SIF_DestinationId = "123451234512345";

            //  Write SIF_Response -- without its SIF_ObjectData payload -- to a buffer
            using (MemoryStream envelope = new MemoryStream())
            {
                SifWriter writer = new SifWriter(envelope);
                writer.Write(rsp);
                writer.Flush();

                envelope.Seek(0, SeekOrigin.Begin);
                StreamReader reader      = new StreamReader(envelope, Encoding.UTF8);
                String       envelopeStr = reader.ReadToEnd();
                Console.Out.WriteLine(envelopeStr);

                envelope.Seek(0, SeekOrigin.Begin);

                using (
                    MessageStreamer ms =
                        new MessageStreamer(envelope, payloads, error ? "<SIF_Error>" : "<SIF_ObjectData>", replace))
                {
                    AssertMessageStreamer(ms, version);
                }
                envelope.Close();
            }
        }
예제 #7
0
        /// <summary>
        /// Sends a specific packet to the zone
        /// </summary>
        /// <param name="file"></param>
        /// <param name="morePackets"></param>
        protected internal virtual void SendPacket(FileInfo file,
                                                   bool morePackets)
        {
            long extraBytes = 0;

            ResponsePacketInfo responsePacket = DeserializeResponseFileName(file.Name);

            /*	If we're processing SIF_ReportObject responses, read the ReportInfo
             *	data from the "requestMsgId.rpt" file
             *
             *  TT 894 - If a SIFException is thrown after the ReportInfo is set on
             *  the ReportObjectStream, then we don't want to include that ReportInfo
             *  in the packet with the error.  In that case, rptInfoReader will be
             *  null and will not be included in the list of payloads below.
             */

            FileStream rptInfoStream = null;

            if (fSrc == ResponseDeliveryType.SIFReportObject && !responsePacket.errorPacket)
            {
                try {
                    FileInfo f =
                        new FileInfo
                            (fWorkDir + Path.DirectorySeparatorChar.ToString() + responsePacket.destinationId + "." +
                            responsePacket.requestMsgId + ".rpt");
                    rptInfoStream = f.OpenRead();
                    extraBytes    = f.Length;
                }
                catch (FileNotFoundException fnfe) {
                    fZone.Log.Debug
                        ("Error sending SIF_ReportObject packet #" + responsePacket.packetNumber + (morePackets ? "" : " (last packet)") + ", file not found: " + fnfe.Message);
                }
            }

            if ((Adk.Debug & AdkDebugFlags.Messaging) != 0)
            {
                fZone.Log.Debug
                    ("Sending " + (responsePacket.errorPacket ? "SIF_Error response" : "SIF_Response") +
                    " packet #" + responsePacket.packetNumber +
                    (morePackets ? "" : " (last packet)"));
            }

            //  Prepare SIF_Response
            SIF_Response rsp = new SIF_Response();

            rsp.SetSIF_MorePackets(morePackets ? YesNo.YES : YesNo.NO);
            rsp.SIF_RequestMsgId = responsePacket.requestMsgId;
            rsp.SIF_PacketNumber = responsePacket.packetNumber;

            //  The SIF_Response is rendered in the same version of SIF as the original SIF_Request
            rsp.SifVersion = responsePacket.version;

            if (responsePacket.errorPacket)
            {
                //  Write an empty "<SIF_Error> </SIF_Error>" for the MessageStreamer
                //  to replace
                SIF_Error err = new SIF_Error();
                err.TextValue = " ";
                rsp.SIF_Error = err;
            }

            if (!responsePacket.errorPacket || responsePacket.version.Major == 1)
            {
                //  Write an empty "<SIF_ObjectData> </SIFObjectData>" for the
                //  MessageStreamer to fill in. If this is an errorPacket, the empty
                //  element is required per the SIF 1.x Specifications, but disallowed
                // in SIF 2.x.
                SIF_ObjectData placeholder = new SIF_ObjectData();
                placeholder.TextValue = " ";
                rsp.SIF_ObjectData    = placeholder;
            }


            //  Assign values to message header - this is usually done by
            //  MessageDispatcher.send() but because we're preparing a SIF_Response
            //  output stream we need to do it manually
            SIF_Header hdr = rsp.Header;

            hdr.SIF_Timestamp     = DateTime.Now;
            hdr.SIF_MsgId         = SifFormatter.GuidToSifRefID(Guid.NewGuid());
            hdr.SIF_SourceId      = fZone.Agent.Id;
            hdr.SIF_Security      = fZone.Dispatcher.secureChannel();
            hdr.SIF_DestinationId = responsePacket.destinationId;

            //  Write SIF_Response -- without its SIF_ObjectData payload -- to a buffer
            using (MemoryStream envelope = new MemoryStream()) {
                SifWriter writer = new SifWriter(envelope);
                writer.Write(rsp);
                writer.Flush();
                envelope.Seek(0, SeekOrigin.Begin);

                FileStream fs = file.OpenRead();
                try {
                    //  Send the SIF_Response as a stream
                    Stream [] payloads;
                    if (fSrc == ResponseDeliveryType.Generic)
                    {
                        payloads = new Stream [] { fs };
                    }
                    else
                    {
                        if (rptInfoStream != null)
                        {
                            payloads = new Stream [] { rptInfoStream, fs };
                        }
                        else
                        {
                            payloads = new Stream [] { fs };
                        }
                    }

                    using (MessageStreamer ms = new MessageStreamer
                                                (
                               envelope,
                               payloads,
                               responsePacket.errorPacket ? "<SIF_Error>" : "<SIF_ObjectData>", responsePacket.errorPacket))
                    {
                        if ((Adk.Debug & AdkDebugFlags.Messaging) != 0)
                        {
                            fZone.Log.Debug("Send SIF_Response");
                        }
                        if ((Adk.Debug & AdkDebugFlags.Messaging_Detailed) != 0)
                        {
                            fZone.Log.Debug("  MsgId: " + rsp.MsgId);
                        }

                        SIF_Ack ack;
                        using (IMessageInputStream ackStream = fZone.ProtocolHandler.Send(ms)) {
                            ack =
                                (SIF_Ack)
                                fParser.Parse
                                    (ackStream.GetInputStream(), fZone, SifParserFlags.None);
                        }
                        if (ack != null)
                        {
                            ack.LogRecv(fZone.Log);
                        }
                    }

                    //  If we get here, the message was sent successfully
                    envelope.Close();

                    for (int i = 0; i < payloads.Length; i++)
                    {
                        payloads[i].Close();
                    }
                    fs.Close();

                    if (DELETE_ON_SUCCESS && file.Exists)
                    {
                        file.Delete();
                    }
                }
                catch (AdkException adke) {
                    AdkUtils._throw(adke, fZone.Log);
                }
                catch (Exception e) {
                    AdkUtils._throw
                        (new AdkException("Failed to send SIF_Response: " + e, fZone), fZone.Log);
                }
                finally {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                    if (rptInfoStream != null)
                    {
                        rptInfoStream.Close();
                    }
                }
            }
        }