PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/granite-client-flex-udp/src/main/flex/org/granite/gravity/channels/udp/UdpGravityChannel.as

http://github.com/graniteds/graniteds
ActionScript | 127 lines | 62 code | 21 blank | 44 comment | 9 complexity | 245ceef50f139796e3a5a4b61516409b MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. * GRANITE DATA SERVICES
  3. * Copyright (C) 2006-2015 GRANITE DATA SERVICES S.A.S.
  4. *
  5. * This file is part of the Granite Data Services Platform.
  6. *
  7. * ***
  8. *
  9. * Community License: GPL 3.0
  10. *
  11. * This file is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published
  13. * by the Free Software Foundation, either version 3 of the License,
  14. * or (at your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * ***
  25. *
  26. * Available Commercial License: GraniteDS SLA 1.0
  27. *
  28. * This is the appropriate option if you are creating proprietary
  29. * applications and you are not prepared to distribute and share the
  30. * source code of your application under the GPL v3 license.
  31. *
  32. * Please visit http://www.granitedataservices.com/license for more
  33. * details.
  34. */
  35. package org.granite.gravity.channels.udp {
  36. import flash.net.DatagramSocket;
  37. import mx.logging.ILogger;
  38. import mx.logging.Log;
  39. import mx.messaging.MessageResponder;
  40. import mx.messaging.messages.IMessage;
  41. import mx.utils.URLUtil;
  42. import org.granite.gravity.channels.GravityChannel;
  43. import org.granite.gravity.channels.GravityStreamTunnel;
  44. /**
  45. * Channel implementation for UDP based communication. Note that UDP is only used for data
  46. * pushed from the server: all other operations (ping, authentication, subscriptions, etc.)
  47. * are performed through a standard (unencrypted) HTTP channel. A <code>UdpSecureGravityChannel</code>
  48. * is also provided in order to perform these operations through an HTTPS (encrypted) channel.
  49. *
  50. * @author Franck WOLFF
  51. *
  52. * @see UdpSecureGravityChannel
  53. */
  54. public class UdpGravityChannel extends GravityChannel {
  55. private static var log:ILogger = Log.getLogger("org.granite.enterprise.gravity.channels.UdpGravityChannel");
  56. public static const UDP_CONNECT:String = "udpChannelConnect";
  57. public static const UDP_DISCONNECT:String = "udpChannelDisconnect";
  58. private var _defaultLocalPort:int = 0;
  59. private var _defaultLocalAddress:String = "0.0.0.0"; // For IPv6, use "::"
  60. public function UdpGravityChannel(id:String, uri:String) {
  61. super(id, uri);
  62. }
  63. public function get defaultLocalPort():int {
  64. return _defaultLocalPort;
  65. }
  66. public function set defaultLocalPort(value:int):void {
  67. if (value < 0)
  68. value = 0;
  69. _defaultLocalPort = value;
  70. }
  71. public function get defaultLocalAddress():String {
  72. return _defaultLocalAddress;
  73. }
  74. public function set defaultLocalAddress(value:String):void {
  75. _defaultLocalAddress = value;
  76. }
  77. public function get localPort():int {
  78. var socket:DatagramSocket = (tunnel as UdpGravityStreamTunnel).datagramSocket;
  79. return (socket != null && socket.bound ? socket.localPort : -1);
  80. }
  81. public function get localAddress():String {
  82. var socket:DatagramSocket = (tunnel as UdpGravityStreamTunnel).datagramSocket;
  83. return (socket != null && socket.bound ? socket.localAddress : null);
  84. }
  85. public function get remotePort():int {
  86. var socket:DatagramSocket = (tunnel as UdpGravityStreamTunnel).datagramSocket;
  87. return (socket != null && socket.connected ? socket.remotePort : -1);
  88. }
  89. public function get remoteAddress():String {
  90. var socket:DatagramSocket = (tunnel as UdpGravityStreamTunnel).datagramSocket;
  91. return (socket != null && socket.connected ? socket.remoteAddress : null);
  92. }
  93. override protected function newTunnel():GravityStreamTunnel {
  94. return new UdpGravityStreamTunnel(this);
  95. }
  96. internal function sendFromCommand(messageResponder:MessageResponder):void {
  97. internalSend(messageResponder);
  98. }
  99. internal function callResponder(responder:MessageResponder, response:IMessage):void {
  100. internalCallResponder(responder, response);
  101. }
  102. internal function get serverIp():String {
  103. return URLUtil.getServerName(uri);
  104. }
  105. }
  106. }