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