View Javadoc

1   /*
2    * @(#) $Id: VmPipeSession.java 210062 2005-07-11 03:52:38Z trustin $
3    */
4   package org.apache.mina.protocol.vmpipe;
5   
6   import java.io.IOException;
7   import java.net.SocketAddress;
8   
9   import org.apache.mina.common.BaseSession;
10  import org.apache.mina.common.SessionConfig;
11  import org.apache.mina.common.TransportType;
12  import org.apache.mina.protocol.ProtocolDecoder;
13  import org.apache.mina.protocol.ProtocolEncoder;
14  import org.apache.mina.protocol.ProtocolFilterChain;
15  import org.apache.mina.protocol.ProtocolHandler;
16  import org.apache.mina.protocol.ProtocolSession;
17  import org.apache.mina.protocol.ProtocolSessionFilterChain;
18  import org.apache.mina.protocol.vmpipe.VmPipeAcceptor.Entry;
19  import org.apache.mina.util.ExceptionUtil;
20  
21  /***
22   * A {@link ProtocolSession} for in-VM transport (VM_PIPE).
23   * 
24   * @author Trustin Lee (trustin@apache.org)
25   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
26   */
27  class VmPipeSession extends BaseSession implements ProtocolSession
28  {
29      private final SocketAddress localAddress;
30  
31      private final SocketAddress remoteAddress;
32  
33      private final ProtocolHandler handler;
34      
35      private final VmPipeSessionConfig config = new VmPipeSessionConfig();
36  
37      private final ProtocolSessionFilterChain filterChain;
38      
39      private final VmPipeSessionManagerFilterChain managerFilterChain;
40  
41      final VmPipeSession remoteSession;
42  
43      final Object lock;
44  
45      boolean closed;
46  
47      /***
48       * Constructor for client-side session.
49       */
50      VmPipeSession( Object lock, SocketAddress localAddress,
51                     VmPipeSessionManagerFilterChain managerFilterChain,
52                     ProtocolHandler handler,
53                     Entry remoteEntry ) throws IOException
54      {
55          this.lock = lock;
56          this.localAddress = localAddress;
57          this.remoteAddress = remoteEntry.address;
58          this.handler = handler;
59          this.filterChain = new ProtocolSessionFilterChain( managerFilterChain );
60          this.managerFilterChain = managerFilterChain;
61  
62          remoteSession = new VmPipeSession( this, remoteEntry );
63          
64          // initialize remote session
65          try
66          {
67              remoteEntry.handler.sessionCreated( remoteSession );
68          }
69          catch( Throwable t )
70          {
71              remoteEntry.acceptor.getExceptionMonitor().exceptionCaught( remoteEntry.acceptor, t );
72              IOException e = new IOException( "Failed to initialize remote session." );
73              e.initCause( t );
74              throw e;
75          }
76          
77          // initialize client session
78          try
79          {
80              handler.sessionCreated( this );
81          }
82          catch( Throwable t )
83          {
84              ExceptionUtil.throwException( t );
85          }
86  
87          remoteEntry.managerFilterChain.sessionOpened( remoteSession );
88          managerFilterChain.sessionOpened( this );
89      }
90  
91      /***
92       * Constructor for server-side session.
93       */
94      VmPipeSession( VmPipeSession remoteSession, Entry entry )
95      {
96          this.lock = remoteSession.lock;
97          this.localAddress = remoteSession.remoteAddress;
98          this.remoteAddress = remoteSession.localAddress;
99          this.handler = entry.handler;
100         this.managerFilterChain = entry.managerFilterChain;
101         this.filterChain = new ProtocolSessionFilterChain( entry.managerFilterChain );
102         this.remoteSession = remoteSession;
103     }
104     
105     VmPipeSessionManagerFilterChain getManagerFilterChain()
106     {
107         return managerFilterChain;
108     }
109     
110     public ProtocolFilterChain getFilterChain()
111     {
112         return filterChain;
113     }
114 
115     public ProtocolHandler getHandler()
116     {
117         return handler;
118     }
119 
120     public ProtocolEncoder getEncoder()
121     {
122         return null;
123     }
124 
125     public ProtocolDecoder getDecoder()
126     {
127         return null;
128     }
129 
130     public void close( boolean wait )
131     {
132         synchronized( lock )
133         {
134             if( closed )
135                 return;
136 
137             closed = remoteSession.closed = true;
138             managerFilterChain.sessionClosed( this );
139             remoteSession.getManagerFilterChain().sessionClosed( remoteSession );
140         }
141     }
142 
143     public void write( Object message )
144     {
145         this.filterChain.filterWrite( this, message );
146     }
147 
148     public int getScheduledWriteRequests()
149     {
150         return 0;
151     }
152 
153     public TransportType getTransportType()
154     {
155         return TransportType.VM_PIPE;
156     }
157 
158     public boolean isConnected()
159     {
160         return !closed;
161     }
162 
163     public SessionConfig getConfig()
164     {
165         return config;
166     }
167 
168     public SocketAddress getRemoteAddress()
169     {
170         return remoteAddress;
171     }
172 
173     public SocketAddress getLocalAddress()
174     {
175         return localAddress;
176     }
177 }