File: NetSupportSocket.java

package info (click to toggle)
tomcat8 8.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 33,280 kB
  • sloc: java: 303,023; xml: 46,103; jsp: 3,182; sh: 1,358; perl: 269; makefile: 114
file content (171 lines) | stat: -rw-r--r-- 5,045 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tomcat.spdy;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;


public class NetSupportSocket extends SpdyContext.NetSupport {

    @Override
    public boolean isSpdy(Object socketW) {
        return false;
    }

    @Override
    public SpdyConnection getConnection(String host, int port) throws IOException {
        try {
            Socket sock = getSocket(host, port);

            sock.getInputStream();
            SpdyConnectionSocket con = new SpdyConnectionSocket(ctx, sock);

            ctx.getExecutor().execute(con.inputThread);
            return con;
        } catch (IOException ex) {
            ex.printStackTrace();
            throw ex;
        }

    }


    protected Socket getSocket(String host, int port) throws IOException {
        try {
            if (ctx.tls) {
                SSLContext sslCtx = SSLContext.getDefault();
                SSLSocket socket = (SSLSocket) sslCtx.getSocketFactory().createSocket(host, port);
                //socket.setEnabledProtocols(new String[] {"TLS1"});
                socket.startHandshake();
                return socket;
            } else {
                return new Socket(host, port);
            }
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(e);
        }

    }

    private boolean running = true;
    private ServerSocket serverSocket;

    @Override
    public void stop() throws IOException {
        running = false;
        serverSocket.close();
    }

    @Override
    public void onAccept(Object socket) {
        SpdyConnectionSocket ch = new SpdyConnectionSocket(ctx, (Socket) socket);
        ch.onBlockingSocket();
    }


    @Override
    public void listen(final int port, String cert, String key) throws IOException {
        ctx.getExecutor().execute(new Runnable() {
            @Override
            public void run() {
                accept(port);
            }
        });
    }

    private void accept(int port) {
        try {
            serverSocket = new ServerSocket(port);
            while (running) {
                final Socket socket = serverSocket.accept();
                ctx.getExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        onAccept(socket);
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (IOException ex) {
            if (running) {
                ex.printStackTrace();
            }
            running = false;
        }
    }


    private static class SpdyConnectionSocket extends SpdyConnection {
        private Socket socket;


        Runnable inputThread = new Runnable() {
            @Override
            public void run() {
                onBlockingSocket();
                try {
                    inClosed = true;
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        public SpdyConnectionSocket(SpdyContext spdyContext) {
            super(spdyContext);
        }

        public SpdyConnectionSocket(SpdyContext spdyContext, Socket socket) {
            super(spdyContext);
            this.socket = socket;
        }

        @Override
        public void close() throws IOException {
            socket.close();
        }

        @Override
        public synchronized int write(byte[] data, int off, int len) throws IOException {
            socket.getOutputStream().write(data, off, len);
            return len;
        }

        @Override
        public int read(byte[] data, int off, int len) throws IOException {
            try {
                return socket.getInputStream().read(data, off, len);
            } catch (SocketTimeoutException ex) {
                return 0;
            }
        }
    }

}