diff options
Diffstat (limited to 'dotnet/Qpid.Codec')
19 files changed, 1250 insertions, 0 deletions
diff --git a/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs b/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs new file mode 100644 index 0000000000..70b4940c7b --- /dev/null +++ b/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs @@ -0,0 +1,140 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using Qpid.Buffer; + +namespace Qpid.Codec +{ + public abstract class CumulativeProtocolDecoder : IProtocolDecoder + { + ByteBuffer _remaining; + + /// <summary> + /// Creates a new instance with the 65535 bytes initial capacity of + /// cumulative buffer. + /// Note that capacity is currently fixed as the .NET ByteBuffers does not implement auto-expansion. + /// </summary> + protected CumulativeProtocolDecoder() + { + // Needs to be as large as the largest frame to be decoded. + _remaining = ByteBuffer.Allocate(65535); // FIXME: Probably should not be fixed. + } + + /// <summary> + /// Cumulates content of <tt>in</tt> into internal buffer and forwards + /// decoding request to {@link #doDecode(IoSession, ByteBuffer, ProtocolDecoderOutput)}. + /// <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt> + /// and the cumulative buffer is compacted after decoding ends. + /// </summary> + /// <exception cref="Exception"> + /// if your <tt>doDecode()</tt> returned <tt>true</tt> not consuming the cumulative buffer. + /// </exception> + public void Decode(ByteBuffer input, IProtocolDecoderOutput output) + { + if (_remaining.Position != 0) // If there were remaining undecoded bytes + { + DecodeRemainingAndInput(input, output); + } + else + { + DecodeInput(input, output); + } + } + + private void DecodeInput(ByteBuffer input, IProtocolDecoderOutput output) + { + // Just decode the input buffer and remember any remaining undecoded bytes. + try + { + DecodeAll(input, output); + } + finally + { + if (input.HasRemaining()) + { + _remaining.Put(input); + } + } + } + + private void DecodeRemainingAndInput(ByteBuffer input, IProtocolDecoderOutput output) + { + // Concatenate input buffer with left-over bytes. + _remaining.Put(input); + _remaining.Flip(); + + try + { + DecodeAll(_remaining, output); + } + finally + { + _remaining.Compact(); + } + } + + private void DecodeAll(ByteBuffer buf, IProtocolDecoderOutput output) + { + for (;;) + { + int oldPos = buf.Position; + bool decoded = DoDecode(buf, output); + if (decoded) + { + if (buf.Position == oldPos) + { + throw new Exception( + "doDecode() can't return true when buffer is not consumed."); + } + + if (!buf.HasRemaining()) + { + break; + } + } + else + { + break; + } + } + } + + /// <summary> + /// Implement this method to consume the specified cumulative buffer and + /// decode its content into message(s). + /// </summary> + /// <param name="input">the cumulative buffer</param> + /// <param name="output">decoder output</param> + /// <returns> + /// <tt>true</tt> if and only if there's more to decode in the buffer + /// and you want to have <tt>doDecode</tt> method invoked again. + /// Return <tt>false</tt> if remaining data is not enough to decode, + /// then this method will be invoked again when more data is cumulated. + /// </returns> + /// <exception cref="Exception">If cannot decode</exception> + protected abstract bool DoDecode(ByteBuffer input, IProtocolDecoderOutput output); + + public void Dispose() + { + _remaining = null; + } + } +} diff --git a/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs b/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs new file mode 100644 index 0000000000..8a58eecda4 --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs @@ -0,0 +1,385 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using System.Collections; +using Qpid.Buffer; + +namespace Qpid.Codec.Demux +{ + public class DemuxingProtocolCodecFactory : IProtocolCodecFactory + { + private ArrayList _decoderFactories = new ArrayList(); + private ArrayList _encoderFactories = new ArrayList(); + + public void Register(Type encoderOrDecoderClass) + { + if (encoderOrDecoderClass == null) + { + throw new ArgumentNullException("encoderOrDecoderClass"); + } + + bool registered = false; + if (typeof(IMessageEncoder).IsAssignableFrom(encoderOrDecoderClass)) + { + Register(new DefaultConstructorMessageEncoderFactory(encoderOrDecoderClass)); + registered = true; + } + + if (typeof(IMessageDecoder).IsAssignableFrom(encoderOrDecoderClass)) + { + Register(new DefaultConstructorMessageDecoderFactory(encoderOrDecoderClass)); + registered = true; + } + + if (!registered) + { + throw new ArgumentException("Unregisterable type: " + encoderOrDecoderClass); + } + } + + public void Register(IMessageEncoder encoder) + { + Register(new SingletonMessageEncoderFactory(encoder)); + } + + public void Register(IMessageEncoderFactory factory) + { + if (factory == null) + { + throw new ArgumentNullException("factory"); + } + + _encoderFactories.Add(factory); + } + + public void Register(IMessageDecoder decoder) + { + Register(new SingletonMessageDecoderFactory(decoder)); + } + + public void Register(IMessageDecoderFactory factory) + { + if (factory == null) + { + throw new ArgumentNullException("factory"); + } + _decoderFactories.Add(factory); + } + + public IProtocolEncoder Encoder + { + get + { + return new ProtocolEncoderImpl(this); + } + } + + public IProtocolDecoder Decoder + { + get + { + return new ProtocolDecoderImpl(this); + } + } + + protected void DisposeCodecResources() + { + // Do nothing by default + } + + private class ProtocolEncoderImpl : IProtocolEncoder + { + private readonly Hashtable _encoders = new Hashtable(); + + private DemuxingProtocolCodecFactory _enclosing; + + public ProtocolEncoderImpl(DemuxingProtocolCodecFactory enclosing) + { + _enclosing = enclosing; + ArrayList encoderFactories = enclosing._encoderFactories; + for (int i = encoderFactories.Count - 1; i >= 0; i--) + { + IMessageEncoder encoder = ((IMessageEncoderFactory)encoderFactories[i]).NewEncoder(); + foreach (Type type in encoder.MessageTypes.Keys) + { + _encoders[type] = encoder; + } + } + } + + public void Encode(object message, IProtocolEncoderOutput output) + { + Type type = message.GetType(); + IMessageEncoder encoder = FindEncoder(type); + if (encoder == null) + { + throw new ProtocolEncoderException("Unexpected message type: " + type); + } + + encoder.Encode(message, output); + } + + private IMessageEncoder FindEncoder(Type type) + { + IMessageEncoder encoder = (IMessageEncoder)_encoders[type]; + if (encoder == null) + { + encoder = FindEncoder(type, new Hashtable()); + } + + return encoder; + } + + private IMessageEncoder FindEncoder(Type type, Hashtable triedClasses) + { + IMessageEncoder encoder; + + if (triedClasses.Contains(type)) + { + return null; + } + triedClasses[type] = 1; + + encoder = (IMessageEncoder)_encoders[type]; + if (encoder == null) + { + encoder = FindEncoder(type, triedClasses); + if (encoder != null) + { + return encoder; + } + + Type[] interfaces = type.GetInterfaces(); + for (int i = 0; i < interfaces.Length; i++) + { + encoder = FindEncoder(interfaces[i], triedClasses); + if (encoder != null) + { + return encoder; + } + } + + return null; + } + else + return encoder; + } + + public void Dispose() + { + _enclosing.DisposeCodecResources(); + } + } + + private class ProtocolDecoderImpl : CumulativeProtocolDecoder + { + private readonly IMessageDecoder[] _decoders; + private IMessageDecoder _currentDecoder; + private DemuxingProtocolCodecFactory _enclosing; + + public ProtocolDecoderImpl(DemuxingProtocolCodecFactory enclosing) + { + _enclosing = enclosing; + ArrayList decoderFactories = _enclosing._decoderFactories; + _decoders = new IMessageDecoder[decoderFactories.Count]; + for (int i = decoderFactories.Count - 1; i >= 0; i--) + { + _decoders[i] = ((IMessageDecoderFactory) decoderFactories[i]).NewDecoder(); + } + } + + protected override bool DoDecode(ByteBuffer input, IProtocolDecoderOutput output) + { + MessageDecoderResult result; + if (_currentDecoder == null) + { + IMessageDecoder[] decoders = _decoders; + int undecodables = 0; + + for (int i = decoders.Length - 1; i >= 0; i --) + { + IMessageDecoder decoder = decoders[i]; + int limit = input.Limit; + int pos = input.Position; + + try + { + result = decoder.Decodable(input); + } + finally + { + input.Position = pos; + input.Limit = limit; + } + + if (result == MessageDecoderResult.OK) + { + _currentDecoder = decoder; + break; + } + else if(result == MessageDecoderResult.NOT_OK) + { + undecodables ++; + } + else if (result != MessageDecoderResult.NEED_DATA) + { + throw new Exception("Unexpected decode result (see your decodable()): " + result); + } + } + + if (undecodables == _decoders.Length) + { + // Throw an exception if all decoders cannot decode data. + input.Position = input.Limit; // Skip data + throw new ProtocolDecoderException( + "No appropriate message decoder: " + input.HexDump); + } + + if (_currentDecoder == null) + { + // Decoder is not determined yet (i.e. we need more data) + return false; + } + } + + result = _currentDecoder.Decode(input, output); + if (result == MessageDecoderResult.OK) + { + _currentDecoder = null; + return true; + } + else if (result == MessageDecoderResult.NEED_DATA) + { + return false; + } + else if (result == MessageDecoderResult.NOT_OK) + { + throw new ProtocolDecoderException("Message decoder returned NOT_OK."); + } + else + { + throw new Exception("Unexpected decode result (see your decode()): " + result); + } + } + } + + private class SingletonMessageEncoderFactory : IMessageEncoderFactory + { + private readonly IMessageEncoder _encoder; + + public SingletonMessageEncoderFactory(IMessageEncoder encoder) + { + if (encoder == null) + { + throw new ArgumentNullException("encoder"); + } + _encoder = encoder; + } + + public IMessageEncoder NewEncoder() + { + return _encoder; + } + } + + private class SingletonMessageDecoderFactory : IMessageDecoderFactory + { + private readonly IMessageDecoder _decoder; + + public SingletonMessageDecoderFactory(IMessageDecoder decoder) + { + if (decoder == null) + { + throw new ArgumentNullException("decoder"); + } + _decoder = decoder; + } + + public IMessageDecoder NewDecoder() + { + return _decoder; + } + } + + private class DefaultConstructorMessageEncoderFactory : IMessageEncoderFactory + { + private readonly Type _encoderClass; + + public DefaultConstructorMessageEncoderFactory(Type encoderClass) + { + if (encoderClass == null) + { + throw new ArgumentNullException("encoderClass"); + } + + if(!typeof(IMessageEncoder).IsAssignableFrom(encoderClass)) + { + throw new ArgumentException("encoderClass is not assignable to MessageEncoder"); + } + _encoderClass = encoderClass; + } + + public IMessageEncoder NewEncoder() + { + try + { + return (IMessageEncoder) Activator.CreateInstance(_encoderClass); + } + catch (Exception e) + { + throw new Exception( "Failed to create a new instance of " + _encoderClass, e); + } + } + } + + private class DefaultConstructorMessageDecoderFactory : IMessageDecoderFactory + { + private readonly Type _decoderClass; + + public DefaultConstructorMessageDecoderFactory(Type decoderClass) + { + if (decoderClass == null) + { + throw new ArgumentNullException("decoderClass"); + } + + if(!typeof(IMessageDecoder).IsAssignableFrom(decoderClass)) + { + throw new ArgumentException("decoderClass is not assignable to MessageDecoder"); + } + _decoderClass = decoderClass; + } + + public IMessageDecoder NewDecoder() + { + try + { + return (IMessageDecoder) Activator.CreateInstance(_decoderClass); + } + catch (Exception e) + { + throw new Exception("Failed to create a new instance of " + _decoderClass, e); + } + } + } + } +} + diff --git a/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs b/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs new file mode 100644 index 0000000000..686544c78b --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs @@ -0,0 +1,55 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using Qpid.Buffer; + +namespace Qpid.Codec.Demux +{ + public interface IMessageDecoder + { + /// <summary> + /// Checks the specified buffer is decodable by this decoder. + /// </summary> + /// <param name="buffer">The buffer to read data from.</param> + /// <returns> + /// OK if this decoder can decode the specified buffer. + /// NOT_OK if this decoder cannot decode the specified buffer. + /// if more data is required to determine if the + /// specified buffer is decodable ({@link #OK}) or not decodable + /// {@link #NOT_OK}.</returns> + MessageDecoderResult Decodable(ByteBuffer buffer); + + /// <summary> + /// Decodes binary or protocol-specific content into higher-level message objects. + /// MINA invokes {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)} + /// method with read data, and then the decoder implementation puts decoded + /// messages into {@link ProtocolDecoderOutput}. + /// </summary> + /// <returns> + /// {@link #OK} if you finished decoding messages successfully. + /// {@link #NEED_DATA} if you need more data to finish decoding current message. + /// {@link #NOT_OK} if you cannot decode current message due to protocol specification violation. + /// </returns> + /// <exception cref="Exception">if the read data violated protocol specification </exception> + MessageDecoderResult Decode(ByteBuffer buffer, IProtocolDecoderOutput output); + } +} + diff --git a/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs b/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs new file mode 100644 index 0000000000..418b5cf7e4 --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs @@ -0,0 +1,31 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +namespace Qpid.Codec.Demux +{ + public interface IMessageDecoderFactory + { + /// <summary> + /// Creates a new message decoder. + /// </summary> + IMessageDecoder NewDecoder(); + } +} + diff --git a/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs b/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs new file mode 100644 index 0000000000..7c1d117ad5 --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs @@ -0,0 +1,47 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using System.Collections; + +namespace Qpid.Codec.Demux +{ + public interface IMessageEncoder + { + /// <summary> + /// Returns the set of message classes this encoder can encode. + /// </summary> + Hashtable MessageTypes + { + get; + } + + /// <summary> + /// Encodes higher-level message objects into binary or protocol-specific data. + /// MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)} + /// method with message which is popped from the session write queue, and then + /// the encoder implementation puts encoded {@link ByteBuffer}s into + /// {@link ProtocolEncoderOutput}. + /// </summary> + /// <exception cref="Exception">if the message violated protocol specification</exception> + void Encode(Object message, IProtocolEncoderOutput output); + } +} + diff --git a/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs b/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs new file mode 100644 index 0000000000..b8996e1853 --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs @@ -0,0 +1,31 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +namespace Qpid.Codec.Demux +{ + public interface IMessageEncoderFactory + { + /// <summary> + /// Creates a new message encoder. + /// </summary> + IMessageEncoder NewEncoder(); + } +} + diff --git a/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs b/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs new file mode 100644 index 0000000000..689d305919 --- /dev/null +++ b/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs @@ -0,0 +1,28 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +namespace Qpid.Codec.Demux +{ + public enum MessageDecoderResult + { + OK, NOT_OK, NEED_DATA + } +} + diff --git a/dotnet/Qpid.Codec/IProtocolCodecFactory.cs b/dotnet/Qpid.Codec/IProtocolCodecFactory.cs new file mode 100644 index 0000000000..fe4ccc7fde --- /dev/null +++ b/dotnet/Qpid.Codec/IProtocolCodecFactory.cs @@ -0,0 +1,36 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +namespace Qpid.Codec +{ + public interface IProtocolCodecFactory + { + IProtocolEncoder Encoder + { + get; + } + + IProtocolDecoder Decoder + { + get; + } + } +} + diff --git a/dotnet/Qpid.Codec/IProtocolDecoder.cs b/dotnet/Qpid.Codec/IProtocolDecoder.cs new file mode 100644 index 0000000000..54317c3d07 --- /dev/null +++ b/dotnet/Qpid.Codec/IProtocolDecoder.cs @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; +using Qpid.Buffer; + +namespace Qpid.Codec +{ + public interface IProtocolDecoder : IDisposable + { + /// <summary> + /// Decodes binary or protocol-specific content into higher-level message objects. + /// MINA invokes {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)} + /// method with read data, and then the decoder implementation puts decoded + /// messages into {@link ProtocolDecoderOutput}. + /// </summary> + /// <param name="input"></param> + /// <param name="output"></param> + /// <exception cref="Exception">if the read data violated protocol specification</exception> + void Decode(ByteBuffer input, IProtocolDecoderOutput output); + } +} + diff --git a/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs b/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs new file mode 100644 index 0000000000..5862b36aed --- /dev/null +++ b/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs @@ -0,0 +1,34 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +namespace Qpid.Codec +{ + public interface IProtocolDecoderOutput + { + /// <summary> + /// Callback for {@link ProtocolDecoder} to generate decoded messages. + /// {@link ProtocolDecoder} must call {@link #write(Object)} for each + /// decoded messages. + /// </summary> + /// <param name="message">the decoded message</param> + void Write(object message); + } +} + diff --git a/dotnet/Qpid.Codec/IProtocolEncoder.cs b/dotnet/Qpid.Codec/IProtocolEncoder.cs new file mode 100644 index 0000000000..aab803d3d6 --- /dev/null +++ b/dotnet/Qpid.Codec/IProtocolEncoder.cs @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; + +namespace Qpid.Codec +{ + public interface IProtocolEncoder : IDisposable + { + /// <summary> + /// Encodes higher-level message objects into binary or protocol-specific data. + /// MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)} + /// method with message which is popped from the session write queue, and then + /// the encoder implementation puts encoded {@link ByteBuffer}s into + /// {@link ProtocolEncoderOutput}. + /// </summary> + /// <param name="message"></param> + /// <param name="output"></param> + /// <exception cref="Exception">if the message violated protocol specification</exception> + void Encode(Object message, IProtocolEncoderOutput output); + } +} + diff --git a/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs b/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs new file mode 100644 index 0000000000..12b9d68885 --- /dev/null +++ b/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs @@ -0,0 +1,36 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using Qpid.Buffer; + +namespace Qpid.Codec +{ + public interface IProtocolEncoderOutput + { + /// <summary> + /// Callback for {@link ProtocolEncoder} to generate encoded + /// {@link ByteBuffer}s. {@link ProtocolEncoder} must call + /// {@link #write(ByteBuffer)} for each decoded messages. + /// </summary> + /// <param name="buf">the buffer which contains encoded data</param> + void Write(ByteBuffer buf); + } +} + diff --git a/dotnet/Qpid.Codec/Properties/AssemblyInfo.cs b/dotnet/Qpid.Codec/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..515e5f2998 --- /dev/null +++ b/dotnet/Qpid.Codec/Properties/AssemblyInfo.cs @@ -0,0 +1,53 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System.Reflection; +using System.Runtime.InteropServices; +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Mina.Codec")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Apache Qpid")] +[assembly: AssemblyProduct("Mina.Codec")] +[assembly: AssemblyCopyright("Copyright (c) 2006 The Apache Software Foundation")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8bfe84f8-cd88-48f7-b0d2-0010411a14e5")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/dotnet/Qpid.Codec/ProtocolCodecException.cs b/dotnet/Qpid.Codec/ProtocolCodecException.cs new file mode 100644 index 0000000000..797b475f19 --- /dev/null +++ b/dotnet/Qpid.Codec/ProtocolCodecException.cs @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; + +namespace Qpid.Codec +{ + public class ProtocolCodecException : Exception + { + public ProtocolCodecException() : base() + { + } + + public ProtocolCodecException(string message) : base(message) + { + } + + public ProtocolCodecException(Exception cause) : base("Codec Exception", cause) + { + } + } +} + diff --git a/dotnet/Qpid.Codec/ProtocolDecoderException.cs b/dotnet/Qpid.Codec/ProtocolDecoderException.cs new file mode 100644 index 0000000000..94f3e6a591 --- /dev/null +++ b/dotnet/Qpid.Codec/ProtocolDecoderException.cs @@ -0,0 +1,54 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; + +namespace Qpid.Codec +{ + public class ProtocolDecoderException : ProtocolCodecException + { + private string _hexdump; + + public ProtocolDecoderException() : base() + { + } + + public ProtocolDecoderException(string message) : base(message) + { + } + + public ProtocolDecoderException(Exception cause) : base(cause) + { + } + + public string HexDump + { + get + { + return _hexdump; + } + set + { + _hexdump = value; + } + } + } +} + diff --git a/dotnet/Qpid.Codec/ProtocolEncoderException.cs b/dotnet/Qpid.Codec/ProtocolEncoderException.cs new file mode 100644 index 0000000000..db589c6c00 --- /dev/null +++ b/dotnet/Qpid.Codec/ProtocolEncoderException.cs @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System; + +namespace Qpid.Codec +{ + public class ProtocolEncoderException : ProtocolCodecException + { + public ProtocolEncoderException() : base() + { + } + + public ProtocolEncoderException(string message) : base(message) + { + } + + public ProtocolEncoderException(Exception cause) : base(cause) + { + } + } +} + diff --git a/dotnet/Qpid.Codec/Qpid.Codec.csproj b/dotnet/Qpid.Codec/Qpid.Codec.csproj new file mode 100644 index 0000000000..581f68c5b4 --- /dev/null +++ b/dotnet/Qpid.Codec/Qpid.Codec.csproj @@ -0,0 +1,73 @@ +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{22D0D0C2-77AF-4DE3-B456-7FF3893F9F88}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Qpid.Codec</RootNamespace>
+ <AssemblyName>Qpid.Codec</AssemblyName>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="log4net, Version=1.2.0.30714, Culture=neutral, PublicKeyToken=500ffcafb14f92df">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\Qpid.Common\lib\log4net\log4net.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="CumulativeProtocolDecoder.cs" />
+ <Compile Include="Demux\DemuxingProtocolCodecFactory.cs" />
+ <Compile Include="Demux\IMessageDecoder.cs" />
+ <Compile Include="Demux\IMessageDecoderFactory.cs" />
+ <Compile Include="Demux\IMessageEncoder.cs" />
+ <Compile Include="Demux\IMessageEncoderFactory.cs" />
+ <Compile Include="Demux\MessageDecoderResult.cs" />
+ <Compile Include="IProtocolCodecFactory.cs" />
+ <Compile Include="IProtocolDecoder.cs" />
+ <Compile Include="IProtocolDecoderOutput.cs" />
+ <Compile Include="IProtocolEncoder.cs" />
+ <Compile Include="IProtocolEncoderOutput.cs" />
+ <Compile Include="ProtocolCodecException.cs" />
+ <Compile Include="ProtocolDecoderException.cs" />
+ <Compile Include="ProtocolEncoderException.cs" />
+ <Compile Include="Support\SimpleProtocolDecoderOutput.cs" />
+ <Compile Include="Support\SimpleProtocolEncoderOutput.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Qpid.Buffer\Qpid.Buffer.csproj">
+ <Project>{44384DF2-B0A4-4580-BDBC-EE4BAA87D995}</Project>
+ <Name>Qpid.Buffer</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file diff --git a/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.cs b/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.cs new file mode 100644 index 0000000000..bb5d377ad9 --- /dev/null +++ b/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.cs @@ -0,0 +1,43 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System.Collections; + +namespace Qpid.Codec.Support +{ + public class SimpleProtocolDecoderOutput : IProtocolDecoderOutput + { + private readonly Queue _messageQueue = new Queue(); + + public Queue MessageQueue + { + get + { + return _messageQueue; + } + } + + public void Write(object message) + { + _messageQueue.Enqueue(message); + } + } +} + diff --git a/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.cs b/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.cs new file mode 100644 index 0000000000..6f4f5cbb28 --- /dev/null +++ b/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.cs @@ -0,0 +1,44 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +using System.Collections; +using Qpid.Buffer; + +namespace Qpid.Codec.Support +{ + public abstract class SimpleProtocolEncoderOutput : IProtocolEncoderOutput + { + private readonly Queue _bufferQueue = new Queue(); + + public Queue BufferQueue + { + get + { + return _bufferQueue; + } + } + + public void Write(ByteBuffer buf) + { + _bufferQueue.Enqueue(buf); + } + } +} + |
