Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
117 lines (104 loc) · 2.65 KB
/
Copy pathServer.java
File metadata and controls
117 lines (104 loc) · 2.65 KB
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
import java.net.*;
import java.io.*;
import java.util.*;
public class Server{
private ServerSocket socServ;
private ArrayList<Player> players;
private ArrayList<Socket> sockets;
private Scores score;
private ArrayList<Triplet> arrivedList;
private final boolean debug = false;
public Server(ServerSocket socServ, ArrayList<String> names, ArrayList<Socket> sockets){
this.socServ=socServ;
players=new ArrayList();
for(String str:names){
players.add(new Player(str));
}
this.sockets=sockets;
score=new Scores();
arrivedList=new ArrayList<Triplet>();
for(int i=0;i<names.size();i++){
waitScore ws= new waitScore(sockets.get(i),players.get(i));
ws.start();
}
if(debug)
System.out.println("Server en marche");
sendPos sp2=new sendPos();
sp2.start();
}
private void sendScoreFinished(){
ArrayList<String> tmp = new ArrayList<String>();
for(Triplet t:arrivedList)
netFunc.sendObject(t.getSocket(),score);
}
private void sendEnd(){
for(Triplet t:arrivedList)
netFunc.sendObject(t.getSocket(),false);
}
private void sendPosPlayers(){
for(Socket soc:sockets){
netFunc.sendObject(soc,players);
}
}
private void printPlayer(){
for(Player p:players){
System.out.println(p.getName()+" :"+p.getPosition().toString());
}
}
class waitScore extends Thread {
private Socket soc;
private Player player;
public waitScore(Socket s, Player p) {
super();
soc = s;
player = p;
}
public void run() {
while(true) {
Object tmp = netFunc.readObject(soc);
if (debug)
System.out.println("Je recois un objet");
if (tmp == null) {
//netFunc.closeSocket(soc);
} else if (tmp instanceof Scores) {
if (debug)
System.out.println(player.getName() + " est arrivé.");
Triplet t = new Triplet(soc, (Scores) tmp, player);
score.add((Scores) tmp);
arrivedList.add(t);
players.remove(player);
sockets.remove(soc);
sendScoreFinished();
} else if (tmp instanceof Player) {
Player p = (Player) tmp;
player.setPosition(p.getPosition(), p.orientation());
if (debug) {
System.out.println("--------------------------------------------");
System.out.println("J'ai recu info Position de " + p.getName());
System.out.println(p.getPosition());
System.out.println(player.getPosition());
}
}
if (sockets.isEmpty())
sendEnd();
}
}
}
public class sendPos extends Thread{
public sendPos(){
super();
}
public void run(){
while(true){
sendPosPlayers();
if(true)
printPlayer();
try {
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}