Qpid .NET Messaging API  0.22
Message.h
Go to the documentation of this file.
00001 /*
00002 * Licensed to the Apache Software Foundation (ASF) under one
00003 * or more contributor license agreements.  See the NOTICE file
00004 * distributed with this work for additional information
00005 * regarding copyright ownership.  The ASF licenses this file
00006 * to you under the Apache License, Version 2.0 (the
00007 * "License"); you may not use this file except in compliance
00008 * with the License.  You may obtain a copy of the License at
00009 *
00010 *   http://www.apache.org/licenses/LICENSE-2.0
00011 *
00012 * Unless required by applicable law or agreed to in writing,
00013 * software distributed under the License is distributed on an
00014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015 * KIND, either express or implied.  See the License for the
00016 * specific language governing permissions and limitations
00017 * under the License.
00018 */
00019 #pragma once
00020 
00021 #include <windows.h>
00022 #include <msclr\lock.h>
00023 #include <oletx2xa.h>
00024 #include <string>
00025 #include <limits>
00026 
00027 #include "qpid/messaging/Message.h"
00028 
00029 #include "QpidMarshal.h"
00030 #include "Address.h"
00031 #include "Duration.h"
00032 #include "TypeTranslator.h"
00033 
00034 namespace Org {
00035 namespace Apache {
00036 namespace Qpid {
00037 namespace Messaging {
00038 
00039     ref class Address;
00040     ref class Duration;
00041 
00045 
00046     public ref class Message
00047     {
00048 
00049     private:
00050         // The kept object in the Messaging C++ DLL
00051         ::qpid::messaging::Message * nativeObjPtr;
00052 
00053         // per-instance lock object
00054         System::Object ^ privateLock;
00055 
00056         // Disallow use after object is destroyed
00057         void ThrowIfDisposed();
00058 
00059     public:
00060         // Create empty message
00061         Message();
00062 
00063         // Create from String
00064         Message(System::String ^ theStr);
00065 
00066         // Create from object
00067         Message(System::Object ^ theValue);
00068 
00069         // Create from byte array
00070         Message(array<System::Byte> ^ bytes);
00071 
00072         // Create from byte array slice
00073         Message(array<System::Byte> ^ bytes, int offset, int size);
00074 
00075         // System destructor/finalizer entry points
00076         ~Message();
00077         !Message();
00078 
00079         // Copy constructor
00080         Message(const Message ^ message);
00081         Message(const Message % message);
00082 
00083         // unmanaged clone
00084         Message(const ::qpid::messaging::Message & msgp);
00085 
00086         // assignment operator
00087         Message % operator=(const Message % rhs)
00088         {
00089             msclr::lock lk(privateLock);
00090             ThrowIfDisposed();
00091 
00092             if (this == %rhs)
00093             {
00094                 // Self assignment, do nothing
00095             }
00096             else
00097             {
00098                 if (NULL != nativeObjPtr)
00099                     delete nativeObjPtr;
00100                 nativeObjPtr = new ::qpid::messaging::Message(
00101                     *(const_cast<Message %>(rhs).NativeMessage) );
00102             }
00103             return *this;
00104         }
00105 
00106         //
00107         // IsDisposed
00108         //
00109         property bool IsDisposed
00110         {
00111             bool get()
00112             {
00113                 return NULL == nativeObjPtr;
00114             }
00115         }
00116 
00117 
00118         //
00119         // NativeMessage
00120         //
00121         property ::qpid::messaging::Message * NativeMessage
00122         {
00123             ::qpid::messaging::Message * get ()
00124             {
00125                 return nativeObjPtr;
00126             }
00127         }
00128 
00129         //
00130         // ReplyTo
00131         //
00132         property Address ^ ReplyTo
00133         {
00134             void set (Address ^ address)
00135             {
00136                 msclr::lock lk(privateLock);
00137                 ThrowIfDisposed();
00138 
00139                 nativeObjPtr->setReplyTo(*(address->NativeAddress));
00140             }
00141 
00142             Address ^ get ()
00143             {
00144                 msclr::lock lk(privateLock);
00145                 ThrowIfDisposed();
00146 
00147                 const ::qpid::messaging::Address & addrp =
00148                     nativeObjPtr->::qpid::messaging::Message::getReplyTo();
00149 
00150                 return gcnew Address(addrp);
00151             }
00152         }
00153 
00154         //
00155         // Subject
00156         //
00157         property System::String ^ Subject
00158         {
00159             void set (System::String ^ subject)
00160             {
00161                 msclr::lock lk(privateLock);
00162                 ThrowIfDisposed();
00163 
00164                 nativeObjPtr->setSubject(QpidMarshal::ToNative(subject));
00165             }
00166 
00167             System::String ^ get ()
00168             {
00169                 msclr::lock lk(privateLock);
00170                 ThrowIfDisposed();
00171 
00172                 return gcnew String(nativeObjPtr->getSubject().c_str());
00173             }
00174         }
00175 
00176 
00177         //
00178         // ContentType
00179         //
00180         property System::String ^ ContentType
00181         {
00182             void set (System::String ^ ct)
00183             {
00184                 msclr::lock lk(privateLock);
00185                 ThrowIfDisposed();
00186 
00187                 nativeObjPtr->setContentType(QpidMarshal::ToNative(ct));
00188             }
00189 
00190             System::String ^ get ()
00191             {
00192                 msclr::lock lk(privateLock);
00193                 ThrowIfDisposed();
00194 
00195                 return gcnew String(nativeObjPtr->::qpid::messaging::Message::getContentType().c_str());
00196             }
00197         }
00198 
00199 
00200         //
00201         // MessageId
00202         //
00203         property System::String ^ MessageId
00204         {
00205             void set (System::String ^ messageId)
00206             {
00207                 msclr::lock lk(privateLock);
00208                 ThrowIfDisposed();
00209 
00210                 nativeObjPtr->setMessageId(QpidMarshal::ToNative(messageId));
00211             }
00212 
00213             System::String ^ get ()
00214             {
00215                 msclr::lock lk(privateLock);
00216                 ThrowIfDisposed();
00217 
00218                 return gcnew String(nativeObjPtr->getMessageId().c_str());
00219             }
00220         }
00221 
00222 
00223         //
00224         // UserId
00225         //
00226         property System::String ^ UserId
00227         {
00228             void set (System::String ^ uId)
00229             {
00230                 msclr::lock lk(privateLock);
00231                 ThrowIfDisposed();
00232 
00233                 nativeObjPtr->setUserId(QpidMarshal::ToNative(uId));
00234             }
00235 
00236             System::String ^ get ()
00237             {
00238                 msclr::lock lk(privateLock);
00239                 ThrowIfDisposed();
00240 
00241                 return gcnew String(nativeObjPtr->getUserId().c_str());
00242             }
00243         }
00244 
00245 
00246         //
00247         // CorrelationId
00248         //
00249         property System::String ^ CorrelationId
00250         {
00251             void set (System::String ^ correlationId)
00252             {
00253                 msclr::lock lk(privateLock);
00254                 ThrowIfDisposed();
00255 
00256                 nativeObjPtr->setCorrelationId(QpidMarshal::ToNative(correlationId));
00257             }
00258 
00259             System::String ^ get ()
00260             {
00261                 msclr::lock lk(privateLock);
00262                 ThrowIfDisposed();
00263 
00264                 return gcnew String(nativeObjPtr->getCorrelationId().c_str());
00265             }
00266         }
00267 
00268 
00269         //
00270         // Priority
00271         //
00272         property unsigned char Priority
00273         {
00274             void set (unsigned char priority)
00275             {
00276                 msclr::lock lk(privateLock);
00277                 ThrowIfDisposed();
00278 
00279                 nativeObjPtr->setPriority(priority);
00280             }
00281 
00282             unsigned char get ()
00283             {
00284                 msclr::lock lk(privateLock);
00285                 ThrowIfDisposed();
00286 
00287                 return nativeObjPtr->getPriority();
00288             }
00289         }
00290 
00291 
00292         //
00293         // Ttl
00294         //
00295         property Duration ^ Ttl
00296         {
00297             void set (Duration ^ ttl)
00298             {
00299                 msclr::lock lk(privateLock);
00300                 ThrowIfDisposed();
00301 
00302                 ::qpid::messaging::Duration dur(ttl->Milliseconds);
00303 
00304                 nativeObjPtr->setTtl(dur);
00305             }
00306 
00307             Duration ^ get ()
00308             {
00309                 msclr::lock lk(privateLock);
00310                 ThrowIfDisposed();
00311 
00312                 Duration ^ dur = gcnew Duration(nativeObjPtr->getTtl().getMilliseconds());
00313 
00314                 return dur;
00315             }
00316         }
00317 
00318         //
00319         // Durable
00320         //
00321         property bool Durable
00322         {
00323             void set (bool durable)
00324             {
00325                 msclr::lock lk(privateLock);
00326                 ThrowIfDisposed();
00327 
00328                 nativeObjPtr->setDurable(durable);
00329             }
00330 
00331             bool get ()
00332             {
00333                 msclr::lock lk(privateLock);
00334                 ThrowIfDisposed();
00335 
00336                 return nativeObjPtr->getDurable();
00337             }
00338         }
00339 
00340         //
00341         // Redelivered
00342         //
00343         property bool Redelivered
00344         {
00345             bool get ()
00346             {
00347                 msclr::lock lk(privateLock);
00348                 ThrowIfDisposed();
00349 
00350                 return nativeObjPtr->getRedelivered();
00351             }
00352 
00353             void set (bool redelivered)
00354             {
00355                 msclr::lock lk(privateLock);
00356                 ThrowIfDisposed();
00357 
00358                 nativeObjPtr->setRedelivered(redelivered);
00359             }
00360         }
00361 
00362         //
00363         // Property
00364         //
00365         void Message::SetProperty(System::String ^ name, System::Object ^ value);
00366 
00367         //
00368         // Properties
00369         //
00370         property System::Collections::Generic::Dictionary<
00371             System::String^, System::Object^> ^ Properties
00372         {
00373             System::Collections::Generic::Dictionary<
00374                 System::String^, System::Object^> ^ get ()
00375             {
00376                 msclr::lock lk(privateLock);
00377                 ThrowIfDisposed();
00378 
00379                 ::qpid::types::Variant::Map map;
00380 
00381                 map = nativeObjPtr->getProperties();
00382 
00383                 System::Collections::Generic::Dictionary<
00384                     System::String^, System::Object^> ^ dict =
00385                     gcnew System::Collections::Generic::Dictionary<
00386                     System::String^, System::Object^> ;
00387 
00388 
00389                 TypeTranslator::NativeToManaged(map, dict);
00390 
00391                 return dict;
00392             }
00393 
00394 
00395             void set (System::Collections::Generic::Dictionary<
00396                 System::String^, System::Object^> ^ properties)
00397             {
00398                 msclr::lock lk(privateLock);
00399                 ThrowIfDisposed();
00400 
00401                 for each (System::Collections::Generic::KeyValuePair
00402                     <System::String^, System::Object^> kvp in properties)
00403                 {
00404                     SetProperty(kvp.Key, kvp.Value);
00405                 }
00406             }
00407         }
00408 
00409 
00410         void SetContent(System::String ^ content);
00411 
00412         void SetContent(cli::array<System::Byte> ^ bytes);
00413 
00414         void SetContent(cli::array<System::Byte> ^ bytes, int offset, int size);
00415 
00416         //TODO:: void setContent(Bytes{} bytes, offset, length);
00417 
00418         // get content as string
00419         System::String ^ GetContent();
00420 
00421         // get content as dictionary
00422         void GetContent(System::Collections::Generic::Dictionary<
00423             System::String^,
00424             System::Object^> ^ dict);
00425 
00426         // get content as map
00427         void GetContent(System::Collections::ObjectModel::Collection<
00428             System::Object^> ^);
00429 
00430         // get content as bytes
00431         void GetContent(cli::array<System::Byte> ^ arr);
00432 
00433         //
00434         // ContentSize
00435         //
00436         property System::UInt64 ContentSize
00437         {
00438             System::UInt64 get ()
00439             {
00440                 msclr::lock lk(privateLock);
00441                 ThrowIfDisposed();
00442 
00443                 return nativeObjPtr->getContentSize();
00444             }
00445         }
00446 
00447 
00448         // A message has been returned to managed code through GetContent().
00449         // Display the content of that System::Object as a string.
00450         System::String ^ AsString(System::Object ^ obj);
00451 
00452         System::String ^ MapAsString(System::Collections::Generic::Dictionary<
00453             System::String^, System::Object^> ^ dict);
00454 
00455         System::String ^ ListAsString(System::Collections::ObjectModel::Collection<
00456             System::Object^> ^ list);
00457 
00458         //TODO: EncodingException
00459 
00460         // Note: encode/decode functions are in TypeTranslator
00461     };
00462 }}}}