PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/singlestack/binding-java/runtime/src/main/java/org/apache/etch/bindings/java/transport/UdpTransportFactory.java

#
Java | 170 lines | 107 code | 29 blank | 34 comment | 2 complexity | cbf108173fa72a14459a993bf6c9909c MD5 | raw file
Possible License(s): Apache-2.0
  1. /* $Id$
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. package org.apache.etch.bindings.java.transport;
  21. import java.net.SocketAddress;
  22. import org.apache.etch.bindings.java.msg.ValueFactory;
  23. import org.apache.etch.bindings.java.support.ServerFactory;
  24. import org.apache.etch.bindings.java.support.TransportFactory;
  25. import org.apache.etch.util.Resources;
  26. import org.apache.etch.util.URL;
  27. import org.apache.etch.util.core.io.SessionListener;
  28. import org.apache.etch.util.core.io.Transport;
  29. import org.apache.etch.util.core.io.TransportPacket;
  30. import org.apache.etch.util.core.io.UdpConnection;
  31. import org.apache.etch.util.core.io.UdpListener;
  32. /**
  33. * Transport factory for udp connections.
  34. */
  35. public class UdpTransportFactory extends TransportFactory
  36. {
  37. /**
  38. * Resource tags
  39. */
  40. private final static String UDP_LISTENER = "UdpTransportFactory.udpListener";
  41. private final static String SOCKET_ADDRESS = "UdpTransportFactory.socketAddress";
  42. /**
  43. * Constructs a UdpTransportFactory which delivers UdpConnection.
  44. */
  45. public UdpTransportFactory()
  46. {
  47. }
  48. @Override
  49. public TransportMessage newTransport( String uri, Resources resources ) throws Exception
  50. {
  51. UdpListener udpListener = (UdpListener) resources.get( UDP_LISTENER );
  52. SocketAddress socketAddress = (SocketAddress) resources.get( SOCKET_ADDRESS );
  53. URL url = new URL( uri );
  54. TransportPacket transportPacket = null;
  55. if (udpListener != null)
  56. transportPacket = new UdpConnection( socketAddress, udpListener );
  57. else
  58. transportPacket = new UdpConnection( uri );
  59. TransportMessage transportMessage = new Messagizer( transportPacket, url, resources );
  60. transportMessage = addFilters( transportMessage, url, resources );
  61. ValueFactory vf = (ValueFactory) resources.get( Transport.VALUE_FACTORY );
  62. vf.lockDynamicTypes();
  63. return transportMessage;
  64. }
  65. @Override
  66. public Transport<ServerFactory> newListener( final String uri, final Resources resources ) throws Exception
  67. {
  68. UdpListener transportListener = new UdpListener( uri );
  69. return new MySessionListener( this, transportListener, uri, resources );
  70. }
  71. private class MySessionListener implements Transport<ServerFactory>, SessionListener<SocketAddress>
  72. {
  73. /**
  74. * @param utf
  75. * @param transportListener
  76. * @param uri
  77. * @param resources
  78. */
  79. public MySessionListener(UdpTransportFactory utf, UdpListener transportListener, String uri, Resources resources)
  80. {
  81. this.udpTransportFactory = utf;
  82. this.listener = transportListener;
  83. this.uri = uri;
  84. this.resources = resources;
  85. transportListener.setSession( this );
  86. }
  87. private final UdpTransportFactory udpTransportFactory;
  88. private final UdpListener listener;
  89. private final String uri;
  90. private final Resources resources;
  91. public ServerFactory getSession()
  92. {
  93. return session;
  94. }
  95. public void setSession( ServerFactory session )
  96. {
  97. this.session = session;
  98. }
  99. private ServerFactory session;
  100. @Override
  101. public String toString()
  102. {
  103. return "UdpTransportFactory.MySessionListener/" + listener;
  104. }
  105. public Object transportQuery( Object query ) throws Exception
  106. {
  107. return listener.transportQuery( query );
  108. }
  109. public void transportControl( Object control, Object value ) throws Exception
  110. {
  111. listener.transportControl( control, value );
  112. }
  113. public void transportNotify( Object event ) throws Exception
  114. {
  115. listener.transportNotify( event );
  116. }
  117. @Override
  118. public void sessionAccepted( SocketAddress socketAddress ) throws Exception
  119. {
  120. ValueFactory vf = session.newValueFactory( uri );
  121. Resources r = new Resources( resources );
  122. r.put( UDP_LISTENER, listener );
  123. r.put( SOCKET_ADDRESS, socketAddress );
  124. r.put( Transport.VALUE_FACTORY, vf );
  125. TransportMessage t = udpTransportFactory.newTransport( uri, r );
  126. session.newServer( t, uri, r );
  127. }
  128. public Object sessionQuery( Object query ) throws Exception
  129. {
  130. return session.sessionQuery( query );
  131. }
  132. public void sessionControl( Object control, Object value ) throws Exception
  133. {
  134. session.sessionControl( control, value );
  135. }
  136. public void sessionNotify( Object event ) throws Exception
  137. {
  138. session.sessionNotify( event );
  139. }
  140. }
  141. }