| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Session | 
  | 
  | 0.0;0 | 
| 1 |  /* | |
| 2 |          Copyright 2007 Alexey Akhunov | |
| 3 | ||
| 4 |     Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 |     you may not use this file except in compliance with the License. | |
| 6 |     You may obtain a copy of the License at | |
| 7 | ||
| 8 |         http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | ||
| 10 |     Unless required by applicable law or agreed to in writing, software | |
| 11 |     distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 |     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 |     See the License for the specific language governing permissions and | |
| 14 |     limitations under the License. | |
| 15 |  */ | |
| 16 |  package diy.middleware.client; | |
| 17 | ||
| 18 |  import java.util.concurrent.BlockingQueue; | |
| 19 |  import java.util.concurrent.CountDownLatch; | |
| 20 |  import java.util.concurrent.TimeUnit; | |
| 21 | ||
| 22 |  import diy.middleware.Frame; | |
| 23 | ||
| 24 | public class Session {  | |
| 25 | ||
| 26 |          private String sessionId; | |
| 27 |          private BlockingQueue<Frame> inQueue; | |
| 28 |          private BlockingQueue<Frame> outQueue; | |
| 29 | private volatile CountDownLatch latch;  | |
| 30 | ||
| 31 | 0 |          Session(String sessionId, BlockingQueue<Frame> inQueue, CountDownLatch initialLatch) { | 
| 32 | 0 |                  this.sessionId = sessionId; | 
| 33 | 0 |                  this.inQueue = inQueue; | 
| 34 | 0 |                  this.latch = initialLatch; | 
| 35 | 0 |          } | 
| 36 | ||
| 37 |          public BlockingQueue<Frame> getInQueue() { | |
| 38 | 0 |                  return inQueue; | 
| 39 | }  | |
| 40 | ||
| 41 |          public String getSessionId() { | |
| 42 | 0 |                  return sessionId; | 
| 43 | }  | |
| 44 | ||
| 45 | public void send(String message) throws InterruptedException {  | |
| 46 | 0 |                  outQueue.put(new Frame(sessionId, "M" + message)); | 
| 47 | 0 |          } | 
| 48 | ||
| 49 |          /** | |
| 50 |           * Subscribe to messages from the server that match | |
| 51 |           * given pattern. This method is synchronized because | |
| 52 |           * only one subscription per session is allowed | |
| 53 |           * at this stage. | |
| 54 |           * @param pattern Regular expression, defining the subscription | |
| 55 |           * pattern. Messages matching this pattern will be delivered | |
| 56 |           * to this session's inQueue. | |
| 57 |           * @param timeout Amount of time to wait for the subscription | |
| 58 |           * to complete. Successful subscription involves received | |
| 59 |           * acknowledgment message from the server. If this ack | |
| 60 |           * message does not come within specified timeout, | |
| 61 |           * this method will return. | |
| 62 |           * @param unit Time units of timeout parameter. | |
| 63 |           * @return <code>true</code> if subscription was confirmed | |
| 64 |           * by the server within given timeout, <code>false</code> | |
| 65 |           * otherwise. | |
| 66 |           * @throws InterruptedException  | |
| 67 |           */ | |
| 68 | public synchronized boolean receive(String pattern, long timeout, TimeUnit unit) throws InterruptedException {  | |
| 69 |                  // Create a new latch | |
| 70 | 0 |                  latch = new CountDownLatch(1); | 
| 71 | 0 |                  outQueue.put(new Frame(sessionId, "REC" + pattern)); | 
| 72 | 0 |                  return latch.await(timeout, unit); | 
| 73 | }  | |
| 74 | ||
| 75 | public synchronized boolean stop(long timeout, TimeUnit unit) throws InterruptedException {  | |
| 76 |                  // Create a new latch | |
| 77 | 0 |                  latch = new CountDownLatch(1); | 
| 78 | 0 |                  outQueue.put(new Frame(sessionId, "STOP")); | 
| 79 | 0 |                  return latch.await(timeout, unit); | 
| 80 | }  | |
| 81 | ||
| 82 |          void setOutQueue(BlockingQueue<Frame> outQueue) { | |
| 83 | 0 |                  this.outQueue = outQueue; | 
| 84 | 0 |          } | 
| 85 | ||
| 86 |          CountDownLatch getLatch() { | |
| 87 | 0 |                  return latch; | 
| 88 | }  | |
| 89 | ||
| 90 | }  |