00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "ObjectRequestQueue.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 ObjectRequestQueue::ObjectRequestQueue() {
00034 inQueue = new ObjectQueue();
00035 outQueue = new ObjectQueue();
00036 }
00037
00038 ObjectRequestQueue::~ObjectRequestQueue()
00039 {
00040 delete(inQueue);
00041 delete(outQueue);
00042 }
00043
00044
00045 unsigned long ObjectRequestQueue::getPayloadSize() const {
00046 return inQueue->getPayloadSize() + outQueue->getPayloadSize();
00047 }
00048
00049 JString ObjectRequestQueue::enterRequest(Object* req) {
00050 JString id = inQueue->add(req);
00051 wait(0);
00052 return id;
00053 }
00054
00055 Object* ObjectRequestQueue::waitForReply(JString id, long ms) {
00056 return outQueue->waitForNewEntry(id, ms);
00057 }
00058
00059 bool ObjectRequestQueue::isEmpty() {
00060 return inQueue->isEmpty();
00061 }
00062
00063 int ObjectRequestQueue::getCount() {
00064 return inQueue->getCount();
00065 }
00066
00067
00068
00069 bool ObjectRequestQueue::waitForNewRequestToAppear(int ms) {
00070 return inQueue->waitForNewEntryToAppear(ms);
00071 }
00072
00073 JString ObjectRequestQueue::waitForNewRequestID(int ms) {
00074 return inQueue->waitForNewEntryID(ms);
00075 }
00076
00077 Object* ObjectRequestQueue::getRequest(JString id) {
00078 return inQueue->retrieveEntry(id);
00079 }
00080
00081 bool ObjectRequestQueue::reply(JString id, Object* rep) {
00082 return (outQueue->add(id, rep).length() > 0);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 long ObjectRequestQueue::getBinarySize(int chunk) {
00093 int chunkCountInQueue = 0, chunkCountOutQueue = 0;
00094 long binarySize = 0;
00095 if (inQueue != NULL) {
00096 chunkCountInQueue = inQueue->getBinaryChunkCount();
00097 if (chunk <= chunkCountInQueue)
00098 return inQueue->getBinarySize(chunk);
00099 }
00100 if (outQueue != NULL) {
00101 chunkCountOutQueue = outQueue->getBinaryChunkCount();
00102 if (chunk > chunkCountInQueue)
00103 return outQueue->getBinarySize(chunk - chunkCountInQueue);
00104 }
00105 return 0;
00106 }
00107
00108
00109 int ObjectRequestQueue::getBinaryChunkCount() {
00110 int chunkCountInQueue = 0, chunkCountOutQueue = 0;
00111 if (inQueue != NULL)
00112 chunkCountInQueue = inQueue->getBinaryChunkCount();
00113 if (outQueue != NULL)
00114 chunkCountOutQueue = outQueue->getBinaryChunkCount();
00115 return chunkCountInQueue + chunkCountOutQueue;
00116 }
00117
00118
00119 long ObjectRequestQueue::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
00120 int chunkCountInQueue = 0, chunkCountOutQueue = 0;
00121 long binarySize = 0;
00122 if (inQueue != NULL) {
00123 chunkCountInQueue = inQueue->getBinaryChunkCount();
00124 if (chunk <= chunkCountInQueue)
00125 return inQueue->toBinaryBuffer(chunk, buffer, maxlen);
00126 }
00127 if (outQueue != NULL) {
00128 chunkCountOutQueue = outQueue->getBinaryChunkCount();
00129 if (chunk > chunkCountInQueue)
00130 return outQueue->toBinaryBuffer(chunk-chunkCountInQueue, buffer, maxlen);
00131 }
00132 return 0;
00133 }
00134
00135
00136 bool ObjectRequestQueue::fromBinaryBuffer(int chunk, char* buffer, long len) {
00137 int chunkCountInQueue = 0, chunkCountOutQueue = 0;
00138 long binarySize = 0;
00139 if (inQueue != NULL) {
00140 chunkCountInQueue = inQueue->getBinaryChunkCount();
00141 if (chunk <= chunkCountInQueue)
00142 return inQueue->fromBinaryBuffer(chunk, buffer, len);
00143 }
00144 if (outQueue != NULL) {
00145 chunkCountOutQueue = outQueue->getBinaryChunkCount();
00146 if (chunk > chunkCountInQueue)
00147 return outQueue->fromBinaryBuffer(chunk-chunkCountInQueue, buffer, len);
00148 }
00149 return 0;
00150 }
00151
00152
00153 bool ObjectRequestQueue::unitTest() {
00154
00155 ObjectRequestQueue* rq = new ObjectRequestQueue();
00156
00157 if (rq->waitForReply("bla", 500) != NULL) {
00158 delete(rq);
00159 return false;
00160 }
00161
00162 if (rq->waitForNewRequestID(500).length() > 0)
00163 return false;
00164
00165
00166
00167 JString* str = new JString("Test");
00168 JString id = rq->enterRequest(str);
00169
00170
00171 if (rq->waitForReply(id, 500) != NULL)
00172 return false;
00173
00174 JString id2 = rq->waitForNewRequestID(500);
00175
00176 if (!id.equals(id2))
00177 return false;
00178
00179 JString* request = (JString*) rq->getRequest(id2);
00180
00181 if (!request->equals("Test"))
00182 return false;
00183
00184 delete(request);
00185
00186
00187 str = new JString("TestReply");
00188 if (!rq->reply(id, str))
00189 return false;
00190
00191
00192 JString* repl = (JString*) rq->waitForReply(id, 500);
00193 if (!repl->equals("TestReply"))
00194 return false;
00195
00196 delete(rq);
00197 delete(repl);
00198
00199 return true;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 }