1 /*
2  * Copyright (c) 1997, 2001, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.net.*;
25 import javax.net.ssl.*;
26 
27 class ClientThread extends Handler
28     implements HandshakeCompletedListener
29 {
30     private int         port;
31     private InetAddress server;
32     private SSLSocketFactory factory;
33 
34     static private int  threadCounter = 0;
35 
getCounter()36     private static synchronized int getCounter ()
37         { return ++threadCounter; }
38 
ClientThread(int port, SSLContext ctx)39     ClientThread (int port, SSLContext ctx)
40     {
41         super ("Client-" + getCounter ());
42         roleIsClient = true;
43         factory = ctx.getSocketFactory();
44 
45         try {
46             this.server = InetAddress.getLocalHost ();
47             this.port = port;
48         } catch (UnknownHostException e) {
49             synchronized (out) {
50                 out.println ("%% " + getName ());
51                 e.printStackTrace (out);
52             }
53         }
54     }
55 
ClientThread(InetAddress server, int port, SSLContext ctx)56     ClientThread (InetAddress server, int port, SSLContext ctx)
57     {
58         super ("Client-" + getCounter ());
59         roleIsClient = true;
60         factory = ctx.getSocketFactory();
61 
62         this.server = server;
63         this.port = port;
64     }
65 
66 
setReverseRole(boolean flag)67     public void setReverseRole (boolean flag)
68     {
69         if (flag)
70             roleIsClient = false;
71         else
72             roleIsClient = true;
73     }
74 
run()75     public void run ()
76     {
77         try {
78             s = (SSLSocket) factory.createSocket(server, port);
79 
80         } catch (Throwable t) {
81             synchronized (out) {
82                 out.println ("%% " + getName ());
83                 t.printStackTrace (out);
84             }
85             return;
86         }
87 
88         if (basicCipherSuites != null) {
89             s.setEnabledCipherSuites (basicCipherSuites);
90             if (basicCipherSuites.length == 1)
91                 System.out.println ("%% " + getName () + " trying "
92                         + basicCipherSuites [0]);
93         }
94 
95         super.run ();
96 
97         out.println ("%% " + getName ()
98             + (passed () ? ", Passed!" : " ... FAILED"));
99     }
100 
101 }
102