1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * A class to manage queueing of messages for IPC
  28  */
  29 
  30 #include "AccessBridgeDebug.h"
  31 #include "AccessBridgeMessageQueue.h"
  32 #include "AccessBridgePackages.h"               // for debugging only
  33 #include <windows.h>
  34 #include <malloc.h>
  35 
  36 DEBUG_CODE(extern HWND theDialogWindow);
  37 extern "C" {
  38     DEBUG_CODE(void AppendToCallInfo(char *s));
  39 }
  40 
  41 // -------------------
  42 
  43 
  44 AccessBridgeQueueElement::AccessBridgeQueueElement(char *buf, int size) {
  45     bufsize = size;
  46     next = (AccessBridgeQueueElement *) 0;
  47     previous = (AccessBridgeQueueElement *) 0;
  48     buffer = (char *) malloc(bufsize);
  49     memcpy(buffer, buf, bufsize);
  50 }
  51 
  52 AccessBridgeQueueElement::~AccessBridgeQueueElement() {
  53     //  delete buffer;
  54     free(buffer);
  55 }
  56 
  57 
  58 // -------------------
  59 
  60 
  61 AccessBridgeMessageQueue::AccessBridgeMessageQueue() {
  62     queueLocked = FALSE;
  63     queueRemoveLocked = FALSE;
  64     start = (AccessBridgeQueueElement *) 0;
  65     end = (AccessBridgeQueueElement *) 0;
  66     size = 0;
  67 }
  68 
  69 AccessBridgeMessageQueue::~AccessBridgeMessageQueue() {
  70     // empty queue, then exit
  71 }
  72 
  73 /**
  74  * getEventsWaiting - gets the number of events waiting to fire
  75  */
  76 int
  77 AccessBridgeMessageQueue::getEventsWaiting() {
  78     return size;
  79 }
  80 
  81 /**
  82  * add - add an element to the queue, which is locked with semaphores
  83  *
  84  */
  85 QueueReturns
  86 AccessBridgeMessageQueue::add(AccessBridgeQueueElement *element) {
  87     PrintDebugString("  in AccessBridgeMessageQueue::add()");
  88     PrintDebugString("    queue size = %d", size);
  89 
  90     QueueReturns returnVal = cElementPushedOK;
  91     if (queueLocked) {
  92         PrintDebugString("    queue was locked; returning cQueueInUse!");
  93         return cQueueInUse;
  94     }
  95     queueLocked = TRUE;
  96     {
  97         PrintDebugString("    adding element to queue!");
  98         if (end == (AccessBridgeQueueElement *) 0) {
  99             if (start == (AccessBridgeQueueElement *) 0 && size == 0) {
 100                 start = element;
 101                 end = element;
 102                 element->previous = (AccessBridgeQueueElement *) 0;
 103                 element->next = (AccessBridgeQueueElement *) 0;
 104                 size++;
 105             } else {
 106                 returnVal = cQueueBroken;       // bad voodo!
 107             }
 108         } else {
 109             element->previous = end;
 110             element->next = (AccessBridgeQueueElement *) 0;
 111             end->next = element;
 112             end = element;
 113             size++;
 114         }
 115     }
 116     queueLocked = FALSE;
 117     PrintDebugString("    returning from AccessBridgeMessageQueue::add()");
 118     return returnVal;
 119 }
 120 
 121 
 122 /**
 123  * remove - remove an element from the queue, which is locked with semaphores
 124  *
 125  */
 126 QueueReturns
 127 AccessBridgeMessageQueue::remove(AccessBridgeQueueElement **element) {
 128     PrintDebugString("  in AccessBridgeMessageQueue::remove()");
 129     PrintDebugString("    queue size = %d", size);
 130 
 131     QueueReturns returnVal = cMoreMessages;
 132     if (queueLocked) {
 133         PrintDebugString("    queue was locked; returning cQueueInUse!");
 134         return cQueueInUse;
 135     }
 136     queueLocked = TRUE;
 137     {
 138         PrintDebugString("    removing element from queue!");
 139         if (size > 0) {
 140             if (start != (AccessBridgeQueueElement *) 0) {
 141                 *element = start;
 142                 start = start->next;
 143                 if (start != (AccessBridgeQueueElement *) 0) {
 144                     start->previous = (AccessBridgeQueueElement *) 0;
 145                 } else {
 146                     end = (AccessBridgeQueueElement *) 0;
 147                     if (size != 1) {
 148                         returnVal = cQueueBroken;       // bad voodo, should only be 1 in this situation
 149                     }
 150                 }
 151                 size--;
 152             } else {
 153                 returnVal = cQueueBroken;       // bad voodo!
 154             }
 155         } else {
 156             returnVal = cQueueEmpty;
 157         }
 158     }
 159     queueLocked = FALSE;
 160     PrintDebugString("    returning from AccessBridgeMessageQueue::remove()");
 161     return returnVal;
 162 }
 163 
 164 
 165 /**
 166  * setRemoveLock - set the state of the removeLock (TRUE or FALSE)
 167  *
 168  */
 169 QueueReturns
 170 AccessBridgeMessageQueue::setRemoveLock(BOOL removeLockSetting) {
 171     if (queueLocked) {
 172         return cQueueInUse;
 173     }
 174     queueRemoveLocked = removeLockSetting;
 175 
 176     return cQueueOK;
 177 }
 178 
 179 /**
 180  * setRemoveLock - set the state of the removeLock (TRUE or FALSE)
 181  *
 182  */
 183 BOOL
 184 AccessBridgeMessageQueue::getRemoveLockSetting() {
 185     return queueRemoveLocked;
 186 }