-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.java
148 lines (133 loc) · 3.25 KB
/
chat.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
import java.net.*;
import java.util.*;
import java.io.*;
import javafx.application.*;
import javafx.event.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
public class chat{
private boolean hote=false;
private LinkedList<Socket> sockets;
private ServerSocket socServ;
private Socket me;
private String myName;
private waitClients waitCli;
private LinkedList<String> messages;
private chatPane pan;
private boolean newMessage = false;
private static int PORT = 1235;
public chat(chatPane pan){
this.pan=pan;
}
public void close(){
if(hote){
waitCli.stop();
for(Socket soc:sockets){
netFunc.closeSocket(soc);
}
}
netFunc.closeSocket(me);
}
public void initHost(String name){
hote=true;
sockets=new LinkedList<Socket>();
try{
socServ = new ServerSocket(PORT);
sockets = new LinkedList<Socket>();
initMe(name,"localHost");
getClient();
}catch(IOException e){e.printStackTrace();}
}
public void initClient(String name,String addr){
initMe(name,addr);
}
public void getClient(){
waitCli = new waitClients();
waitCli.start();
}
public void initMe(String name, String addr){
myName=name;
messages=new LinkedList<String>();
try{
me=new Socket(addr, PORT);
waitNewMessages wnm= new waitNewMessages(me,false);
wnm.start();
}catch(IOException e){}
}
public void sendMessage(String str){
netFunc.sendString(me,myName+" : "+str);
}
private class waitClients extends Thread{
private volatile boolean exit = false;
public void run(){
System.out.println("Attente de clients...");
while(!exit){
try{
Socket tmp = socServ.accept();
if(!exit){
sockets.add(tmp);
System.out.println("Un nouveau client a été ajouté.");
waitNewMessages wnm= new waitNewMessages(tmp,true);
wnm.start();
}else
tmp.close();
}catch(Exception e){}
}
}
}
private class waitNewMessages extends Thread{
private Socket soc;
private boolean isHote;
private waitNewMessages(Socket soc, boolean isHote){
super();
this.soc=soc;
this.isHote=isHote;
}
public void run(){
while(true){
String str = netFunc.readString(soc);
if(str==null) {
netFunc.closeSocket(soc);
sockets.remove(soc);
}else{
newMessage=true;
System.out.println("J'ai recu un message.");
if(isHote){
System.out.println("Je suis hote et j'envoie un msg");
for(Socket tmp:sockets){
netFunc.sendString(tmp,str);
}
}else{
System.out.println(str);
messages.add(str);
Platform.runLater(()-> pan.miseAjour());
}
}
}
}
}
public String getNewMessage(){
if(newMessage){
System.out.println("J'ai un newMess");
newMessage=false;
return messages.getLast();
}
return null;
}
public LinkedList<String> getMessages(){return messages;}
/*public static void main(String[] args) {
chat tmp = new chat();
if(args.length<1){
tmp.initHost("hote");
}else{
tmp.initClient("client", args[0]);
}
while(true){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
tmp.sendMessage(str);
}
}*/
}