reformated everything.
it is not my fav formater, but at least its uniform, and its getting late. Good night!
This commit is contained in:
parent
14c72c3f7a
commit
abe0c16aa3
6 changed files with 52 additions and 49 deletions
|
@ -1,19 +1,19 @@
|
||||||
package usherbrooke.ift630;
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
static private int numLines = 3;
|
static private int numLines = 3;
|
||||||
static private int numBusPerLine = 2;
|
static private int numBusPerLine = 2;
|
||||||
static private int numBusses = numLines * numBusPerLine;
|
static private int numBusses = numLines * numBusPerLine;
|
||||||
static private int numStopPerBus = 8;
|
static private int numStopPerBus = 8;
|
||||||
static private int numStops = numBusses * numStopPerBus;
|
static private int numStops = numBusses * numStopPerBus;
|
||||||
static private int numPassengersPerBus = 5;
|
static private int numPassengersPerBus = 5;
|
||||||
static private int numPassengersPerStop = 5;
|
static private int numPassengersPerStop = 5;
|
||||||
//static private int numPassengers = numPassengersPerStop * numStops;
|
// static private int numPassengers = numPassengersPerStop * numStops;
|
||||||
static private int numThreadsBus = 5;
|
static private int numThreadsBus = 5;
|
||||||
static private int numThreadsStop = 2;
|
static private int numThreadsStop = 2;
|
||||||
static private int timeBetweenStops = 5;
|
static private int timeBetweenStops = 5;
|
||||||
static private int timeEmbark = 2;
|
static private int timeEmbark = 2;
|
||||||
static private int socketPort = 8181;
|
static private int socketPort = 8181;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -64,7 +64,7 @@ public class Bus extends Thread {
|
||||||
overMaxCapacity = currentCapacity >= maxCapacity;
|
overMaxCapacity = currentCapacity >= maxCapacity;
|
||||||
nextStopEmpty = getNextStopPassengers().isEmpty();
|
nextStopEmpty = getNextStopPassengers().isEmpty();
|
||||||
|
|
||||||
if (stopAsked())
|
if (stopAsked())
|
||||||
Logger.getInstance().print(id, "[BUS] " + name + " stop asked!");
|
Logger.getInstance().print(id, "[BUS] " + name + " stop asked!");
|
||||||
if (overMaxCapacity) {
|
if (overMaxCapacity) {
|
||||||
Logger.getInstance().print(id, "[BUS] " + name + " im full, sorry!");
|
Logger.getInstance().print(id, "[BUS] " + name + " im full, sorry!");
|
||||||
|
@ -141,15 +141,15 @@ public class Bus extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
Bus(Line line, int id, int timeStop, int timeEmbark, int maxCapacity) {
|
Bus(Line line, int id, int timeStop, int timeEmbark, int maxCapacity) {
|
||||||
this.timeBetweenStops = timeStop * 1000 + 1;
|
this.timeBetweenStops = timeStop * 1000 + 1;
|
||||||
this.timeEmbark = timeEmbark * 1000 + 1;
|
this.timeEmbark = timeEmbark * 1000 + 1;
|
||||||
this.name = "Bus n°" + id;
|
this.name = "Bus n°" + id;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.passengers = new ArrayList<Passenger>();
|
this.passengers = new ArrayList<Passenger>();
|
||||||
this.line = line;
|
this.line = line;
|
||||||
this.nextStop = line.get(0);
|
this.nextStop = line.get(0);
|
||||||
this.currentCapacity = 0;
|
this.currentCapacity = 0;
|
||||||
this.maxCapacity = maxCapacity;
|
this.maxCapacity = maxCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// disembark passenger at a stop. Synchronizes, locks on that stop, and waits.
|
// disembark passenger at a stop. Synchronizes, locks on that stop, and waits.
|
||||||
|
@ -219,7 +219,8 @@ public class Bus extends Thread {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// still there since i first didnt understand what a trip should have been. See git logs.
|
// still there since i first didnt understand what a trip should have been. See
|
||||||
|
// git logs.
|
||||||
public List<Stop> getStops() {
|
public List<Stop> getStops() {
|
||||||
return line.getStops();
|
return line.getStops();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package usherbrooke.ift630;
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
public class ExitException extends Exception {
|
public class ExitException extends Exception {
|
||||||
ExitException() {}
|
ExitException() {
|
||||||
|
}
|
||||||
|
|
||||||
public ExitException(String message) {
|
public ExitException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
|
@ -3,14 +3,14 @@ package usherbrooke.ift630;
|
||||||
public class Logger {
|
public class Logger {
|
||||||
private static Logger logger;
|
private static Logger logger;
|
||||||
|
|
||||||
private static final String RED = "\u001B[31m";
|
private static final String RED = "\u001B[31m";
|
||||||
private static final String GREEN = "\u001B[32m";
|
private static final String GREEN = "\u001B[32m";
|
||||||
private static final String YELLOW = "\u001B[33m";
|
private static final String YELLOW = "\u001B[33m";
|
||||||
private static final String BLUE = "\u001B[34m";
|
private static final String BLUE = "\u001B[34m";
|
||||||
private static final String PURPLE = "\u001B[35m";
|
private static final String PURPLE = "\u001B[35m";
|
||||||
private static final String CYAN = "\u001B[36m";
|
private static final String CYAN = "\u001B[36m";
|
||||||
private static final String RESET = "\u001B[0m" ;
|
private static final String RESET = "\u001B[0m";
|
||||||
private static final String WHITE = "\u001B[0m";
|
private static final String WHITE = "\u001B[0m";
|
||||||
|
|
||||||
private String[] cols = {
|
private String[] cols = {
|
||||||
WHITE,
|
WHITE,
|
||||||
|
@ -22,30 +22,30 @@ public class Logger {
|
||||||
CYAN,
|
CYAN,
|
||||||
};
|
};
|
||||||
|
|
||||||
private Logger() {}
|
private Logger() {
|
||||||
|
}
|
||||||
|
|
||||||
public void printTime() {
|
public void printTime() {
|
||||||
System.out.println(System.currentTimeMillis());
|
System.out.println(System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printTime(String s) {
|
public void printTime(String s) {
|
||||||
System.out.println(System.currentTimeMillis() + s);
|
System.out.println(System.currentTimeMillis() + s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printTime(int c, String s) {
|
public void printTime(int c, String s) {
|
||||||
System.out.println(cols[c % cols.length] +System.currentTimeMillis() + s + RESET);
|
System.out.println(cols[c % cols.length] + System.currentTimeMillis() + s + RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void print(int c, String s) {
|
public void print(int c, String s) {
|
||||||
System.out.println(cols[c % cols.length] + s + RESET);
|
System.out.println(cols[c % cols.length] + s + RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Logger getInstance() {
|
public static Logger getInstance() {
|
||||||
if(logger == null) {
|
if (logger == null) {
|
||||||
logger = new Logger();
|
logger = new Logger();
|
||||||
}
|
}
|
||||||
|
|
||||||
return logger;
|
return logger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,13 +20,12 @@ public class Stop extends Thread {
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private PrintWriter socketTx;
|
private PrintWriter socketTx;
|
||||||
|
|
||||||
|
|
||||||
private void connectSocket() throws IOException {
|
private void connectSocket() throws IOException {
|
||||||
socket = new Socket("localhost", socketPort);
|
socket = new Socket("localhost", socketPort);
|
||||||
socketTx = new PrintWriter(socket.getOutputStream(), true);
|
socketTx = new PrintWriter(socket.getOutputStream(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
private void sendCurrentCapacity() throws IOException {
|
private void sendCurrentCapacity() throws IOException {
|
||||||
connectSocket();
|
connectSocket();
|
||||||
// prone to over estimation. Some passenger will take other lines.
|
// prone to over estimation. Some passenger will take other lines.
|
||||||
|
@ -51,7 +50,8 @@ public class Stop extends Thread {
|
||||||
Logger.getInstance().print(id,
|
Logger.getInstance().print(id,
|
||||||
"\t".repeat(indent) + "[STOP] " + info.getBus(this).getNameBus() + " arrives in " + time + "s");
|
"\t".repeat(indent) + "[STOP] " + info.getBus(this).getNameBus() + " arrives in " + time + "s");
|
||||||
// else, catch
|
// else, catch
|
||||||
} catch (NullPointerException e) {}
|
} catch (NullPointerException e) {
|
||||||
|
}
|
||||||
|
|
||||||
} catch (UnauthorizedException e) {
|
} catch (UnauthorizedException e) {
|
||||||
// if unauth, requeue message.
|
// if unauth, requeue message.
|
||||||
|
@ -147,7 +147,7 @@ public class Stop extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removePassenger(Passenger p) throws IOException {
|
public void removePassenger(Passenger p) throws IOException {
|
||||||
if(passengers.remove(p)) {
|
if (passengers.remove(p)) {
|
||||||
synchronized (p) {
|
synchronized (p) {
|
||||||
p.notify();
|
p.notify();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package usherbrooke.ift630;
|
package usherbrooke.ift630;
|
||||||
|
|
||||||
public class UnauthorizedException extends Exception {
|
public class UnauthorizedException extends Exception {
|
||||||
public UnauthorizedException() {}
|
public UnauthorizedException() {
|
||||||
|
}
|
||||||
|
|
||||||
public UnauthorizedException(String message) {
|
public UnauthorizedException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
Loading…
Reference in a new issue