private void AckPush(SIF_Ack ack, AdkHttpResponse response) { try { // Set SIF_Ack / SIF_Header fields SIF_Header hdr = ack.Header; hdr.SIF_Timestamp = DateTime.Now; hdr.SIF_MsgId = SifFormatter.GuidToSifRefID(Guid.NewGuid()); hdr.SIF_SourceId = this.Zone.Agent.Id; ack.LogSend(this.Zone.Log); response.ContentType = SifIOFormatter.CONTENTTYPE; // TODO: This code may need to change. The ADKHttpResponse should not automatically set the content length // and other implementations will not do so. SifWriter w = new SifWriter(response.GetResponseStream()); w.Write(ack); w.Flush(); } catch (Exception thr) { Console.Out.WriteLine ("HttpProtocolHandler failed to send SIF_Ack for pushed message (zone=" + this.Zone.ZoneId + "): " + thr); throw new AdkHttpException (AdkHttpStatusCode.ServerError_500_Internal_Server_Error, thr.Message, thr); } }
public void testConvertUUIDToRefId() { String str = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"; Guid guid = new Guid(str); String adkGuid = SifFormatter.GuidToSifRefID(guid); assertRefId(adkGuid); Assert.AreEqual("F81D4FAE7DEC11D0A76500A0C91E6BF6", adkGuid, "match"); }
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(); } }
/// <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(); } } } }