Skip to content

Commit

Permalink
Merge branches 'feature_connections_concurrent_list' and 'master' of h…
Browse files Browse the repository at this point in the history
  • Loading branch information
markusranda committed Nov 14, 2019
2 parents 8b49220 + 6ac061c commit 9f52005
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 27 additions & 11 deletions server/src/main/java/no/ntnu/trostespel/model/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static no.ntnu.trostespel.model.ConnectionStatus.CONNECTED;
import static no.ntnu.trostespel.model.ConnectionStatus.DISCONNECTED;

public class Connection {

private InetAddress address;
private int port;
private final InetAddress address;
private final int port;
private DatagramSocket clientSocket;
private double timeArrived;

private String username;
private long pid;
private final String username;
private final long pid;
private static AtomicLong idCounter = new AtomicLong(100);
private ConnectionStatus connectionStatus;
private volatile ConnectionStatus connectionStatus;
private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();

public Connection(InetAddress address, int port, String username) {
this.address = address;
Expand Down Expand Up @@ -63,7 +67,23 @@ public int getPort() {
}

public void setDisconnected() {
this.connectionStatus = DISCONNECTED;
Lock writeLock = reentrantReadWriteLock.writeLock();
writeLock.lock();
try {
this.connectionStatus = DISCONNECTED;
} finally {
writeLock.unlock();
}
}

public ConnectionStatus getConnectionStatus() {
Lock readLock = reentrantReadWriteLock.readLock();
readLock.lock();
try {
return connectionStatus;
} finally {
readLock.unlock();
}
}

public static long createID() {
Expand All @@ -72,10 +92,6 @@ public static long createID() {

@Override
public String toString() {
return super.toString() + "[" + this.clientSocket + ", " + this.username + ", " + this.pid + "]";
}

public ConnectionStatus getConnectionStatus() {
return connectionStatus;
return super.toString() + "[" + this.clientSocket + ", " + this.username + ", " + this.pid + "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.ntnu.trostespel.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Connections {

Expand All @@ -18,7 +18,7 @@ public static Connections getInstance() {
}

private Connections() {
connections = new ArrayList<>();
connections = new CopyOnWriteArrayList<>();
}

public List<Connection> getConnections() {
Expand Down

0 comments on commit 9f52005

Please sign in to comment.