diff options
Diffstat (limited to 'qpid/dotnet/client-010/client/transport/util')
7 files changed, 808 insertions, 0 deletions
diff --git a/qpid/dotnet/client-010/client/transport/util/ByteEncoder.cs b/qpid/dotnet/client-010/client/transport/util/ByteEncoder.cs new file mode 100644 index 0000000000..873ca75688 --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/ByteEncoder.cs @@ -0,0 +1,218 @@ +/* +* +* 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 org.apache.qpid.transport.util +{ + public static class ByteEncoder + { + #region Endian conversion helper routines + /// <summary> + /// Returns the value encoded in Big Endian (PPC, XDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Big-endian encoded value.</returns> + public static Int32 GetBigEndian(Int32 value) + { + if (BitConverter.IsLittleEndian) + { + return SwapByteOrder(value); + } + return value; + } + + /// <summary> + /// Returns the value encoded in Big Endian (PPC, XDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Big-endian encoded value.</returns> + public static UInt16 GetBigEndian(UInt16 value) + { + if (BitConverter.IsLittleEndian) + { + return SwapByteOrder(value); + } + return value; + } + + /// <summary> + /// Returns the value encoded in Big Endian (PPC, XDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Big-endian encoded value.</returns> + public static UInt32 GetBigEndian(UInt32 value) + { + if (BitConverter.IsLittleEndian) + { + return SwapByteOrder(value); + } + return value; + } + + /// <summary> + /// Returns the value encoded in Big Endian (PPC, XDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Big-endian encoded value.</returns> + public static long GetBigEndian(long value) + { + if (BitConverter.IsLittleEndian) + { + return SwapByteOrder(value); + } + return value; + } + + public static double GetBigEndian(double value) + { + if (BitConverter.IsLittleEndian) + { + return SwapByteOrder(value); + } + return value; + } + + /// <summary> + /// Returns the value encoded in Little Endian (x86, NDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Little-endian encoded value.</returns> + public static Int32 GetLittleEndian(Int32 value) + { + if (BitConverter.IsLittleEndian) + { + return value; + } + return SwapByteOrder(value); + } + + /// <summary> + /// Returns the value encoded in Little Endian (x86, NDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Little-endian encoded value.</returns> + public static UInt32 GetLittleEndian(UInt32 value) + { + if (BitConverter.IsLittleEndian) + { + return value; + } + return SwapByteOrder(value); + } + + /// <summary> + /// Returns the value encoded in Little Endian (x86, NDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Little-endian encoded value.</returns> + public static UInt16 GetLittleEndian(UInt16 value) + { + if (BitConverter.IsLittleEndian) + { + return value; + } + return SwapByteOrder(value); + } + + /// <summary> + /// Returns the value encoded in Little Endian (x86, NDR) format. + /// </summary> + /// <param name="value">Value to encode.</param> + /// <returns>Little-endian encoded value.</returns> + public static long GetLittleEndian(long value) + { + if (BitConverter.IsLittleEndian) + { + return value; + } + return SwapByteOrder(value); + } + + public static double GetLittleEndian(double value) + { + if (BitConverter.IsLittleEndian) + { + return value; + } + return SwapByteOrder(value); + } + + /// <summary> + /// Swaps the Byte order of an <see cref="Int32"/>. + /// </summary> + /// <param name="value"><see cref="Int32"/> to swap the bytes of.</param> + /// <returns>Byte order swapped <see cref="Int32"/>.</returns> + private static Int32 SwapByteOrder(Int32 value) + { + Int32 swapped = (Int32)((0x000000FF) & (value >> 24) + | (0x0000FF00) & (value >> 8) + | (0x00FF0000) & (value << 8) + | (0xFF000000) & (value << 24)); + return swapped; + } + + /// <summary> + /// Swaps the byte order of a <see cref="UInt16"/>. + /// </summary> + /// <param name="value"><see cref="UInt16"/> to swap the bytes of.</param> + /// <returns>Byte order swapped <see cref="UInt16"/>.</returns> + private static UInt16 SwapByteOrder(UInt16 value) + { + return (UInt16)((0x00FF & (value >> 8)) + | (0xFF00 & (value << 8))); + } + + /// <summary> + /// Swaps the byte order of a <see cref="UInt32"/>. + /// </summary> + /// <param name="value"><see cref="UInt32"/> to swap the bytes of.</param> + /// <returns>Byte order swapped <see cref="UInt32"/>.</returns> + private static UInt32 SwapByteOrder(UInt32 value) + { + UInt32 swapped = ((0x000000FF) & (value >> 24) + | (0x0000FF00) & (value >> 8) + | (0x00FF0000) & (value << 8) + | (0xFF000000) & (value << 24)); + return swapped; + } + + /// <summary> + /// Swaps the byte order of a <see cref="Double"/> (double precision IEEE 754) + /// </summary> + /// <param name="value"><see cref="Double"/> to swap.</param> + /// <returns>Byte order swapped <see cref="Double"/> value.</returns> + private static long SwapByteOrder(long value) + { + Byte[] buffer = BitConverter.GetBytes(value); + Array.Reverse(buffer, 0, buffer.Length); + return BitConverter.ToInt64(buffer, 0); + } + + private static double SwapByteOrder(double value) + { + Byte[] buffer = BitConverter.GetBytes(value); + Array.Reverse(buffer, 0, buffer.Length); + return BitConverter.ToDouble(buffer,0) ; + } + #endregion + } + +} diff --git a/qpid/dotnet/client-010/client/transport/util/CircularBuffer.cs b/qpid/dotnet/client-010/client/transport/util/CircularBuffer.cs new file mode 100644 index 0000000000..00d7b20d4c --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/CircularBuffer.cs @@ -0,0 +1,132 @@ +/* +* +* 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.Threading; + +namespace common.org.apache.qpid.transport.util +{ + public class CircularBuffer<T> + { + private readonly T[] buffer; + private Int32 nrp, nwp; + private readonly Int32 len; + private Int32 countValue; + private readonly Int32 add; + + + /// <summary> + /// Constructor creates N=len element + /// Circular Buffer that olds MemoryStream + /// </summary> + public CircularBuffer(Int32 len) + { + buffer = new T[len]; + this.len = len; + add = 1 - len; + nrp = 0; + nwp = 0; + countValue = 0; + } + + + public void Enqueue(T t) + { + lock (this) + { + if (countValue >= (len - 1)) + { + // wait for room to be available + Monitor.Wait(this); + } + bool notifyDequeue = countValue <= 0; + Load(t); + if (notifyDequeue) //notifyDequeue) + { + Monitor.PulseAll(this); + } + } + } + + + public T Dequeue() + { + lock (this) + { + if (countValue <= 0) + { + Monitor.Wait(this); + } + bool notifyEnqueue = countValue >= (len - 1); + T temp = Get(); + if (notifyEnqueue) //notifyEnqueue) + { + Monitor.PulseAll(this); + } + return temp; + } + } + + public void Close() + { + nrp = 0; + nwp = 0; + countValue = 0; + Array.Clear(buffer, 0, len); + lock (this) + { + Monitor.PulseAll(this); + } + } + + #region Private Support Functions + + private void Load(T t) + { + Int32 i = nwp; + buffer[i] = t; + i += add; + if (i < 0) i += len; + nwp = i; + UpdateCount(); + } + + private void UpdateCount() + { + countValue = nwp - nrp; + if (countValue <= 0 ) + countValue += len; // modulo buffer size + } + + private T Get() + { + Int32 i = nrp; + T temp = buffer[i]; + i += add; + if (i < 0) i += len; + nrp = i; + countValue--; + return (temp); + } + + #endregion + } +} diff --git a/qpid/dotnet/client-010/client/transport/util/Functions.cs b/qpid/dotnet/client-010/client/transport/util/Functions.cs new file mode 100644 index 0000000000..eee3848386 --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/Functions.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. +* +*/ + +namespace org.apache.qpid.transport.util +{ + + /// <summary> + /// Functions + /// </summary> + + public class Functions + { + public static sbyte Lsb(int i) + { + return (sbyte) (0xFF & i); + } + + public static sbyte Lsb(long l) + { + return (sbyte) (0xFF & l); + } + } +} diff --git a/qpid/dotnet/client-010/client/transport/util/Logger.cs b/qpid/dotnet/client-010/client/transport/util/Logger.cs new file mode 100644 index 0000000000..f889fe2aab --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/Logger.cs @@ -0,0 +1,114 @@ +/* +* +* 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; + +namespace org.apache.qpid.transport.util +{ + + /// <summary> Logger + /// + /// </summary> + + public sealed class Logger + { + private readonly ILog log; + + public static Logger Get(Type type) + { + return new Logger(LogManager.GetLogger(type)); + } + + private Logger(ILog log) + { + this.log = log; + } + + public bool IsDebugEnabled() + { + return log.IsDebugEnabled; + } + + public void Debug(String message, params Object[] args) + { + if (log.IsDebugEnabled) + { + log.Debug(String.Format(message, args)); + } + } + + public void Debug(Exception t, String message, params Object[] args) + { + if (log.IsDebugEnabled) + { + log.Debug(String.Format(message, args), t); + } + } + + public void Error(String message, params Object[] args) + { + if (log.IsErrorEnabled) + { + log.Error(String.Format(message, args)); + } + } + + public void Error(Exception t, String message, params Object[] args) + { + if (log.IsErrorEnabled) + { + log.Error(String.Format(message, args), t); + } + } + + public void Warn(String message, params Object[] args) + { + if (log.IsWarnEnabled) + { + log.Warn(String.Format(message, args)); + } + } + + public void Warn(Exception t, String message, params Object[] args) + { + if (log.IsWarnEnabled) + { + log.Warn(String.Format(message, args), t); + } + } + + public void Info(String message, params Object[] args) + { + if (log.IsInfoEnabled) + { + log.Info(String.Format(message, args)); + } + } + + public void Info(Exception t, String message, params Object[] args) + { + if (log.IsInfoEnabled) + { + log.Info(String.Format(message, args), t); + } + } + } +} diff --git a/qpid/dotnet/client-010/client/transport/util/ResultFuture.cs b/qpid/dotnet/client-010/client/transport/util/ResultFuture.cs new file mode 100644 index 0000000000..0de2b27656 --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/ResultFuture.cs @@ -0,0 +1,80 @@ +/* + * + * 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.Threading; +using org.apache.qpid.transport; +using org.apache.qpid.transport.util; + +namespace common.org.apache.qpid.transport.util +{ + public class ResultFuture : IFuture + { + const long _timeout = 60000; + private Struct _result; + private Session _session; + private static readonly Logger log = Logger.Get(typeof(ResultFuture)); + + public Struct Get(long timeout) + { + lock (this) + { + DateTime start = DateTime.Now; + long elapsed = 0; + while (! _session.IsClosed && timeout - elapsed > 0 && _result == null) + { + log.Debug("{0} waiting for result: {1}", _session, this ); + Monitor.Wait(this, (int) (timeout - elapsed)); + elapsed = (long) (DateTime.Now.Subtract(start)).TotalMilliseconds; + } + } + if( _session.IsClosed ) + { + throw new SessionException(_session.GetExceptions()); + } + return _result; + } + + public Struct Result + { + get { return Get(_timeout); } + set + { + lock (this) + { + _result = value; + Monitor.PulseAll(this); + } + } + } + + public Session Session + { + set { _session = value; } + } + + public override String ToString() + { + return String.Format("Future({0})", _result); + } + + } +} diff --git a/qpid/dotnet/client-010/client/transport/util/Serial.cs b/qpid/dotnet/client-010/client/transport/util/Serial.cs new file mode 100644 index 0000000000..874097084a --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/Serial.cs @@ -0,0 +1,94 @@ +/* +* +* 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 org.apache.qpid.transport.util +{ + /// <summary> + /// This class provides basic serial number comparisons as defined in + /// RFC 1982. + /// </summary> + public class Serial + { + /// + /// + ///Compares two numbers using serial arithmetic. + /// + /// param s1 the first serial number + /// param s2 the second serial number + /// + /// return a negative integer, zero, or a positive integer as the + /// first argument is less than, equal to, or greater than the + /// second + /// + public static int Compare(int s1, int s2) + { + return s1 - s2; + } + + public static bool Lt(int s1, int s2) + { + return Compare(s1, s2) < 0; + } + + public static bool Le(int s1, int s2) + { + return Compare(s1, s2) <= 0; + } + + public static bool Gt(int s1, int s2) + { + return Compare(s1, s2) > 0; + } + + public static bool Ge(int s1, int s2) + { + return Compare(s1, s2) >= 0; + } + + public static bool Eq(int s1, int s2) + { + return s1 == s2; + } + + public static int Min(int s1, int s2) + { + if (Lt(s1, s2)) + { + return s1; + } + else + { + return s2; + } + } + + public static int Max(int s1, int s2) + { + if (Gt(s1, s2)) + { + return s1; + } + else + { + return s2; + } + } + } +} diff --git a/qpid/dotnet/client-010/client/transport/util/UUID.cs b/qpid/dotnet/client-010/client/transport/util/UUID.cs new file mode 100644 index 0000000000..07a3d267a5 --- /dev/null +++ b/qpid/dotnet/client-010/client/transport/util/UUID.cs @@ -0,0 +1,129 @@ +/* +* +* 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 org.apache.qpid.transport.util +{ + public class UUID + { + private long _mostSigBits; + private long _leastSigBits; + private static readonly Random _random = new Random(); + private static readonly object _randomLock = new object(); + + + public UUID(long mostSigBits, long leastSigBits) + { + _mostSigBits = mostSigBits; + _leastSigBits = leastSigBits; + } + + public long MostSignificantBits + { + get { return _mostSigBits; } + set { _mostSigBits = value; } + } + + public long LeastSignificantBits + { + get { return _leastSigBits; } + set { _leastSigBits = value; } + } + + internal UUID(byte[] r) + { + MostSignificantBits = 0; + LeastSignificantBits = 0; + for (int i = 0; i < 8; i++) + MostSignificantBits = (MostSignificantBits << 8) | (r[i] & 0xff); + for (int i = 8; i < 16; i++) + LeastSignificantBits = (LeastSignificantBits << 8) | (r[i] & 0xff); + } + + public static UUID RandomUuid() + { + byte[] randomBytes = new byte[16]; + lock (_randomLock) + { + _random.NextBytes(randomBytes); + } + + randomBytes[6] &= 0x0f; + randomBytes[6] |= 0x40; + randomBytes[8] &= 0x3f; + randomBytes[8] |= 0x80; + + return new UUID(randomBytes); + } + + + public override String ToString() + { + return (Digits(_mostSigBits >> 32, 8) + "-" + + Digits(_mostSigBits >> 16, 4) + "-" + + Digits(_mostSigBits, 4) + "-" + + Digits(_leastSigBits >> 48, 4) + "-" + + Digits(_leastSigBits, 12)); + } + + private static String Digits(long val, int digits) + { + long hi = 1L << (digits * 4); + return Convert.ToString((hi | (val & (hi - 1))), 16); + } + + #region equality + public bool Equals(UUID other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return other._mostSigBits == _mostSigBits && other._leastSigBits == _leastSigBits; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != typeof (UUID)) return false; + return Equals((UUID) obj); + } + + public override int GetHashCode() + { + unchecked + { + return (_mostSigBits.GetHashCode()*397) ^ _leastSigBits.GetHashCode(); + } + } + + public static bool operator ==(UUID left, UUID right) + { + return Equals(left, right); + } + + public static bool operator !=(UUID left, UUID right) + { + return !Equals(left, right); + } + #endregion + } +} |