View Javadoc

1   /***
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  
19  package org.activeio.net;
20  
21  import java.io.IOException;
22  import java.net.URI;
23  
24  import org.activeio.AcceptListener;
25  import org.activeio.AsynchChannel;
26  import org.activeio.AsynchChannelServer;
27  
28  /***
29   * @version $Revision$
30   */
31  final public class VMPipeAsynchChannelServer implements AsynchChannelServer {
32  
33      private final URI bindURI;
34      private final URI connectURI;
35      private AcceptListener acceptListener;
36      private boolean disposed;
37      
38      public VMPipeAsynchChannelServer(URI bindURI) {
39          this.bindURI = this.connectURI = bindURI;
40      }
41  
42      public URI getBindURI() {
43          return bindURI;
44      }
45  
46      public URI getConnectURI() {
47          return this.connectURI;
48      }
49  
50      public void dispose() {
51          if( disposed )
52              return;
53          
54          VMPipeAsynchChannelFactory.unbindServer(bindURI);
55          disposed=true;
56      }
57  
58      public void start() throws IOException {
59          if( acceptListener==null )
60              throw new IOException("acceptListener has not been set.");
61      }
62  
63      public void stop(long timeout) {
64      }
65  
66      public Object narrow(Class target) {
67          if( target.isAssignableFrom(getClass()) ) {
68              return this;
69          }
70          return null;
71      }
72      
73      public String toString() {
74          return "VM Pipe Server: "+getConnectURI();
75      }
76  
77      public void setAcceptListener(AcceptListener acceptListener) {
78          this.acceptListener = acceptListener;
79      }
80  
81      public AsynchChannel connect() {
82          VMPipeAsynchChannelPipe pipe = new VMPipeAsynchChannelPipe();
83          acceptListener.onAccept(pipe.getRightAsynchChannel());
84          return pipe.getLeftAsynchChannel();
85      }
86      
87  }