Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

OS::MessageQueue Class Reference

Class that represents a UNIX system message queue. More...

#include <MessageQueue.h>

Inheritance diagram for OS::MessageQueue:

Inheritance graph
[legend]
Collaboration diagram for OS::MessageQueue:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 MessageQueue (int queueKey, bool exclusive)
 Initializes the queue.
virtual long bufferSize (void)
virtual long priority (void)
virtual void priority (long level)
virtual void close (void)
 Close the message queue.
virtual int key (void)
 Get the message creation key Get the hash key used to create this queue.
virtual int readInto (ByteArray *buffer, long startIndex, long count)
virtual int writeFrom (ByteArray *buffer, long startIndex, long count)
virtual void seekTo (long position)
virtual bool isActive (void)
 Answer the status of the queue.

Static Public Member Functions

static MessageQueueopen (void)
static MessageQueueopen (String *key, bool exclusive=false)
static MessageQueueopen (bool exclusive)

Protected Attributes

int queueid
 The system-wide ID of the message queue.
int queueCreationKey
 The key this queue has been created with.
bool active
 Status of the message queue accessor.
int _priority
 Message priority level.

Detailed Description

Class that represents a UNIX system message queue.

A message queue is part of the UNIX interprocess communication.

Definition at line 37 of file MessageQueue.h.


Constructor & Destructor Documentation

MessageQueue::MessageQueue int  queueKey,
bool  exclusive
 

Initializes the queue.

Creates a new message queue with the given key. When exclusive is false, the queue is accessed even if it already exist. When it is set to true and the queue exists, an error is raised.

  • queueKey - the ID to allocate
  • exclusive - whether to find an unused queue or not

Definition at line 42 of file MessageQueue.cc.

References _priority, active, queueCreationKey, and queueid.

Referenced by open().

00043 {
00044     int flags = 0664 | IPC_CREAT;
00045 
00046     if (exclusive)
00047         flags = flags | IPC_EXCL;
00048     queueid = msgget(queueKey, flags);
00049     queueCreationKey = queueKey;
00050     if (queueid >= 0) active = true;
00051     _priority = 1;
00052 }


Member Function Documentation

long MessageQueue::bufferSize void   )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 70 of file MessageQueue.cc.

References MAXBUFFERLEN.

00071 {
00072     return MAXBUFFERLEN;
00073 }

void MessageQueue::close void   )  [virtual]
 

Close the message queue.

Deallocates the message queue resource. It must be called to physically free the message queue from the system kernel IPC tables.

Reimplemented from Core::IOAccessor.

Definition at line 86 of file MessageQueue.cc.

References active, and queueid.

00087 {
00088     if (active)
00089         msgctl(queueid, IPC_RMID, NULL);
00090     active = false;
00091 }

virtual bool OS::MessageQueue::isActive void   )  [inline, virtual]
 

Answer the status of the queue.

Definition at line 92 of file MessageQueue.h.

References active.

00093       { return active; }

virtual int OS::MessageQueue::key void   )  [inline, virtual]
 

Get the message creation key Get the hash key used to create this queue.

Returns:
Hash key to be used to create the remote side of the queue.

Definition at line 80 of file MessageQueue.h.

References queueCreationKey.

00081       { return queueCreationKey; }

MessageQueue * MessageQueue::open bool  exclusive  )  [static]
 

Definition at line 59 of file MessageQueue.cc.

References open().

00060 {
00061     return MessageQueue::open(new String("stlib-MessageQueue"), exclusive);
00062 }

MessageQueue * MessageQueue::open String key,
bool  exclusive = false
[static]
 

Definition at line 64 of file MessageQueue.cc.

References Core::String::asCString(), getkey(), and MessageQueue().

00065 {
00066     return new MessageQueue(getkey(key->asCString()), exclusive);
00067 }

MessageQueue * MessageQueue::open void   )  [static]
 

Definition at line 54 of file MessageQueue.cc.

Referenced by open().

00055 {
00056     return MessageQueue::open(new String("stlib-MessageQueue"));
00057 }

void MessageQueue::priority long  level  )  [virtual]
 

Definition at line 75 of file MessageQueue.cc.

References _priority.

00076 {
00077     _priority = level;
00078 }

long MessageQueue::priority void   )  [virtual]
 

Definition at line 80 of file MessageQueue.cc.

References _priority.

00081 {
00082     return _priority;
00083 }

int MessageQueue::readInto ByteArray buffer,
long  startIndex,
long  count
[virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 94 of file MessageQueue.cc.

References MAXBUFFERLEN, queueid, and Core::ByteArray::replace().

00095 {
00096     int bytes;
00097     struct msgbuf {
00098         long mtype;
00099         unsigned char data[MAXBUFFERLEN];
00100     } buf;
00101 
00102     if (queueid < 0) return 0;
00103     if (count > MAXBUFFERLEN) count = MAXBUFFERLEN;
00104 
00105     bytes = msgrcv(queueid, &buf, count, 0, MSG_NOERROR);
00106     if (bytes > 0)
00107         buffer->replace(startIndex, startIndex+bytes, buf.data);
00108     return bytes;
00109 }

void MessageQueue::seekTo long  position  )  [virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 133 of file MessageQueue.cc.

References Core::Object::shouldNotImplement().

00134 {
00135     shouldNotImplement(new String(__PRETTY_FUNCTION__));
00136 }

int MessageQueue::writeFrom ByteArray buffer,
long  startIndex,
long  count
[virtual]
 

Reimplemented from Core::IOAccessor.

Definition at line 111 of file MessageQueue.cc.

References _priority, Core::Object::error(), MAXBUFFERLEN, queueid, and Core::ByteArray::rawBytesReadOnly().

00112 {
00113     struct msgbuf {
00114         long mtype;
00115         unsigned char mtext[MAXBUFFERLEN];
00116     } buf;
00117 
00118     if (queueid < 0 || count < 1)
00119         return 0;
00120 
00121     if (count > MAXBUFFERLEN) count = MAXBUFFERLEN;
00122     buf.mtype = _priority;
00123     /* Must memcpy, pointer doesn't work. */
00124     memcpy(buf.mtext, &(buffer->rawBytesReadOnly())[startIndex], count);
00125     int error = msgsnd(queueid, &buf, count, 0);
00126     if (error < 0)
00127         perror(__PRETTY_FUNCTION__);
00128 
00129     return (error < 0) ? 0 : count;
00130 }


Member Data Documentation

int OS::MessageQueue::_priority [protected]
 

Message priority level.

Definition at line 47 of file MessageQueue.h.

Referenced by MessageQueue(), priority(), and writeFrom().

bool OS::MessageQueue::active [protected]
 

Status of the message queue accessor.

Definition at line 45 of file MessageQueue.h.

Referenced by close(), isActive(), and MessageQueue().

int OS::MessageQueue::queueCreationKey [protected]
 

The key this queue has been created with.

Definition at line 43 of file MessageQueue.h.

Referenced by key(), and MessageQueue().

int OS::MessageQueue::queueid [protected]
 

The system-wide ID of the message queue.

Definition at line 41 of file MessageQueue.h.

Referenced by close(), MessageQueue(), readInto(), and writeFrom().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:52:20 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2