diff options
Diffstat (limited to 'qpid/dotnet/Qpid.Codec')
20 files changed, 1364 insertions, 0 deletions
diff --git a/qpid/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs b/qpid/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs new file mode 100644 index 0000000000..6cfd75c851 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/CumulativeProtocolDecoder.cs @@ -0,0 +1,152 @@ +/* + * + * 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 log4net; +using Apache.Qpid.Buffer; + +namespace Apache.Qpid.Codec +{ + public abstract class CumulativeProtocolDecoder : IProtocolDecoder + { + static ILog _logger = LogManager.GetLogger(typeof(CumulativeProtocolDecoder)); + + ByteBuffer _remaining; + + /// <summary> + /// Creates a new instance with the 4096 bytes initial capacity of + /// cumulative buffer. + /// </summary> + protected CumulativeProtocolDecoder() + { + _remaining = AllocateBuffer(); + } + + /// <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) + { + _logger.Debug(string.Format("DecodeInput: input {0}", input.Remaining)); + // 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) + { + _logger.Debug(string.Format("DecodeRemainingAndInput: input {0}, remaining {1}", input.Remaining, _remaining.Position)); + // replace the _remainder buffer, so that we can leave the + // original one alone. Necessary because some consumer splice + // the buffer and only consume it until later, causing + // a race condition if we compact it too soon. + ByteBuffer newRemainding = AllocateBuffer(); + ByteBuffer temp = _remaining; + _remaining = newRemainding; + temp.Put(input); + temp.Flip(); + try + { + DecodeAll(temp, output); + } finally + { + if ( temp.Remaining > 0 ) + _remaining.Put(temp); + } + } + + 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; + } + + private ByteBuffer AllocateBuffer() + { + ByteBuffer buffer = ByteBuffer.Allocate(4096); + buffer.IsAutoExpand = true; + return buffer; + } + } +} diff --git a/qpid/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs b/qpid/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs new file mode 100644 index 0000000000..78276202d6 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/DemuxingProtocolCodecFactory.cs @@ -0,0 +1,387 @@ +/* + * + * 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 Apache.Qpid.Buffer; + +namespace Apache.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.GetHexDump()); + } + + 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/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs b/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs new file mode 100644 index 0000000000..5892673440 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoder.cs @@ -0,0 +1,56 @@ +/* + * + * 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 Apache.Qpid.Buffer; + +namespace Apache.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/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs b/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs new file mode 100644 index 0000000000..9e333d670f --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/IMessageDecoderFactory.cs @@ -0,0 +1,32 @@ +/* + * + * 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 Apache.Qpid.Codec.Demux +{ + public interface IMessageDecoderFactory + { + /// <summary> + /// Creates a new message decoder. + /// </summary> + IMessageDecoder NewDecoder(); + } +} + + diff --git a/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs b/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs new file mode 100644 index 0000000000..75ae23592b --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoder.cs @@ -0,0 +1,48 @@ +/* + * + * 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 Apache.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/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs b/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs new file mode 100644 index 0000000000..3001d1a963 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/IMessageEncoderFactory.cs @@ -0,0 +1,32 @@ +/* + * + * 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 Apache.Qpid.Codec.Demux +{ + public interface IMessageEncoderFactory + { + /// <summary> + /// Creates a new message encoder. + /// </summary> + IMessageEncoder NewEncoder(); + } +} + + diff --git a/qpid/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs b/qpid/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs new file mode 100644 index 0000000000..ab01864bc0 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Demux/MessageDecoderResult.cs @@ -0,0 +1,29 @@ +/* + * + * 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 Apache.Qpid.Codec.Demux +{ + public enum MessageDecoderResult + { + OK, NOT_OK, NEED_DATA + } +} + + diff --git a/qpid/dotnet/Qpid.Codec/IProtocolCodecFactory.cs b/qpid/dotnet/Qpid.Codec/IProtocolCodecFactory.cs new file mode 100644 index 0000000000..a26b91b16c --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/IProtocolCodecFactory.cs @@ -0,0 +1,37 @@ +/* + * + * 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 Apache.Qpid.Codec +{ + public interface IProtocolCodecFactory + { + IProtocolEncoder Encoder + { + get; + } + + IProtocolDecoder Decoder + { + get; + } + } +} + + diff --git a/qpid/dotnet/Qpid.Codec/IProtocolDecoder.cs b/qpid/dotnet/Qpid.Codec/IProtocolDecoder.cs new file mode 100644 index 0000000000..3cccb0f7da --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/IProtocolDecoder.cs @@ -0,0 +1,41 @@ +/* + * + * 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 Apache.Qpid.Buffer; + +namespace Apache.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/qpid/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs b/qpid/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs new file mode 100644 index 0000000000..77a1aea9db --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/IProtocolDecoderOutput.cs @@ -0,0 +1,35 @@ +/* + * + * 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 Apache.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/qpid/dotnet/Qpid.Codec/IProtocolEncoder.cs b/qpid/dotnet/Qpid.Codec/IProtocolEncoder.cs new file mode 100644 index 0000000000..a16f2ad9d6 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/IProtocolEncoder.cs @@ -0,0 +1,41 @@ +/* + * + * 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 Apache.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/qpid/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs b/qpid/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs new file mode 100644 index 0000000000..70f9be38dc --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/IProtocolEncoderOutput.cs @@ -0,0 +1,37 @@ +/* + * + * 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 Apache.Qpid.Buffer; + +namespace Apache.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/qpid/dotnet/Qpid.Codec/Properties/AssemblyInfo.cs b/qpid/dotnet/Qpid.Codec/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..5261a62ec5 --- /dev/null +++ b/qpid/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("Apache.Qpid.Codec")] +[assembly: AssemblyDescription("Built from svn revision number: ")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Apache Software Foundation")] +[assembly: AssemblyProduct("Apache.Qpid.Codec")] +[assembly: AssemblyCopyright("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("0.5.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/qpid/dotnet/Qpid.Codec/ProtocolCodecException.cs b/qpid/dotnet/Qpid.Codec/ProtocolCodecException.cs new file mode 100644 index 0000000000..49678d2c11 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/ProtocolCodecException.cs @@ -0,0 +1,49 @@ +/* + * + * 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.Runtime.Serialization; + +namespace Apache.Qpid.Codec +{ + [Serializable] + public class ProtocolCodecException : Exception + { + public ProtocolCodecException() : base() + { + } + + public ProtocolCodecException(string message) : base(message) + { + } + + public ProtocolCodecException(Exception cause) : base("Codec Exception", cause) + { + } + + protected ProtocolCodecException(SerializationInfo info, StreamingContext ctxt) + : base(info, ctxt) + { + } + } +} + + + diff --git a/qpid/dotnet/Qpid.Codec/ProtocolDecoderException.cs b/qpid/dotnet/Qpid.Codec/ProtocolDecoderException.cs new file mode 100644 index 0000000000..8e7e6da145 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/ProtocolDecoderException.cs @@ -0,0 +1,70 @@ +/* + * + * 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.Runtime.Serialization; + +namespace Apache.Qpid.Codec +{ + [Serializable] + public class ProtocolDecoderException : ProtocolCodecException + { + private string _hexdump; + + public ProtocolDecoderException() : base() + { + } + + public ProtocolDecoderException(string message) : base(message) + { + } + + public ProtocolDecoderException(Exception cause) : base(cause) + { + } + + protected ProtocolDecoderException(SerializationInfo info, StreamingContext ctxt) + : base(info, ctxt) + { + _hexdump = info.GetString("HexDump"); + } + + public string HexDump + { + get + { + return _hexdump; + } + set + { + _hexdump = value; + } + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("HexDump", _hexdump); + } + } +} + + + diff --git a/qpid/dotnet/Qpid.Codec/ProtocolEncoderException.cs b/qpid/dotnet/Qpid.Codec/ProtocolEncoderException.cs new file mode 100644 index 0000000000..ac565a308b --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/ProtocolEncoderException.cs @@ -0,0 +1,49 @@ +/* + * + * 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.Runtime.Serialization; + +namespace Apache.Qpid.Codec +{ + [Serializable] + public class ProtocolEncoderException : ProtocolCodecException + { + public ProtocolEncoderException() : base() + { + } + + public ProtocolEncoderException(string message) : base(message) + { + } + + public ProtocolEncoderException(Exception cause) : base(cause) + { + } + + protected ProtocolEncoderException(SerializationInfo info, StreamingContext ctxt) + : base(info, ctxt) + { + } + } +} + + + diff --git a/qpid/dotnet/Qpid.Codec/Qpid.Codec.csproj b/qpid/dotnet/Qpid.Codec/Qpid.Codec.csproj new file mode 100644 index 0000000000..a0217cffa3 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Qpid.Codec.csproj @@ -0,0 +1,82 @@ +<!--
+
+ 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.
+
+-->
+
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
+ <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>Apache.Qpid.Codec</RootNamespace>
+ <AssemblyName>Apache.Qpid.Codec</AssemblyName>
+ <FileUpgradeFlags>
+ </FileUpgradeFlags>
+ <OldToolsVersion>2.0</OldToolsVersion>
+ <UpgradeBackupLocation>
+ </UpgradeBackupLocation>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>..\bin\net-2.0\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\net-2.0\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="**\*.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>
diff --git a/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.cs b/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.cs new file mode 100644 index 0000000000..0a4ff10ff0 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolDecoderOutput.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; + +namespace Apache.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/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.cs b/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.cs new file mode 100644 index 0000000000..2e4224ef98 --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/Support/SimpleProtocolEncoderOutput.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; +using Apache.Qpid.Buffer; + +namespace Apache.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); + } + } +} diff --git a/qpid/dotnet/Qpid.Codec/default.build b/qpid/dotnet/Qpid.Codec/default.build new file mode 100644 index 0000000000..dd59df7d6a --- /dev/null +++ b/qpid/dotnet/Qpid.Codec/default.build @@ -0,0 +1,47 @@ +<?xml version="1.0"?> +<!-- + + 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. + +--> + +<project name="Apache.Qpid.Codec" default="build"> + <!-- + Properties that come from master build file + - build.dir: root directory for build + - build.debug: true if building debug release + - build.defines: variables to define during build + --> + + <target name="build"> + <csc target="library" + define="${build.defines}" + debug="${build.debug}" + output="${build.dir}/${project::get-name()}.dll"> + + <sources> + <include name="**/*.cs" /> + </sources> + <references> + <include name="${build.dir}/log4net.dll" /> + <include name="${build.dir}/Apache.Qpid.Buffer.dll" /> + </references> + </csc> + </target> +</project> + |