PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ibis/smartsockets/virtual/modules/reverse/ReverseVirtualSocket.java

https://github.com/interdroid/smartsockets
Java | 445 lines | 188 code | 62 blank | 195 comment | 8 complexity | 105c753b68bde196d0d620672635d8b3 MD5 | raw file
  1. package ibis.smartsockets.virtual.modules.reverse;
  2. import ibis.smartsockets.virtual.VirtualSocket;
  3. import ibis.smartsockets.virtual.modules.AbstractDirectModule;
  4. import ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket;
  5. import java.io.EOFException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.net.ConnectException;
  10. import java.net.InetAddress;
  11. import java.net.SocketAddress;
  12. import java.net.SocketException;
  13. import java.nio.channels.SocketChannel;
  14. import java.util.Map;
  15. public class ReverseVirtualSocket extends VirtualSocket {
  16. private final DirectVirtualSocket s;
  17. protected ReverseVirtualSocket(DirectVirtualSocket s) {
  18. this.s = s;
  19. }
  20. protected void connectionAccepted(int timeout) throws IOException {
  21. int ack1 = -1;
  22. int ack2 = -1;
  23. try {
  24. s.setSoTimeout(timeout);
  25. s.setTcpNoDelay(true);
  26. OutputStream out = s.getOutputStream();
  27. InputStream in = s.getInputStream();
  28. // This is a bit nasty... We need to send the accept twice, and
  29. // read the reply twice, simply because the connection setup is
  30. // 'doubled' by first doing a reverse setup followed by a 'normal'
  31. // setup
  32. out.write(AbstractDirectModule.ACCEPT);
  33. out.write(AbstractDirectModule.ACCEPT);
  34. out.flush();
  35. // We should do a three way handshake here to ensure both side agree
  36. // that we have a connection...
  37. ack1 = in.read();
  38. ack2 = in.read();
  39. if (ack1 == -1 || ack2 == -1) {
  40. throw new EOFException("Reverse connection handshake failed: "
  41. + " Unexpected EOF");
  42. } else if (ack1 != AbstractDirectModule.ACCEPT ||
  43. ack2 != AbstractDirectModule.ACCEPT) {
  44. throw new ConnectException("Client disconnected");
  45. }
  46. s.setSoTimeout(0);
  47. s.setTcpNoDelay(false);
  48. } catch (IOException e) {
  49. s.close();
  50. throw e;
  51. }
  52. }
  53. /**
  54. * @throws IOException
  55. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#close()
  56. */
  57. public void close() throws IOException {
  58. s.close();
  59. }
  60. /**
  61. * @param timeout
  62. * @param opcode
  63. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#connectionRejected(int, byte)
  64. */
  65. public void connectionRejected(int timeout, byte opcode) {
  66. s.connectionRejected(timeout, opcode);
  67. }
  68. /**
  69. * @param timeout
  70. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#connectionRejected(int)
  71. */
  72. public void connectionRejected(int timeout) {
  73. s.connectionRejected(timeout);
  74. }
  75. /**
  76. * @param obj
  77. * @see java.lang.Object#equals(java.lang.Object)
  78. */
  79. public boolean equals(Object obj) {
  80. return s.equals(obj);
  81. }
  82. /**
  83. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getChannel()
  84. */
  85. public SocketChannel getChannel() {
  86. return s.getChannel();
  87. }
  88. /**
  89. * @see ibis.smartsockets.virtual.VirtualSocket#getInetAddress()
  90. */
  91. public InetAddress getInetAddress() {
  92. return s.getInetAddress();
  93. }
  94. /**
  95. * @throws IOException
  96. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getInputStream()
  97. */
  98. public InputStream getInputStream() throws IOException {
  99. return s.getInputStream();
  100. }
  101. /**
  102. * @throws SocketException
  103. * @see ibis.smartsockets.virtual.VirtualSocket#getKeepAlive()
  104. */
  105. public boolean getKeepAlive() throws SocketException {
  106. return s.getKeepAlive();
  107. }
  108. /**
  109. * @see ibis.smartsockets.virtual.VirtualSocket#getLocalAddress()
  110. */
  111. public InetAddress getLocalAddress() {
  112. return s.getLocalAddress();
  113. }
  114. /**
  115. * @see ibis.smartsockets.virtual.VirtualSocket#getLocalPort()
  116. */
  117. public int getLocalPort() {
  118. return s.getLocalPort();
  119. }
  120. /**
  121. * @see ibis.smartsockets.virtual.VirtualSocket#getLocalSocketAddress()
  122. */
  123. public SocketAddress getLocalSocketAddress() {
  124. return s.getLocalSocketAddress();
  125. }
  126. /**
  127. * @throws SocketException
  128. * @see ibis.smartsockets.virtual.VirtualSocket#getOOBInline()
  129. */
  130. public boolean getOOBInline() throws SocketException {
  131. return s.getOOBInline();
  132. }
  133. /**
  134. * @throws IOException
  135. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getOutputStream()
  136. */
  137. public OutputStream getOutputStream() throws IOException {
  138. return s.getOutputStream();
  139. }
  140. /**
  141. * @see ibis.smartsockets.virtual.VirtualSocket#getPort()
  142. */
  143. public int getPort() {
  144. return s.getPort();
  145. }
  146. /**
  147. * @param key
  148. * @see ibis.smartsockets.virtual.VirtualSocket#getProperty(java.lang.String)
  149. */
  150. public Object getProperty(String key) {
  151. return s.getProperty(key);
  152. }
  153. /**
  154. * @throws SocketException
  155. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getReceiveBufferSize()
  156. */
  157. public int getReceiveBufferSize() throws SocketException {
  158. return s.getReceiveBufferSize();
  159. }
  160. /**
  161. * @see ibis.smartsockets.virtual.VirtualSocket#getRemoteSocketAddress()
  162. */
  163. public SocketAddress getRemoteSocketAddress() {
  164. return s.getRemoteSocketAddress();
  165. }
  166. /**
  167. * @throws SocketException
  168. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getReuseAddress()
  169. */
  170. public boolean getReuseAddress() throws SocketException {
  171. return s.getReuseAddress();
  172. }
  173. /**
  174. * @throws SocketException
  175. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getSendBufferSize()
  176. */
  177. public int getSendBufferSize() throws SocketException {
  178. return s.getSendBufferSize();
  179. }
  180. /**
  181. * @throws SocketException
  182. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getSoLinger()
  183. */
  184. public int getSoLinger() throws SocketException {
  185. return s.getSoLinger();
  186. }
  187. /**
  188. * @throws SocketException
  189. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getSoTimeout()
  190. */
  191. public int getSoTimeout() throws SocketException {
  192. return s.getSoTimeout();
  193. }
  194. /**
  195. * @throws SocketException
  196. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#getTcpNoDelay()
  197. */
  198. public boolean getTcpNoDelay() throws SocketException {
  199. return s.getTcpNoDelay();
  200. }
  201. /**
  202. * @throws SocketException
  203. * @see ibis.smartsockets.virtual.VirtualSocket#getTrafficClass()
  204. */
  205. public int getTrafficClass() throws SocketException {
  206. return s.getTrafficClass();
  207. }
  208. /**
  209. * @see java.lang.Object#hashCode()
  210. */
  211. public int hashCode() {
  212. return s.hashCode();
  213. }
  214. /**
  215. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#isBound()
  216. */
  217. public boolean isBound() {
  218. return s.isBound();
  219. }
  220. /**
  221. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#isClosed()
  222. */
  223. public boolean isClosed() {
  224. return s.isClosed();
  225. }
  226. /**
  227. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#isConnected()
  228. */
  229. public boolean isConnected() {
  230. return s.isConnected();
  231. }
  232. /**
  233. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#isInputShutdown()
  234. */
  235. public boolean isInputShutdown() {
  236. return s.isInputShutdown();
  237. }
  238. /**
  239. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#isOutputShutdown()
  240. */
  241. public boolean isOutputShutdown() {
  242. return s.isOutputShutdown();
  243. }
  244. /**
  245. * @see ibis.smartsockets.virtual.VirtualSocket#properties()
  246. */
  247. public Map<String, Object> properties() {
  248. return s.properties();
  249. }
  250. /**
  251. * @param data
  252. * @throws IOException
  253. * @see ibis.smartsockets.virtual.VirtualSocket#sendUrgentData(int)
  254. */
  255. public void sendUrgentData(int data) throws IOException {
  256. s.sendUrgentData(data);
  257. }
  258. /**
  259. * @param on
  260. * @throws SocketException
  261. * @see ibis.smartsockets.virtual.VirtualSocket#setKeepAlive(boolean)
  262. */
  263. public void setKeepAlive(boolean on) throws SocketException {
  264. s.setKeepAlive(on);
  265. }
  266. /**
  267. * @param on
  268. * @throws SocketException
  269. * @see ibis.smartsockets.virtual.VirtualSocket#setOOBInline(boolean)
  270. */
  271. public void setOOBInline(boolean on) throws SocketException {
  272. s.setOOBInline(on);
  273. }
  274. /**
  275. * @param connectionTime
  276. * @param latency
  277. * @param bandwidth
  278. * @see ibis.smartsockets.virtual.VirtualSocket#setPerformancePreferences(int, int, int)
  279. */
  280. public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
  281. s.setPerformancePreferences(connectionTime, latency, bandwidth);
  282. }
  283. /**
  284. * @param properties
  285. * @see ibis.smartsockets.virtual.VirtualSocket#setProperties(java.util.Map)
  286. */
  287. public void setProperties(Map<String, Object> properties) {
  288. s.setProperties(properties);
  289. }
  290. /**
  291. * @param key
  292. * @param val
  293. * @see ibis.smartsockets.virtual.VirtualSocket#setProperty(java.lang.String, java.lang.Object)
  294. */
  295. public void setProperty(String key, Object val) {
  296. s.setProperty(key, val);
  297. }
  298. /**
  299. * @param sz
  300. * @throws SocketException
  301. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setReceiveBufferSize(int)
  302. */
  303. public void setReceiveBufferSize(int sz) throws SocketException {
  304. s.setReceiveBufferSize(sz);
  305. }
  306. /**
  307. * @param on
  308. * @throws SocketException
  309. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setReuseAddress(boolean)
  310. */
  311. public void setReuseAddress(boolean on) throws SocketException {
  312. s.setReuseAddress(on);
  313. }
  314. /**
  315. * @param sz
  316. * @throws SocketException
  317. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setSendBufferSize(int)
  318. */
  319. public void setSendBufferSize(int sz) throws SocketException {
  320. s.setSendBufferSize(sz);
  321. }
  322. /**
  323. * @param on
  324. * @param linger
  325. * @throws SocketException
  326. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setSoLinger(boolean, int)
  327. */
  328. public void setSoLinger(boolean on, int linger) throws SocketException {
  329. s.setSoLinger(on, linger);
  330. }
  331. /**
  332. * @param t
  333. * @throws SocketException
  334. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setSoTimeout(int)
  335. */
  336. public void setSoTimeout(int t) throws SocketException {
  337. s.setSoTimeout(t);
  338. }
  339. /**
  340. * @param on
  341. * @throws SocketException
  342. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#setTcpNoDelay(boolean)
  343. */
  344. public void setTcpNoDelay(boolean on) throws SocketException {
  345. s.setTcpNoDelay(on);
  346. }
  347. /**
  348. * @param tc
  349. * @throws SocketException
  350. * @see ibis.smartsockets.virtual.VirtualSocket#setTrafficClass(int)
  351. */
  352. public void setTrafficClass(int tc) throws SocketException {
  353. s.setTrafficClass(tc);
  354. }
  355. /**
  356. * @throws IOException
  357. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#shutdownInput()
  358. */
  359. public void shutdownInput() throws IOException {
  360. s.shutdownInput();
  361. }
  362. /**
  363. * @throws IOException
  364. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#shutdownOutput()
  365. */
  366. public void shutdownOutput() throws IOException {
  367. s.shutdownOutput();
  368. }
  369. /**
  370. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#toString()
  371. */
  372. public String toString() {
  373. return s.toString();
  374. }
  375. /**
  376. * @param timeout
  377. * @throws IOException
  378. * @see ibis.smartsockets.virtual.modules.direct.DirectVirtualSocket#waitForAccept(int)
  379. */
  380. public void waitForAccept(int timeout) throws IOException {
  381. s.waitForAccept(timeout);
  382. }
  383. }