PageRenderTime 31ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/emitrom/ti4j/mobile/client/network/socket/Socket.java

https://github.com/emitrom/titanium4j
Java | 123 lines | 49 code | 17 blank | 57 comment | 2 complexity | dc861ea2126ec2d9d0cbc74e4048cc6f MD5 | raw file
Possible License(s): Apache-2.0
  1. /************************************************************************
  2. Socket.java is part of Ti4j 3.1.0 Copyright 2013 Emitrom LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **************************************************************************/
  13. package com.emitrom.ti4j.mobile.client.network.socket;
  14. import com.emitrom.ti4j.core.client.JsoHelper;
  15. import com.emitrom.ti4j.mobile.client.core.TiFactory;
  16. import com.emitrom.ti4j.mobile.client.core.TiModule;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. /**
  19. * Socket module, used for creating sockets.
  20. */
  21. public class Socket extends TiModule {
  22. public static final double INITIALIZED = INITIALIZED();
  23. public static final double CONNECTED = CONNECTED();
  24. public static final double LISTENING = LISTENING();
  25. public static final double CLOSED = CLOSED();
  26. public static final double ERROR = ERROR();
  27. private static Socket instance = null;
  28. public static Socket get() {
  29. if (instance == null) {
  30. instance = new Socket();
  31. }
  32. return instance;
  33. }
  34. private Socket() {
  35. createPeer();
  36. }
  37. private Socket(JavaScriptObject obj) {
  38. jsObj = obj;
  39. }
  40. /**
  41. * Returns new tcp socket object, takes object containing properties list
  42. * defined in 'titanium.network.socket.tcp'. some properties are only needed
  43. * based on whether the socket will be a connecting socket or a listening
  44. * socket.
  45. *
  46. * @return New TCP socket.
  47. */
  48. public native TCP createTCP() /*-{
  49. var jso = this.@com.emitrom.ti4j.core.client.ProxyObject::getJsObj()();
  50. var obj = jso.createTCP();
  51. var toReturn = @com.emitrom.ti4j.mobile.client.network.socket.TCP::new(Lcom/google/gwt/core/client/JavaScriptObject;)(obj);
  52. return toReturn;
  53. }-*/;
  54. /**
  55. * Returns new tcp socket object, takes object containing properties list
  56. * defined in 'titanium.network.socket.tcp'. some properties are only needed
  57. * based on whether the socket will be a connecting socket or a listening
  58. * socket.
  59. *
  60. * @param params
  61. * creation parameters
  62. * @return New TCP socket.
  63. */
  64. public native TCP createTCP(JavaScriptObject params) /*-{
  65. var jso = this.@com.emitrom.ti4j.core.client.ProxyObject::getJsObj()();
  66. var obj = jso.createTCP(params);
  67. var toReturn = @com.emitrom.ti4j.mobile.client.network.socket.TCP::new(Lcom/google/gwt/core/client/JavaScriptObject;)(obj);
  68. return toReturn;
  69. }-*/;
  70. /**
  71. * Returns a new TCP socket, connecting to the given host and port.
  72. *
  73. * @param host
  74. * @param port
  75. * @return TCP
  76. * @since 1.1.0
  77. */
  78. public TCP createTCP(String host, int port) {
  79. JavaScriptObject jso = JavaScriptObject.createObject();
  80. JsoHelper.setAttribute(jso, "host", host);
  81. JsoHelper.setAttribute(jso, "port", port);
  82. return createTCP(jso);
  83. }
  84. private static native final double INITIALIZED() /*-{
  85. return Titanium.Network.Socket.INITIALIZED;
  86. }-*/;
  87. private static native final double CONNECTED() /*-{
  88. return Titanium.Network.Socket.CONNECTED;
  89. }-*/;
  90. private static native final double LISTENING() /*-{
  91. return Titanium.Network.Socket.LISTENING;
  92. }-*/;
  93. private static native final double CLOSED() /*-{
  94. return Titanium.Network.Socket.CLOSED;
  95. }-*/;
  96. private static native final double ERROR() /*-{
  97. return Titanium.Network.Socket.ERROR;
  98. }-*/;
  99. @Override
  100. public void createPeer() {
  101. jsObj = TiFactory.createNativeSocketModule();
  102. }
  103. }