Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Commit

Permalink
Added code to ensure that only one instance of the app is running at …
Browse files Browse the repository at this point in the history
…a time.
  • Loading branch information
Vlad-Adrian Moglan committed Sep 16, 2018
1 parent fdd9112 commit 43196e7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/moglan/eac/application/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
public class Config {

public static final int PORT = 9090;
public static final int APP_PORT = 9091; // used for ensuring that only one instance of the app runs at a time
public static final int SERVER_PORT = 9090;
public static final int SO_TIMEOUT = 50000;

private Config() { }
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moglan/eac/application/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void stopSimulation() {
public void addClientTask(int sendingFrequency) throws UnknownHostException, IOException {
if (clientTasks.size() < Monitor.get().getMaximumNumberOfConnections()) {
MeasuringDevice clientTask = new MeasuringDevice("127.0.0.1",
Config.PORT, BASE_PERIOD/sendingFrequency);
Config.SERVER_PORT, BASE_PERIOD/sendingFrequency);

clientTasks.add(clientTask);
clientExecutionPool.submit(clientTask);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moglan/eac/model/connection/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Monitor extends TCPServer {
private volatile int messageCount; // the total number of received messages throughout the server's up time

private Monitor() {
super(Config.PORT);
super(Config.SERVER_PORT);

messageCount = 0;
portAllocation = new HashMap<>();
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/moglan/eac/presentation/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Observable;
import java.util.Observer;

Expand All @@ -11,6 +13,7 @@
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.moglan.eac.application.Config;
import com.moglan.eac.application.Simulation;

import javax.swing.GroupLayout;
Expand All @@ -36,7 +39,6 @@
public class MainWindow extends JFrame implements Observer {

private static final long serialVersionUID = 1L;

private JPanel mainPanel;
private JProgressBar serverStatusProgressBar;
private JButton startSimulationButton;
Expand All @@ -61,8 +63,15 @@ public static void main(String[] args) {
}

EventQueue.invokeLater(new Runnable() {
@SuppressWarnings("resource")
public void run() {
try {
/**
* The {@code ServerSocket} instance creation is used for ensuring that only one
* instance of the app is running at a time.
*/

new ServerSocket(Config.APP_PORT, 0, InetAddress.getByAddress(new byte[] {127,0,0,1}));
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void globalCountTest() {
* Starting dummy server
*/

server = new DummyServer(Config.PORT);
server = new DummyServer(Config.SERVER_PORT);

server.start();

Expand All @@ -49,7 +49,7 @@ void globalCountTest() {
*/

for (int i = 0; i < MAX_CLIENTS; i++) {
Runnable runnable = new DummyClient(i, LOCALHOST, Config.PORT);
Runnable runnable = new DummyClient(i, LOCALHOST, Config.SERVER_PORT);
Future<?> future = executionPool.submit(runnable);

futures.add(future);
Expand Down Expand Up @@ -96,7 +96,7 @@ void globalCountTest() {
void serverShutdownTest() {
TCPServer server = null;

server = new DummyServer(Config.PORT);
server = new DummyServer(Config.SERVER_PORT);

server.start();

Expand Down

0 comments on commit 43196e7

Please sign in to comment.