1+ /*
2+ * socket.io-java-client XhrTransport.java
3+ *
4+ * Copyright (c) 2012, Enno Boland
5+ * PROJECT DESCRIPTION
6+ *
7+ * See LICENSE file for more information
8+ */
19package io.socket.transports;
210
311import java.io.BufferedReader;
1321import io.socket.IOConnection;
1422import io.socket.IOTransport;
1523
24+ /**
25+ * The Class XhrTransport.
26+ */
1627public class XhrTransport implements IOTransport {
17- /** The String to identify this Transport */
28+
29+ /** The String to identify this Transport. */
1830 public static final String TRANSPORT_NAME = "xhr-polling";
31+
32+ /** The connection. */
1933 private IOConnection connection;
34+
35+ /** The url. */
2036 private URL url;
37+
38+ /** The queue holding elements to send. */
2139 ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
2240
23- ReceiverThread receiver = null;
41+ /** background thread for managing the server connection. */
42+ PollThread pollThread = null;
43+
44+ /** Indicates whether the {@link IOConnection} wants us to be connected. */
2445 private boolean connect;
46+
47+ /** Indicates whether {@link PollThread} is blocked. */
2548 private boolean blocked;
2649
27- private class ReceiverThread extends Thread {
28- public ReceiverThread() {
50+ /**
51+ * The Class ReceiverThread.
52+ */
53+ private class PollThread extends Thread {
54+
55+ /**
56+ * Instantiates a new receiver thread.
57+ */
58+ public PollThread() {
2959 super(TRANSPORT_NAME);
3060 }
3161
62+ /* (non-Javadoc)
63+ * @see java.lang.Thread#run()
64+ */
3265 @Override
3366 public void run() {
3467 connection.transportConnected();
@@ -69,6 +102,13 @@ public void run() {
69102 }
70103 }
71104
105+ /**
106+ * Creates a new Transport for the given url an {@link IOConnection}.
107+ *
108+ * @param url the url
109+ * @param connection the connection
110+ * @return the iO transport
111+ */
72112 public static IOTransport create(URL url, IOConnection connection) {
73113 try {
74114 URL xhrUrl = new URL(url.toString() + IOConnection.SOCKET_IO_1
@@ -82,65 +122,105 @@ public static IOTransport create(URL url, IOConnection connection) {
82122
83123 }
84124
125+ /**
126+ * Instantiates a new xhr transport.
127+ *
128+ * @param url the url
129+ * @param connection the connection
130+ */
85131 public XhrTransport(URL url, IOConnection connection) {
86132 this.connection = connection;
87133 this.url = url;
88134 }
89135
136+ /* (non-Javadoc)
137+ * @see io.socket.IOTransport#connect()
138+ */
90139 @Override
91140 public void connect() {
92141 this.setConnect(true);
93- if (receiver == null) {
94- receiver = new ReceiverThread();
95- receiver.start();
96- } else
97- new RuntimeException(
98- "connecting on an allready connected transport");
142+ pollThread = new PollThread();
143+ pollThread.start();
99144 }
100145
146+ /* (non-Javadoc)
147+ * @see io.socket.IOTransport#disconnect()
148+ */
101149 @Override
102150 public void disconnect() {
103151 this.setConnect(false);
104- receiver .interrupt();
152+ pollThread .interrupt();
105153 }
106154
155+ /* (non-Javadoc)
156+ * @see io.socket.IOTransport#send(java.lang.String)
157+ */
107158 @Override
108159 public void send(String text) throws IOException {
109160 sendBulk(new String[] { text });
110161 }
111162
163+ /* (non-Javadoc)
164+ * @see io.socket.IOTransport#canSendBulk()
165+ */
112166 @Override
113167 public boolean canSendBulk() {
114168 return true;
115169 }
116170
171+ /* (non-Javadoc)
172+ * @see io.socket.IOTransport#sendBulk(java.lang.String[])
173+ */
117174 @Override
118175 public void sendBulk(String[] texts) throws IOException {
119176 for (String text : texts) {
120177 queue.add(text);
121178 }
122179 if(isBlocked())
123- receiver .interrupt();
180+ pollThread .interrupt();
124181 }
125182
183+ /* (non-Javadoc)
184+ * @see io.socket.IOTransport#invalidate()
185+ */
126186 @Override
127187 public void invalidate() {
128188 this.connection = null;
129189 }
130190
131- public synchronized boolean isConnect() {
191+ /**
192+ * Checks if is connect.
193+ *
194+ * @return true, if is connect
195+ */
196+ private synchronized boolean isConnect() {
132197 return connect;
133198 }
134199
135- public synchronized void setConnect(boolean connect) {
200+ /**
201+ * Sets the connect.
202+ *
203+ * @param connect the new connect
204+ */
205+ private synchronized void setConnect(boolean connect) {
136206 this.connect = connect;
137207 }
138208
139- public synchronized boolean isBlocked() {
209+ /**
210+ * Checks if is blocked.
211+ *
212+ * @return true, if is blocked
213+ */
214+ private synchronized boolean isBlocked() {
140215 return blocked;
141216 }
142217
143- public synchronized void setBlocked(boolean blocked) {
218+ /**
219+ * Sets the blocked.
220+ *
221+ * @param blocked the new blocked
222+ */
223+ private synchronized void setBlocked(boolean blocked) {
144224 this.blocked = blocked;
145225 }
146226}
0 commit comments