1 24 25 package org.objectweb.tribe.channel; 26 27 import java.util.ArrayList ; 28 29 import org.objectweb.tribe.exceptions.EmptyBufferException; 30 import org.objectweb.tribe.messages.ChannelMessage; 31 32 38 public class ReceiveBuffer 39 { 40 41 private ArrayList queue; 42 private Object bufferKey; 43 44 50 public ReceiveBuffer(Object bufferKey) 51 { 52 queue = new ArrayList (); 53 this.bufferKey = bufferKey; 54 } 55 56 61 public Object getBufferKey() 62 { 63 return bufferKey; 64 } 65 66 71 public void addMessage(Object message) 72 { 73 synchronized (queue) 74 { 75 if (queue.isEmpty()) 78 queue.notifyAll(); 79 queue.add(message); 80 } 81 } 82 83 90 public ChannelMessage getMessage() throws EmptyBufferException 91 { 92 synchronized (queue) 93 { 94 while (queue.isEmpty()) 95 { 96 try 97 { 98 queue.wait(); 99 } 100 catch (InterruptedException ignore) 101 { 102 } 103 } 104 return (ChannelMessage) removeMessage(); 105 } 106 } 107 108 114 public Object removeMessage() throws EmptyBufferException 115 { 116 synchronized (queue) 117 { 118 if (queue.isEmpty()) 119 throw new EmptyBufferException(); 120 return queue.remove(0); 121 } 122 } 123 124 129 public synchronized boolean isEmpty() 130 { 131 return queue.isEmpty(); 132 } 133 134 } | Popular Tags |