summaryrefslogtreecommitdiff
path: root/java-compat/test/controller/src/main/java/Test/Common/ControllerServer.java
blob: c90efc8e2721469a10683ed3fa132b92808fa37c (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Test.Common;

import Test.Common._ControllerDisp;
import Test.Common._ServerDisp;
import Test.Common.ServerPrx;

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.regex.Pattern;

public class ControllerServer extends Ice.Application
{
    class ServerI extends _ServerDisp
    {
        public ServerI(Process process, String name)
        {
            _process = process;
            _processOutput = new StringBuffer();
            _name = name;
            _started = 0;
            _terminated = false;

            class Reader extends Thread
            {
                Reader(java.io.InputStream is)
                {
                    _is = is;
                }

                public void run()
                {
                    try
                    {
                        final BufferedReader reader = new BufferedReader(new InputStreamReader(_is));

                        String line = reader.readLine();
                        while(line != null)
                        {
                            if(_started == 0)
                            {
                                _processOutput.append(line + "\n");
                            }

                            if(line.matches(Pattern.quote("starting server...") + ".*ok.*") ||
                               line.matches(Pattern.quote("starting serveramd...") + ".*ok.*") ||
                               line.matches(Pattern.quote("starting servertie...") + ".*ok.*") ||
                               line.matches(Pattern.quote("starting serveramdtie...") + ".*ok.*") ||
                               line.matches("starting test.*" + Pattern.quote("Server...") + ".*ok.*"))
                            {
                                synchronized(ServerI.this)
                                {
                                    _started++;
                                    ServerI.this.notifyAll();
                                }
                            }
                            else if(line.matches(Pattern.quote("starting server...") + ".*") ||
                                    line.matches(Pattern.quote("starting serveramd...") + ".*") ||
                                    line.matches(Pattern.quote("starting servertie...") + ".*") ||
                                    line.matches(Pattern.quote("starting serveramdtie...") + ".*") ||
                                    line.matches("starting test.*" + Pattern.quote("Server...")  + ".*"))
                            {
                                String s = reader.readLine();
                                if(s == null)
                                {
                                    System.out.println(line);
                                    break;
                                }
                                line += s;
                                continue;
                            }
                            System.out.println(line);
                            line = reader.readLine();
                        }
                    }
                    catch(java.io.IOException ex)
                    {
                        System.out.println("server start failed!\n" + ex);
                    }

                    // Make sure to unblock thread waiting for server start.
                    synchronized(ServerI.this)
                    {
                        _terminated = true;
                        ServerI.this.notifyAll();
                    }
                }

                private java.io.InputStream _is;
            }
            new Reader(_process.getInputStream()).start();
        }

        public synchronized void terminate(Ice.Current current)
        {
            try
            {
                _process.exitValue();
                return;
            }
            catch(IllegalThreadStateException ex)
            {
                //
                // process is still running.
                //
                System.out.print("terminating " + _name + "... ");
                System.out.flush();

                _process.destroy();
                while(true)
                {
                    try
                    {
                        _process.waitFor();
                        break;
                    }
                    catch(InterruptedException e)
                    {
                    }
                }
                current.adapter.remove(current.id);
                System.out.println("ok");
            }
        }

        public void waitTestSuccess(Ice.Current current)
        {
            Process p = null;
            synchronized(this)
            {
                p = _process;
            }

            if(p != null)
            {
                while(true)
                {
                    try
                    {
                        p.waitFor();
                        break;
                    }
                    catch(InterruptedException ex)
                    {
                    }
                }
            }
        }

        public synchronized void waitForServer(Ice.Current current)
            throws ServerFailedException
        {
            while(!_terminated)
            {
                try
                {
                    if(_started > 0)
                    {
                        _started--;
                        break;
                    }
                    wait();
                }
                catch(InterruptedException ex)
                {
                    continue;
                }
            }
            if(_terminated && _started == 0)
            {
                throw new ServerFailedException(_processOutput.toString());
            }
        }

        private Process _process;
        private StringBuffer _processOutput;
        private String _name;
        private int _started;
        private boolean _terminated;
    }

    public class ControllerI extends _ControllerDisp
    {
        public ControllerI(String[] args)
        {
            _args = args;
        }

        @Override
        public ServerPrx runServer(String lang, final String name, String protocol, String host,
                                   boolean winrt, String configName, String[] options, Ice.Current current)
        {
            if(_server != null)
            {
                try
                {
                    _server.terminate();
                }
                catch(Ice.LocalException ex)
                {
                }
            }

            String script = lang + (lang.equals("java") ? "/test/src/main/java/" : "/") + "test/" + name + "/run.py";

            java.util.List<String> args = new java.util.ArrayList<String>();
            args.add("python");
            args.add(script);
            args.add("--server");
            args.add("--protocol");
            args.add(protocol);
            if(!host.isEmpty())
            {
                args.add("--host");
                args.add(host);
            }

            if(winrt)
            {
                args.add("--winrt");
            }

            if(!configName.isEmpty())
            {
                args.add("--configName");
                args.add(configName);
            }

            for(String option : options)
            {
                args.add("--arg");
                args.add(option);
            }

            for(String a : _args)
            {
                args.add(a);
            }

            try
            {
                System.out.print("starting " + name + "... ");
                System.out.flush();

                final Process process = new ProcessBuilder(args)
                    .directory(_toplevel)
                    .redirectErrorStream(true)
                    .start();
                _server = ServerPrxHelper.uncheckedCast(current.adapter.addWithUUID(new ServerI(process, name)));
            }
            catch(java.io.IOException ex)
            {
                throw new RuntimeException("failed to start server `" + name + "'", ex);
            }
            return _server;
        }

        private ServerPrx _server;
        private String[] _args;
    }

    @Override
    public int
    run(String[] args)
    {
        Ice.ObjectAdapter adapter = communicator().createObjectAdapter("ControllerAdapter");
        adapter.add(new ControllerI(args), Ice.Util.stringToIdentity("controller"));
        adapter.activate();
        communicator().waitForShutdown();
        return 0;
    }

    public ControllerServer(File toplevel)
    {
        _toplevel = toplevel;
    }

    public static void
    main(String[] args)
    {
        try
        {
            File toplevel = new File(
                new File(ControllerServer.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(),
                "../../../../../");

            ControllerServer app = new ControllerServer(toplevel);
            Ice.InitializationData initData = new Ice.InitializationData();
            initData.properties = Ice.Util.createProperties(args);
            initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
            initData.properties.setProperty("IceSSL.DefaultDir", new File(toplevel, "certs").getCanonicalPath());
            initData.properties.setProperty("IceSSL.Keystore", "server.jks");
            initData.properties.setProperty("IceSSL.Password", "password");
            initData.properties.setProperty("IceSSL.VerifyPeer", "0");
            initData.properties.setProperty("Ice.ThreadPool.Server.SizeMax", "10");
            initData.properties.setProperty("ControllerAdapter.Endpoints",
                                            "tcp -p 15000:ssl -p 15001: ws -p 15002:wss -p 15003");

            int status = app.main("ControllerServer", args, initData);
            System.exit(status);
        }
        catch(java.net.URISyntaxException ex)
        {
            ex.printStackTrace();
            System.exit(1);
        }
        catch(java.io.IOException ex)
        {
            ex.printStackTrace();
            System.exit(1);
        }
    }

    private final File _toplevel;
}