1 /*
   2  * Copyright (c) 2015, 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 package compiler.compilercontrol.share.actions;
  25 
  26 import compiler.compilercontrol.share.scenario.State;
  27 import jdk.test.lib.Pair;
  28 import jdk.test.lib.ProcessTools;
  29 import pool.PoolHelper;
  30 
  31 import java.io.BufferedReader;
  32 import java.io.InputStreamReader;
  33 import java.io.PrintWriter;
  34 import java.io.OutputStreamWriter;
  35 import java.io.IOException;
  36 import java.lang.reflect.Executable;
  37 import java.net.InetAddress;
  38 import java.net.Socket;
  39 import java.util.Arrays;
  40 import java.util.HashMap;
  41 import java.util.List;
  42 import java.util.ListIterator;
  43 import java.util.Map;
  44 import java.util.concurrent.Callable;
  45 import java.util.stream.Collectors;
  46 
  47 public class BaseAction {
  48     private static final List<Pair<Executable, Callable<?>>> METHODS;
  49     private static final Map<String, Executable> METHODS_NAMES;
  50 
  51     static {
  52         METHODS = new PoolHelper().getAllMethods();
  53         METHODS_NAMES = METHODS.stream().collect(Collectors.toMap(
  54                 pair -> pair.first.toGenericString(),
  55                 pair -> pair.first));
  56     }
  57 
  58     /*
  59      * args[0] is a port to connect
  60      * args[1] is an optional parameter that shows that the state map should be
  61      *         passed
  62      */
  63     public static void main(String[] args) {
  64         if (args.length < 1) {
  65             throw new Error("TESTBUG: requires port as parameter: "
  66                     + Arrays.toString(args));
  67         }
  68         boolean getStates = false;
  69         if (args.length == 2) {
  70             if ("states".equals(args[1])) {
  71                 getStates = true;
  72             } else {
  73                 throw new Error("TESTBUG: incorrect argument: "+ args[1]);
  74             }
  75         }
  76         int pid;
  77         try {
  78             pid = ProcessTools.getProcessId();
  79         } catch (Exception e) {
  80             throw new Error("Could not determine own pid", e);
  81         }
  82         int port = Integer.parseInt(args[0]);
  83         System.out.println("INFO: Client connection port = " + port);
  84         List<String> lines;
  85         try (
  86                 Socket socket = new Socket(InetAddress.getLocalHost(), port);
  87                 BufferedReader in = new BufferedReader(
  88                         new InputStreamReader(socket.getInputStream()));
  89                 PrintWriter out = new PrintWriter(
  90                         new OutputStreamWriter(socket.getOutputStream()))) {
  91             // send own pid to execute jcmd if needed
  92             out.println(String.valueOf(pid));
  93             out.flush();
  94             if (getStates) {
  95                 lines = in.lines().collect(Collectors.toList());
  96                 check(decodeMap(lines));
  97             } else {
  98                 in.readLine();
  99             }
 100         } catch (IOException e) {
 101             throw new Error("Error on performing network operation", e);
 102         }
 103     }
 104 
 105     private static Map<Executable, State> decodeMap(List<String> lines) {
 106         if (lines == null || lines.size() == 0) {
 107             throw new Error("TESTBUG: unexpected lines list");
 108         }
 109         Map<Executable, State> stateMap = new HashMap<>();
 110         int startIndex = 0;
 111         ListIterator<String> iterator = lines.listIterator();
 112         while (iterator.hasNext()) {
 113             int index = iterator.nextIndex();
 114             String next = iterator.next();
 115             switch (next) {
 116                 case "{" :
 117                     startIndex = index;
 118                     break;
 119                 case "}" :
 120                     // method name goes after {
 121                     Executable executable = METHODS_NAMES.get(lines.get(
 122                             ++startIndex));
 123                     // state description starts after method
 124                     State state = State.fromString(lines.subList(++startIndex,
 125                             index).toArray(new String[index - startIndex]));
 126                     stateMap.put(executable, state);
 127                     break;
 128             }
 129         }
 130         return stateMap;
 131     }
 132 
 133     protected static void check(Map<Executable, State> methodStates) {
 134         // Check each method from the pool
 135         METHODS.forEach(pair -> {
 136             Executable x = pair.first;
 137             CompileAction.checkCompiled(x, methodStates.get(x));
 138         });
 139     }
 140 }