org.evosuite.rmi.MasterServices Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
* contributors
*
* This file is part of EvoSuite.
*
* EvoSuite is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* EvoSuite is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with EvoSuite. If not, see .
*/
package org.evosuite.rmi;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import org.evosuite.rmi.service.MasterNodeLocal;
import org.evosuite.rmi.service.MasterNodeRemote;
import org.evosuite.rmi.service.MasterNodeImpl;
import org.evosuite.utils.Randomness;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class should be used only in the Master process, not the clients.
* Used to initialize and store all the RMI services in the master.
* It is also used to start the RMI registry
*
* @author arcuri
*
*/
public class MasterServices {
private static Logger logger = LoggerFactory.getLogger(MasterServices.class);
private static MasterServices instance = new MasterServices();
private int registryPort = -1;
/**
* We store it to avoid issues with GC
*/
private Registry registry;
private MasterNodeImpl masterNode;
protected MasterServices(){
}
public static MasterServices getInstance(){
return instance;
}
public boolean startRegistry() throws IllegalStateException{
if(registry != null){
throw new IllegalStateException("RMI registry is already running");
}
/*
* Unfortunately, it does not seem possible to start a RMI registry on an
* ephemeral port. So, we start with a port, and see if free. If not, try the
* next one, etc. Note, it is important to start from a random port to avoid issues
* with several masters running on same node, eg when experiments on cluster.
*/
int port = 2000;
port += Randomness.nextInt(20000);
final int TRIES = 100;
for(int i=0; i