RexxQueueMethods.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /*********************************************************************/
39 /* */
40 /* Function: Queue support routines */
41 /* */
42 /*********************************************************************/
43 
44 #include <string.h> /* Get strcpy, strcat, etc. */
45 #include <stdlib.h>
46 
47 #include "RexxCore.h" /* global REXX declarations */
48 #include "StringClass.hpp"
49 
50 /********************************************************************************************/
51 /* Rexx_query_queue */
52 /********************************************************************************************/
53 RexxMethod0(size_t, rexx_query_queue)
54 {
55  size_t count = 0; /* count of lines */
56 
57  /* get the queue name */
58  RexxObjectPtr queue_name = (RexxStringObject)context->GetObjectVariable("NAMED_QUEUE");
59  /* query the queue */
60  RexxQueryQueue(context->ObjectToStringValue(queue_name), &count);
61 
62  return count;
63 }
64 
65 /********************************************************************************************/
66 /* Rexx_pull_queue */
67 /********************************************************************************************/
68 RexxMethod0(RexxObjectPtr, rexx_pull_queue)
69 {
70  RXSTRING buf; /* pulled line buffer */
71  RexxReturnCode rc; /* pull return code */
72 
73  /* get the queue name */
74  RexxObjectPtr queue_name = context->GetObjectVariable("NAMED_QUEUE");
75 
76  buf.strptr = NULL; /* ask for a returned buffer */
77  buf.strlength = 0;
78  /* pull a line */
79  rc = RexxPullFromQueue(context->ObjectToStringValue(queue_name), &buf, NULL, RXQUEUE_NOWAIT);
80 
81  if (!rc)
82  { /* get a pulled line? */
83  RexxObjectPtr result = context->NewString(buf.strptr, buf.strlength);
84  if (buf.strptr != OREF_NULL)
85  {
87  }
88  return result;
89  }
90  return context->Nil(); /* give back a failure */
91 }
92 
93 /********************************************************************************************/
94 /* Rexx_linein_queue */
95 /********************************************************************************************/
96 RexxMethod0(RexxObjectPtr, rexx_linein_queue)
97 {
98  RexxReturnCode rc; /* pull return code */
99  RexxObjectPtr queue_name; /* current queue name */
100  RXSTRING buf;
101 
102  /* get the queue name */
103  queue_name = context->GetObjectVariable("NAMED_QUEUE");
104 
105  buf.strptr = NULL; /* ask for a returned buffer */
106  buf.strlength = 0;
107 
108  // since we don't know how long we'll be waiting here, turn off the
109  // guard so we don't lock up other threads.
110  context->SetGuardOff();
111  /* pull a line */
112  rc = RexxPullFromQueue(context->ObjectToStringValue(queue_name), &buf, NULL, RXQUEUE_WAIT);
113 
114  if (!rc) /* get a pulled line? */
115  {
116  RexxObjectPtr result = context->NewString(buf.strptr, buf.strlength);
117  if (buf.strptr != OREF_NULL)
118  {
119  RexxFreeMemory(buf.strptr);
120  }
121  return result;
122  }
123  return context->Nil(); /* give back a failure */
124 }
125 
126 /********************************************************************************************/
127 /* add a line to a rexx queue */
128 /********************************************************************************************/
130  RexxMethodContext *context, // the call context
131  RexxStringObject queue_line, /* line to add */
132  int order ) /* queuing order */
133 {
134  char buffer = 0; // buffer for an empty string
135  CONSTRXSTRING rx_string; // rxstring to push
136  RexxReturnCode rc; // queue return code
137 
138  if (queue_line == NULLOBJECT) /* no line given? */
139  {
140  // just use a null string value
141  MAKERXSTRING(rx_string, &buffer, 0);
142  }
143  else
144  {
145  MAKERXSTRING(rx_string, context->StringData(queue_line), context->StringLength(queue_line));
146  }
147  /* get the queue name */
148  RexxObjectPtr queue_name = context->GetObjectVariable("NAMED_QUEUE");
149  /* move the line to the queue */
150  rc = RexxAddQueue(context->ObjectToStringValue(queue_name), &rx_string, order);
151  if (rc != 0) /* stream error? */
152  {
153  context->RaiseException1(Rexx_Error_System_service_service, context->NewStringFromAsciiz("SYSTEM QUEUE"));
154  }
155  return rc; /* return the result */
156 }
157 
158 /********************************************************************************************/
159 /* Rexx_push_queue */
160 /********************************************************************************************/
161 RexxMethod1(wholenumber_t, rexx_push_queue,
162  OPTIONAL_RexxStringObject, queue_line) /* line to queue */
163 {
164  /* push a line onto the queue */
165  return rexx_add_queue(context, queue_line, RXQUEUE_LIFO);
166 }
167 
168 /********************************************************************************************/
169 /* Rexx_queue_queue */
170 /********************************************************************************************/
171 RexxMethod1(wholenumber_t, rexx_queue_queue,
172  OPTIONAL_RexxStringObject, queue_line) /* line to queue */
173 {
174  /* queue a line onto the queue */
175  return rexx_add_queue(context, queue_line, RXQUEUE_FIFO);
176 }
177 
178 /********************************************************************************************/
179 /* Rexx_create_queue */
180 /********************************************************************************************/
181 RexxMethod1(RexxStringObject, rexx_create_queue,
182  OPTIONAL_CSTRING, queue_name) /* current queue name */
183 {
184  char buf[MAX_QUEUE_NAME_LENGTH+1]; /* creation buffer */
185  RexxReturnCode rc; /* creation return code */
186  size_t dup_flag = 0; /* duplicate name flag */
187 
188  /* create a queue */
189  rc = RexxCreateQueue(buf, sizeof(buf), queue_name, &dup_flag);
190 
191  if (!rc) /* work ok? */
192  {
193  return context->NewStringFromAsciiz(buf);
194  }
195  return context->NullString(); /* just return a null string */
196 }
197 
198 /********************************************************************************************/
199 /* Rexx_open_queue */
200 /********************************************************************************************/
201 RexxMethod1(logical_t, rexx_open_queue, CSTRING, queue_name)
202 {
203  size_t dup_flag = 0; /* duplicate name flag */
204  /* create a queue */
205  return RexxOpenQueue(queue_name, &dup_flag);
206 }
207 
208 /********************************************************************************************/
209 /* Rexx_queue_exists */
210 /********************************************************************************************/
211 RexxMethod1(logical_t, rexx_queue_exists, CSTRING, queue_name)
212 {
213  /* create a queue */
214  return RexxQueueExists(queue_name) == 0;
215 }
216 
217 /********************************************************************************************/
218 /* Rexx_delete_queue */
219 /********************************************************************************************/
220 RexxMethod1(wholenumber_t, rexx_delete_queue,
221  CSTRING, queue_name)
222 {
223  /* just delete the queue */
224  return RexxDeleteQueue(queue_name);
225 }
226 
227 /********************************************************************************************/
228 /* Rexx_clear_queue */
229 /********************************************************************************************/
230 RexxMethod0(int, rexx_clear_queue)
231 {
232  /* get the queue name */
233  RexxObjectPtr queue_name = context->GetObjectVariable("NAMED_QUEUE");
234  /* Clear the queue */
235  return RexxClearQueue(context->ObjectToStringValue(queue_name));
236 }
#define OREF_NULL
Definition: RexxCore.h:61
RexxMethod1(wholenumber_t, rexx_push_queue, OPTIONAL_RexxStringObject, queue_line)
RexxMethod0(size_t, rexx_query_queue)
wholenumber_t rexx_add_queue(RexxMethodContext *context, RexxStringObject queue_line, int order)
#define Rexx_Error_System_service_service
Definition: oorexxerrors.h:452
#define MAX_QUEUE_NAME_LENGTH
Definition: rexx.h:780
RexxReturnCode REXXENTRY RexxPullFromQueue(CONSTANT_STRING, PRXSTRING, RexxQueueTime *, size_t)
RexxReturnCode REXXENTRY RexxClearQueue(CONSTANT_STRING)
const char * CSTRING
Definition: rexx.h:78
RexxReturnCode REXXENTRY RexxFreeMemory(void *)
size_t logical_t
Definition: rexx.h:231
struct _RexxStringObject * RexxStringObject
Definition: rexx.h:128
RexxReturnCode REXXENTRY RexxDeleteQueue(CONSTANT_STRING)
RexxReturnCode REXXENTRY RexxOpenQueue(CONSTANT_STRING, size_t *)
RexxReturnCode REXXENTRY RexxAddQueue(CONSTANT_STRING, PCONSTRXSTRING, size_t)
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
#define NULLOBJECT
Definition: rexx.h:147
ssize_t wholenumber_t
Definition: rexx.h:230
#define MAKERXSTRING(r, p, l)
Definition: rexx.h:182
RexxReturnCode REXXENTRY RexxQueryQueue(CONSTANT_STRING, size_t *)
int RexxReturnCode
Definition: rexx.h:73
RexxReturnCode REXXENTRY RexxQueueExists(CONSTANT_STRING)
RexxReturnCode REXXENTRY RexxCreateQueue(char *, size_t, CONSTANT_STRING, size_t *)
#define RXQUEUE_WAIT
Definition: rexxapidefs.h:243
#define RXQUEUE_FIFO
Definition: rexxapidefs.h:239
#define RXQUEUE_LIFO
Definition: rexxapidefs.h:240
#define RXQUEUE_NOWAIT
Definition: rexxapidefs.h:242
size_t strlength
Definition: rexx.h:157
char * strptr
Definition: rexx.h:158