-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCentreCalcule.java
205 lines (192 loc) · 8.32 KB
/
CentreCalcule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import raytracer.*;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static java.lang.Thread.sleep;
public class CentreCalcule implements ServiceCentreCalcule {
List<FabricateurScene> fabricateurScenes;
List<ServiceScene> scenes;
int tailleParti = 10;
public CentreCalcule() {
scenes = new ArrayList<>();
fabricateurScenes = new ArrayList<>();
}
@Override
public synchronized void addCalculeur(FabricateurScene fabricateurScene) throws RemoteException {
fabricateurScenes.add(fabricateurScene);
System.out.println("Ajouter un calculeur: " + fabricateurScene.toString());
}
public synchronized void calculer(ServiceDisp disp, Scene scene) throws RemoteException, InterruptedException {
/* Arreter les services de calcul*/
Iterator<FabricateurScene> fabriquateurSceneIterator = fabricateurScenes.iterator();
List<Thread> threads = new ArrayList<>();
while (fabriquateurSceneIterator.hasNext()) {
FabricateurScene fabricateurScene = fabriquateurSceneIterator.next();
Thread thread = new Thread(() -> {
try {
if (fabricateurScene !=null) fabricateurScene.stop();
} catch (ConnectException connectException) {
fabricateurScenes.remove(fabricateurScene);
System.out.println("Enlever un calculeur: " + fabricateurScene);
} catch (RemoteException e) {
e.printStackTrace();
}
});
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
/* Convertir les services de calcul*/
scenes = new ArrayList<>();
fabriquateurSceneIterator = fabricateurScenes.iterator();
threads = new ArrayList<>();
while (fabriquateurSceneIterator.hasNext()) {
FabricateurScene fabricateurScene = fabriquateurSceneIterator.next();
Thread thread = new Thread(() -> {
try {
if (fabricateurScene != null)scenes.add(fabricateurScene.convertirService(scene));
} catch (ConnectException connectException) {
fabricateurScenes.remove(fabricateurScene);
System.out.println("Enlever un calculeur: " + fabricateurScene);
} catch (RemoteException e) {
e.printStackTrace();
}
});
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
if (scenes.size() == 0) {
System.out.println("Pas de calculeur disponible");
throw new ArithmeticException();
}
/*distribuer les taches de calcul*/
int nbLigne = scene.getHeight() / tailleParti;
int nbColone = scene.getWidth() / tailleParti;
List<int[]> tachesOmettre = new ArrayList<>();
threads = new ArrayList<>();
for (int i = 0; i < nbColone; i++) {
for (int j = 0; j < nbLigne; j++) {
ServiceScene serviceScene = scenes.get((i + j) % scenes.size());
int finalI = i;
int finalJ = j;
Thread thread = toThread(serviceScene, finalI, finalJ,tailleParti,tailleParti, disp, tachesOmettre);
thread.start();
threads.add(thread);
}
}
for (Thread thread : threads) {
thread.join();
}
while (tachesOmettre.size()>0) {
threads = new ArrayList<>();
while (tachesOmettre.size() > 0) {
int[] tache = tachesOmettre.get(0);
tachesOmettre.remove(0);
ServiceScene serviceScene = scenes.get((tache[0] + tache[1]) % scenes.size());
int finalI = tache[0];
int finalJ = tache[1];
Thread thread = toThread(serviceScene, finalI, finalJ,tailleParti,tailleParti, disp, tachesOmettre);
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
}
/*si la taille de l'image n'est pas un multiple de la taille des parties, completer l'image*/
threads = new ArrayList<>();
if(scene.getHeight()>nbLigne*tailleParti){
for (int i=0;i<nbColone;i++){
ServiceScene sceneI = scenes.get(i % scenes.size());
int finalI = i;
List<int[]> finalTachesOmettre = tachesOmettre;
Thread thread=toThread(sceneI, finalI, nbLigne,tailleParti,scene.getHeight()%tailleParti, disp, finalTachesOmettre);
thread.start();
threads.add(thread);
}
}
for (Thread thread : threads) {
thread.join();
}
while (tachesOmettre.size()>0) {
threads = new ArrayList<>();
while (tachesOmettre.size() > 0) {
int[] tache = tachesOmettre.get(0);
tachesOmettre.remove(0);
ServiceScene sceneI = scenes.get(tache[0] % scenes.size());
Thread thread=toThread(sceneI, tache[0], tache[1],tailleParti,scene.getHeight()%tailleParti, disp, tachesOmettre);
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
}
threads = new ArrayList<>();
if(scene.getWidth()>nbColone*tailleParti){
for (int j = 0; j <nbLigne; j++){
int finalJ = j;
ServiceScene sceneI = scenes.get(finalJ % scenes.size());
Thread thread=toThread(sceneI, nbColone, finalJ,scene.getWidth()%tailleParti,tailleParti, disp, tachesOmettre);
thread.start();
threads.add(thread);
}
}
for (Thread thread : threads) {
thread.join();
}
while (tachesOmettre.size()>0) {
threads = new ArrayList<>();
while (tachesOmettre.size() > 0) {
int[] tache = tachesOmettre.get(0);
tachesOmettre.remove(0);
ServiceScene sceneI = scenes.get(tache[0] % scenes.size());
Thread thread=toThread(sceneI, tache[0], tache[1],scene.getWidth()%tailleParti,tailleParti, disp, tachesOmettre);
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
thread.join();
}
}
if(scene.getHeight()>nbLigne*tailleParti && scene.getWidth()>nbColone*tailleParti){
boolean complete = false;
while (!complete) {
try {
Image image = scenes.get(0).compute(nbColone * tailleParti, nbLigne * tailleParti, scene.getWidth() - nbColone * tailleParti, scene.getHeight() - nbLigne * tailleParti);
disp.setImage(image, nbColone * tailleParti, nbLigne * tailleParti);
complete = true;
} catch (ConnectException connectException) {
scenes.remove(scenes.get(0));
System.out.println("Enlever un service de calcul: " + scenes.get(0));
}
}
}
}
private Thread toThread(ServiceScene serviceScene,int finalI,int finalJ,int tailleX,int tailleY,ServiceDisp disp,List<int[]> tachesOmettre){
return new Thread(() -> {
Image image;
try {
image = serviceScene.compute(finalI * tailleParti, finalJ * tailleParti, tailleX, tailleY);
disp.setImage(image, finalI * tailleParti, finalJ * tailleParti);
} catch (RemoteException e) {
synchronized (scenes) {
if (scenes.contains(serviceScene)) {
scenes.remove(serviceScene);
System.out.println("Enlever un service de calcul: " + serviceScene);
}
}
synchronized (tachesOmettre) {
tachesOmettre.add(new int[]{finalI, finalJ});
}
}
});
}
}