KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > proxy > ClientToProxyIoHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.example.proxy;
21
22 import java.net.InetSocketAddress JavaDoc;
23
24 import org.apache.mina.common.ConnectFuture;
25 import org.apache.mina.common.IoConnector;
26 import org.apache.mina.common.IoFuture;
27 import org.apache.mina.common.IoFutureListener;
28 import org.apache.mina.common.IoSession;
29 import org.apache.mina.common.RuntimeIOException;
30 import org.apache.mina.common.TrafficMask;
31
32 /**
33  * Handles the client to proxy part of the proxied connection.
34  *
35  * @author The Apache Directory Project (mina-dev@directory.apache.org)
36  * @version $Rev$, $Date$
37  *
38  */

39 public class ClientToProxyIoHandler extends AbstractProxyIoHandler {
40     private final ServerToProxyIoHandler connectorHandler;
41
42     private final IoConnector connector;
43
44     private final InetSocketAddress JavaDoc address;
45
46     public ClientToProxyIoHandler(ServerToProxyIoHandler connectorHandler,
47             IoConnector connector, InetSocketAddress JavaDoc address) {
48         this.connectorHandler = connectorHandler;
49         this.connector = connector;
50         this.address = address;
51     }
52
53     public void sessionOpened(final IoSession session) throws Exception JavaDoc {
54         connector.connect(address, connectorHandler).addListener(
55                 new IoFutureListener() {
56                     public void operationComplete(IoFuture f) {
57                         ConnectFuture future = (ConnectFuture) f;
58                         try {
59                             future.getSession().setAttachment(session);
60                             session.setAttachment(future.getSession());
61                             future.getSession().setTrafficMask(TrafficMask.ALL);
62                         } catch (RuntimeIOException e) {
63                             // Connect failed
64
session.close();
65                         } finally {
66                             session.setTrafficMask(TrafficMask.ALL);
67                         }
68                     }
69                 });
70     }
71 }
72
Popular Tags