annotate src/main/java/com/glavsoft/rfb/protocol/MessageQueue.java @ 0:daa24f8a557b

TightVNC original
author YU
date Thu, 11 Sep 2014 07:30:03 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
daa24f8a557b TightVNC original
YU
parents:
diff changeset
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
2 // All rights reserved.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
3 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
4 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
5 // This file is part of the TightVNC software. Please visit our Web site:
daa24f8a557b TightVNC original
YU
parents:
diff changeset
6 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
7 // http://www.tightvnc.com/
daa24f8a557b TightVNC original
YU
parents:
diff changeset
8 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
9 // This program is free software; you can redistribute it and/or modify
daa24f8a557b TightVNC original
YU
parents:
diff changeset
10 // it under the terms of the GNU General Public License as published by
daa24f8a557b TightVNC original
YU
parents:
diff changeset
11 // the Free Software Foundation; either version 2 of the License, or
daa24f8a557b TightVNC original
YU
parents:
diff changeset
12 // (at your option) any later version.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
13 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
14 // This program is distributed in the hope that it will be useful,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
daa24f8a557b TightVNC original
YU
parents:
diff changeset
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
daa24f8a557b TightVNC original
YU
parents:
diff changeset
17 // GNU General Public License for more details.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
18 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
19 // You should have received a copy of the GNU General Public License along
daa24f8a557b TightVNC original
YU
parents:
diff changeset
20 // with this program; if not, write to the Free Software Foundation, Inc.,
daa24f8a557b TightVNC original
YU
parents:
diff changeset
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
22 //-------------------------------------------------------------------------
daa24f8a557b TightVNC original
YU
parents:
diff changeset
23 //
daa24f8a557b TightVNC original
YU
parents:
diff changeset
24
daa24f8a557b TightVNC original
YU
parents:
diff changeset
25 package com.glavsoft.rfb.protocol;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
26
daa24f8a557b TightVNC original
YU
parents:
diff changeset
27 import com.glavsoft.rfb.client.ClientToServerMessage;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
28
daa24f8a557b TightVNC original
YU
parents:
diff changeset
29 import java.util.concurrent.BlockingQueue;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
30 import java.util.concurrent.LinkedBlockingQueue;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
31 import java.util.concurrent.TimeUnit;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
32
daa24f8a557b TightVNC original
YU
parents:
diff changeset
33 /**
daa24f8a557b TightVNC original
YU
parents:
diff changeset
34 * @author dime at tightvnc.com
daa24f8a557b TightVNC original
YU
parents:
diff changeset
35 */
daa24f8a557b TightVNC original
YU
parents:
diff changeset
36 public class MessageQueue {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
37 private final BlockingQueue<ClientToServerMessage> queue;
daa24f8a557b TightVNC original
YU
parents:
diff changeset
38
daa24f8a557b TightVNC original
YU
parents:
diff changeset
39 public MessageQueue() {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
40 queue = new LinkedBlockingQueue<ClientToServerMessage>();
daa24f8a557b TightVNC original
YU
parents:
diff changeset
41 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
42
daa24f8a557b TightVNC original
YU
parents:
diff changeset
43 public void put(ClientToServerMessage message) {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
44 queue.offer(message);
daa24f8a557b TightVNC original
YU
parents:
diff changeset
45 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
46
daa24f8a557b TightVNC original
YU
parents:
diff changeset
47 /**
daa24f8a557b TightVNC original
YU
parents:
diff changeset
48 * Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
49 * Retrieves and removes the head of this queue, waiting up to the certain wait time if necessary for
daa24f8a557b TightVNC original
YU
parents:
diff changeset
50 * an element to become available.
daa24f8a557b TightVNC original
YU
parents:
diff changeset
51 * @return the head of this queue, or null if the specified waiting time elapses before an element is available
daa24f8a557b TightVNC original
YU
parents:
diff changeset
52 * @throws InterruptedException - if interrupted while waiting
daa24f8a557b TightVNC original
YU
parents:
diff changeset
53 */
daa24f8a557b TightVNC original
YU
parents:
diff changeset
54 public ClientToServerMessage get() throws InterruptedException {
daa24f8a557b TightVNC original
YU
parents:
diff changeset
55 return queue.poll(1, TimeUnit.SECONDS);
daa24f8a557b TightVNC original
YU
parents:
diff changeset
56 }
daa24f8a557b TightVNC original
YU
parents:
diff changeset
57
daa24f8a557b TightVNC original
YU
parents:
diff changeset
58 }