This commit is contained in:
Edward Capriolo
2015-01-17 18:17:38 -05:00
parent 6d70b28e82
commit 4506fe4a79
2 changed files with 76 additions and 68 deletions

View File

@ -7,40 +7,41 @@ import java.io.IOException;
import org.json.JSONException; import org.json.JSONException;
public class GossipRunner { public class GossipRunner {
private StartupSettings _settings; private StartupSettings _settings;
public static void main(String[] args) { public static void main(String[] args) {
File configFile; File configFile;
if (args.length == 1) { if (args.length == 1) {
configFile = new File("./"+args[0]); configFile = new File("./" + args[0]);
} else { } else {
configFile = new File("gossip.conf"); configFile = new File("gossip.conf");
} }
new GossipRunner(configFile); new GossipRunner(configFile);
} }
public GossipRunner(File configFile) { public GossipRunner(File configFile) {
if (configFile != null && configFile.exists()) { if (configFile != null && configFile.exists()) {
try { try {
System.out.println("Parsing the configuration file..."); System.out.println("Parsing the configuration file...");
_settings = StartupSettings.fromJSONFile(configFile); _settings = StartupSettings.fromJSONFile(configFile);
GossipService gossipService = new GossipService(_settings); GossipService gossipService = new GossipService(_settings);
System.out.println("Gossip service successfully inialized, let's start it..."); System.out.println("Gossip service successfully inialized, let's start it...");
gossipService.start(); gossipService.start();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.err.println("The given file is not found!"); System.err.println("The given file is not found!");
} catch (JSONException e) { } catch (JSONException e) {
System.err.println("The given file is not in the correct JSON format!"); System.err.println("The given file is not in the correct JSON format!");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Could not read the configuration file: " + e.getMessage()); System.err.println("Could not read the configuration file: " + e.getMessage());
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.err.println("Error while starting the gossip service: " + e.getMessage()); System.err.println("Error while starting the gossip service: " + e.getMessage());
} }
} else { } else {
System.out.println("The gossip.conf file is not found.\n\nEither specify the path to the startup settings file or place the gossip.json file in the same folder as the JAR file."); System.out
} .println("The gossip.conf file is not found.\n\nEither specify the path to the startup settings file or place the gossip.json file in the same folder as the JAR file.");
} }
}
} }

View File

@ -15,37 +15,45 @@ import com.google.code.gossip.manager.random.RandomGossipManager;
* @author joshclemm, harmenw * @author joshclemm, harmenw
*/ */
public class GossipService { public class GossipService {
public static final Logger LOGGER = Logger.getLogger(GossipService.class);
private GossipManager _gossipManager;
/**
* Constructor with the default settings.
* @throws InterruptedException
* @throws UnknownHostException
*/
public GossipService(StartupSettings startupSettings) throws InterruptedException, UnknownHostException {
this(InetAddress.getLocalHost().getHostAddress(), startupSettings.getPort(), "", startupSettings.getLogLevel(), startupSettings.getGossipMembers(), startupSettings.getGossipSettings());
}
/** public static final Logger LOGGER = Logger.getLogger(GossipService.class);
* Setup the client's lists, gossiping parameters, and parse the startup config file.
* @throws SocketException private GossipManager _gossipManager;
* @throws InterruptedException
* @throws UnknownHostException /**
*/ * Constructor with the default settings.
public GossipService(String ipAddress, int port, String id, int logLevel, ArrayList<GossipMember> gossipMembers, GossipSettings settings) throws InterruptedException, UnknownHostException { *
_gossipManager = new RandomGossipManager(ipAddress, port, id, settings, gossipMembers); * @throws InterruptedException
} * @throws UnknownHostException
*/
public void start() { public GossipService(StartupSettings startupSettings) throws InterruptedException,
_gossipManager.start(); UnknownHostException {
} this(InetAddress.getLocalHost().getHostAddress(), startupSettings.getPort(), "",
startupSettings.getLogLevel(), startupSettings.getGossipMembers(), startupSettings
public void shutdown() { .getGossipSettings());
_gossipManager.shutdown(); }
}
/**
* Setup the client's lists, gossiping parameters, and parse the startup config file.
*
* @throws SocketException
* @throws InterruptedException
* @throws UnknownHostException
*/
public GossipService(String ipAddress, int port, String id, int logLevel,
ArrayList<GossipMember> gossipMembers, GossipSettings settings)
throws InterruptedException, UnknownHostException {
_gossipManager = new RandomGossipManager(ipAddress, port, id, settings, gossipMembers);
}
public void start() {
_gossipManager.start();
}
public void shutdown() {
_gossipManager.shutdown();
}
public GossipManager get_gossipManager() { public GossipManager get_gossipManager() {
return _gossipManager; return _gossipManager;
} }
@ -53,6 +61,5 @@ public class GossipService {
public void set_gossipManager(GossipManager _gossipManager) { public void set_gossipManager(GossipManager _gossipManager) {
this._gossipManager = _gossipManager; this._gossipManager = _gossipManager;
} }
} }