Skip to content

Commit 447feb1

Browse files
author
Lara Rodrigues
committed
Merge remote-tracking branch 'origin/collectionsite' into main
2 parents 3dd240e + 34e80a0 commit 447feb1

File tree

11 files changed

+240281
-84
lines changed

11 files changed

+240281
-84
lines changed

logs/logging

Lines changed: 240002 additions & 0 deletions
Large diffs are not rendered by default.

src/entities/MasterThief.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,23 @@
77
import utils.MemException;
88

99
import static utils.Parameters.*;
10+
import static utils.Utils.logger;
1011

1112
public class MasterThief extends Thief {
13+
boolean sentAnyAssaultParty;
14+
15+
public boolean sentAnyAssaultParty() {
16+
return sentAnyAssaultParty;
17+
}
18+
19+
public void sentAnyAssaultParty(boolean sentAnyAssaultParty) {
20+
this.sentAnyAssaultParty = sentAnyAssaultParty;
21+
}
22+
1223
public MasterThief(String threadName, int thiefID, Museum museum, ConcentrationSite concentrationSite, CollectionSite collectionSite, AssaultParty[] assaultParties) throws MemException {
1324
super(threadName, thiefID, museum, concentrationSite, collectionSite, assaultParties);
1425
setThiefState(MasterThiefStates.PLANNING_HEIST);
26+
sentAnyAssaultParty = false;
1527
}
1628

1729
@Override
@@ -20,12 +32,18 @@ public void run() {
2032
lifecycle: while(true) {
2133
switch (collectionSite.appraiseSit()) {
2234
case CREATE_ASSAULT_PARTY:
35+
logger(this, "CREATE_ASSAULT_PARTY");
2336
int assaultPartyID = concentrationSite.prepareAssaultParty();
2437
assaultParties[assaultPartyID].sendAssaultParty();
2538
break;
2639
case WAIT_FOR_CANVAS:
40+
logger(this, "WAIT_FOR_CANVAS");
41+
collectionSite.takeARest();
42+
collectionSite.collectACanvas();
2743
break;
2844
case END_HEIST:
45+
logger(this, "END_HEIST");
46+
collectionSite.sumUpResults();
2947
break lifecycle;
3048
}
3149
}

src/entities/OrdinaryThief.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package entities;
22

3+
import genclass.GenericIO;
34
import sharedRegions.AssaultParty;
45
import sharedRegions.CollectionSite;
56
import sharedRegions.ConcentrationSite;
67
import sharedRegions.Museum;
78

89
import static utils.Parameters.*;
9-
import static utils.Utils.random;
10-
import static utils.Utils.max;
10+
import static utils.Utils.*;
1111

1212
public class OrdinaryThief extends Thief {
1313
private AssaultParty party;
1414
private boolean canIMove;
1515
private int position;
1616
private int displacement;
1717
private int movesLeft;
18-
18+
private boolean hasCanvas;
1919
public void joinParty(int partyID) {
2020
this.party = assaultParties[partyID];
2121
}
@@ -44,10 +44,6 @@ public int getDisplacement() {
4444
return displacement;
4545
}
4646

47-
public void setDisplacement(int displacement) {
48-
this.displacement = displacement;
49-
}
50-
5147
public int getMovesLeft() {
5248
return movesLeft;
5349
}
@@ -60,25 +56,31 @@ public void resetMovesLeft() {
6056
movesLeft = max(displacement, MAX_SEPARATION_LIMIT);
6157
}
6258

59+
public boolean hasCanvas() {
60+
return hasCanvas;
61+
}
62+
63+
public void hasCanvas(boolean hasCanvas) {
64+
this.hasCanvas = hasCanvas;
65+
}
66+
6367
public OrdinaryThief(String threadName, int thiefID, Museum museum, ConcentrationSite concentrationSite, CollectionSite collectionSite, AssaultParty[] assaultParties) {
6468
super(threadName, thiefID, museum, concentrationSite, collectionSite, assaultParties);
6569
thiefState = OrdinaryThiefStates.CONCENTRATION_SITE;
6670
displacement = random(MIN_DISPLACEMENT, MAX_DISPLACEMENT);
6771
resetMovesLeft();
68-
canIMove = false;
72+
canIMove = hasCanvas = false;
6973
}
7074

7175
@Override
7276
public void run() {
73-
while (true) {
74-
while (concentrationSite.amINeeded()) {
75-
concentrationSite.prepareExcursion();
76-
party.crawlIn();
77-
museum.rollACanvas(party.getId());
78-
party.reverseDirection();
79-
party.crawlOut();
80-
//collectionSite.handACanvas(assaultID);
81-
}
77+
while (concentrationSite.amINeeded()) {
78+
concentrationSite.prepareExcursion();
79+
party.crawlIn();
80+
museum.rollACanvas(party.getId());
81+
party.reverseDirection();
82+
party.crawlOut();
83+
collectionSite.handACanvas();
8284
}
8385
}
8486
}

src/entities/Thief.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public Museum getMuseum() {
3636
return museum;
3737
}
3838

39+
public ConcentrationSite getConcentrationSite() {
40+
return concentrationSite;
41+
}
42+
3943
public Thief(String threadName, int id, Museum museum, ConcentrationSite concentrationSite, CollectionSite collectionSite, AssaultParty[] assaultParties) {
4044
super(threadName);
4145

src/main/Assault.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(String[] args) throws MemException {
4646
// init shared regions
4747
repos = new GeneralRepos(logFile);
4848
collectionSite = new CollectionSite(repos);
49-
concentrationSite = new ConcentrationSite(N_THIEVES_ORDINARY, N_ASSAULT_PARTIES,N_THIEVES_PER_PARTY,N_ROOMS,repos);
49+
concentrationSite = new ConcentrationSite(repos);
5050
museum = new Museum(repos);
5151
assaultParties = new AssaultParty[N_ASSAULT_PARTIES];
5252
for (int i = 0; i < N_ASSAULT_PARTIES; i++)

src/sharedRegions/AssaultParty.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ public void setRoomID(int roomID) {
5555
repos.setRoomID(roomID);
5656
}
5757

58+
public void resetThieves() {
59+
thieves = new OrdinaryThief[N_THIEVES_PER_PARTY];
60+
nThieves = 0;
61+
nextThiefID = roomID = -1;
62+
}
63+
5864
public AssaultParty(int id, GeneralRepos repos) throws MemException {
5965
this.id = id;
60-
this.thieves = new OrdinaryThief[N_THIEVES_PER_PARTY];
61-
nThieves = 0;
62-
nextThiefID = roomID = -1;
6366
this.repos = repos;
67+
resetThieves();
6468
}
6569

6670
@Override
@@ -78,6 +82,7 @@ public synchronized void sendAssaultParty() {
7882
MasterThief master = (MasterThief) Thread.currentThread();
7983
master.setThiefState(MasterThiefStates.DECIDING_WHAT_TO_DO);
8084
GenericIO.writelnString("Sending " + this + " to Room " + roomID);
85+
master.sentAnyAssaultParty(true);
8186

8287
repos.setAssaultPartyID(id);
8388

@@ -104,7 +109,11 @@ public synchronized void reverseDirection() {
104109

105110
public synchronized void crawlIn() {
106111
OrdinaryThief thief = (OrdinaryThief) Thread.currentThread();
107-
thieves[nThieves++] = thief;
112+
113+
try {
114+
thieves[nThieves++] = thief;
115+
} catch (ArrayIndexOutOfBoundsException e) {}
116+
108117
thief.setThiefState(OrdinaryThiefStates.CRAWLING_INWARDS);
109118
repos.setOrdinaryThiefState(thief.getThiefID(), OrdinaryThiefStates.CRAWLING_INWARDS);
110119
repos.setOrdinaryThiefId(thief.getThiefID());
@@ -154,13 +163,15 @@ public synchronized void crawlOut() {
154163
}
155164

156165
private boolean crawl(OrdinaryThief thief, int goal, int endState, boolean backwards) {
157-
printPositions();
166+
//printPositions();
158167

159168
while(thief.canMove()) // move the most
160169
move(thief, goal, backwards);
161170

162-
if (!wakeUpNextThief(thief, backwards)) // wake up next thief
163-
printPositions(); // if last thief, print final positions
171+
//if (!wakeUpNextThief(thief, backwards)) // wake up next thief
172+
//printPositions(); // if last thief, print final positions
173+
wakeUpNextThief(thief, backwards);
174+
164175

165176
repos.setOrdinaryThiefPosition(thief.getThiefID(), thief.getPosition());
166177

@@ -196,7 +207,7 @@ private void move(OrdinaryThief thief, int goal, boolean backwards) {
196207
// make the move
197208
updatePosition(thief, distanceToMove, backwards);
198209
thief.setMovesLeft(thief.getMovesLeft() - distanceToMove);
199-
loggerCrawl(this, thief, "MOVED " + (backwards ? "-" : "") + distanceToMove + " positions to " + thief.getPosition());
210+
//loggerCrawl(this, thief, "MOVED " + (backwards ? "-" : "") + distanceToMove + " positions to " + thief.getPosition());
200211

201212
if (thief.getPosition() == goal) {
202213
noMoreMoves(thief);
@@ -233,7 +244,6 @@ private boolean wakeUpNextThief(OrdinaryThief thief, boolean backwards) {
233244
if (currentThief == nextThief) return false;
234245

235246
// only choose next thief if not all thieves are at the room
236-
GenericIO.writelnString("Counter: " + counter);
237247
if (counter != N_THIEVES_PER_PARTY) {
238248
nextThiefID = nextThief.getThiefID();
239249
nextThief.resetMovesLeft();
@@ -256,14 +266,14 @@ private boolean wakeUpNextThief(OrdinaryThief thief, boolean backwards) {
256266

257267
private boolean checkExcessMove(OrdinaryThief thief, int distanceToMove, boolean backwards) {
258268
OrdinaryThief newLowerThief = !backwards ? lowerThief(thief) : higherThief(thief);
259-
GenericIO.writelnString("new lower thief: " + newLowerThief);
269+
//GenericIO.writelnString("new lower thief: " + newLowerThief);
260270
int distanceToNextThief = Math.abs(newLowerThief.getPosition() - thief.getPosition());
261271
if (distanceToNextThief > MAX_SEPARATION_LIMIT) {
262272
int excessMove = distanceToMove - distanceToNextThief;
263273
// fix position
264274
updatePosition(thief, excessMove, !backwards);
265-
loggerCrawl(this, thief, "[EXCESS MOVE] fixing...");
266-
loggerCrawl(this, thief, "MOVED " + excessMove + " positions to "+ thief.getPosition());
275+
//loggerCrawl(this, thief, "[EXCESS MOVE] fixing...");
276+
//loggerCrawl(this, thief, "MOVED " + excessMove + " positions to "+ thief.getPosition());
267277
return true;
268278
}
269279
return false;
@@ -283,8 +293,8 @@ private boolean checkOccupiedMove(OrdinaryThief thief, boolean backwards) {
283293
while (inOccupiedPos(thief)) {
284294
wrongMove = true;
285295
updatePosition(thief, 1, !backwards);
286-
loggerCrawl(this, thief, "[MOVED TO OCCUPIED POSITION] fixing...");
287-
loggerCrawl(this, thief, "MOVED " + "1 positions to "+ thief.getPosition());
296+
//loggerCrawl(this, thief, "[MOVED TO OCCUPIED POSITION] fixing...");
297+
//loggerCrawl(this, thief, "MOVED " + "1 positions to "+ thief.getPosition());
288298
}
289299
return wrongMove;
290300
}

src/sharedRegions/CollectionSite.java

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,58 @@
22

33
import entities.MasterThief;
44
import entities.MasterThiefStates;
5+
import entities.OrdinaryThief;
6+
import genclass.GenericIO;
7+
import utils.MemException;
8+
import utils.MemFIFO;
9+
10+
import java.util.Arrays;
511

612
import static utils.Parameters.*;
13+
import static utils.Utils.*;
714

815
public class CollectionSite {
9-
private int collectedCanvas;
10-
private boolean endHeist;
16+
private MemFIFO<Integer> arrivedThieves;
17+
18+
/**
19+
* The number of thieves from each party that are in the site.
20+
*/
21+
private final int[] partyThievesInSite;
22+
private final boolean[] roomEmpty;
23+
24+
@Override
25+
public String toString() {
26+
return "Collection Site";
27+
}
1128

1229
public CollectionSite(GeneralRepos repos) {
13-
collectedCanvas = 0;
14-
endHeist = false;
30+
try {
31+
arrivedThieves = new MemFIFO<>(new Integer[N_THIEVES_ORDINARY]);
32+
} catch (MemException e) {}
33+
roomEmpty = new boolean[N_ROOMS];
34+
for (int i = 0; i < N_ROOMS; i++)
35+
roomEmpty[i] = false;
36+
partyThievesInSite = new int[N_ASSAULT_PARTIES];
37+
for (int i = 0; i < N_ASSAULT_PARTIES; i++)
38+
partyThievesInSite[i] = 0;
1539
}
1640

1741
public synchronized int appraiseSit() {
42+
MasterThief master = (MasterThief) Thread.currentThread();
43+
1844
// check if the heist should end
19-
if (endHeist) return END_HEIST;
45+
GenericIO.writelnString("Rooms : " + Arrays.toString(roomEmpty));
46+
if (all(roomEmpty)) {
47+
// update concentration site
48+
master.getConcentrationSite().endHeist(true);
49+
return END_HEIST;
50+
}
2051

2152
// check if it should wait for canvas
22-
// TODO
53+
if (master.sentAnyAssaultParty() && master.getConcentrationSite().numberOfThieves() < N_THIEVES_PER_PARTY) {
54+
GenericIO.writelnString("Thieves in site: " + master.getConcentrationSite().numberOfThieves());
55+
return WAIT_FOR_CANVAS;
56+
}
2357

2458
// otherwise, make more assault parties
2559
return CREATE_ASSAULT_PARTY;
@@ -29,22 +63,61 @@ public synchronized void takeARest() {
2963
MasterThief master = (MasterThief) Thread.currentThread();
3064
master.setThiefState(MasterThiefStates.WAITING_ARRIVAL);
3165

32-
//while (arrivedThieves == 0) {
33-
try {
34-
wait();
35-
} catch (InterruptedException e) {}
36-
//}
66+
while (arrivedThieves.size() == 0 && master.getConcentrationSite().numberOfThieves() < N_THIEVES_ORDINARY) {
67+
try {
68+
wait();
69+
} catch (InterruptedException e) {}
70+
}
3771
}
3872

39-
public static void handACanvas(int assaultID) {
73+
public synchronized void handACanvas() {
74+
OrdinaryThief thief = (OrdinaryThief) Thread.currentThread();
75+
if (!thief.hasCanvas()) {
76+
roomEmpty[thief.getParty().getRoomID()] = true;
77+
thief.getMuseum().getRoom(thief.getParty().getRoomID()).setAssaultPartyID(-1);
78+
}
79+
partyThievesInSite[thief.getParty().getId()]++;
80+
81+
// wake up master
82+
try {
83+
arrivedThieves.write(thief.getThiefID());
84+
notifyAll();
85+
} catch (MemException e) {}
86+
87+
// wait for master to collect canvas
88+
while (arrivedThieves.has(thief.getThiefID())) {
89+
try {
90+
wait();
91+
} catch (InterruptedException e) {}
92+
}
4093

94+
partyThievesInSite[thief.getParty().getId()]--;
95+
thief.hasCanvas(false);
96+
loggerCrawl(this, thief, "Handed canvas to master thief.");
97+
98+
// if last thief from party handing a canvas, free room
99+
if (partyThievesInSite[thief.getParty().getId()] == 0)
100+
((OrdinaryThief) Thread.currentThread()).getConcentrationSite().setRoomState(thief.getParty().getRoomID(), FREE_ROOM);
41101
}
42102

43-
public static void collectACanvas() {
103+
public synchronized void collectACanvas() {
104+
try {
105+
// collect a canvas by taking a thief from the queue
106+
arrivedThieves.read();
107+
} catch (MemException e) {}
44108

109+
// update rooms states
110+
for (int i = 0; i < N_ROOMS; i++) {
111+
if (roomEmpty[i])
112+
((MasterThief) Thread.currentThread()).getConcentrationSite().setRoomState(i, EMPTY_ROOM);
113+
}
45114
}
46115

47-
public static void sumUpResults() {
116+
public synchronized void sumUpResults() {
117+
MasterThief master = (MasterThief) Thread.currentThread();
118+
master.setThiefState(MasterThiefStates.PRESENTING_REPORT);
119+
GenericIO.writelnString("Master Thief is presenting the report.");
48120

121+
notifyAll();
49122
}
50123
}

0 commit comments

Comments
 (0)