-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
131 lines (98 loc) · 3.89 KB
/
Server.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
import com.rabbitmq.client.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeoutException;
public class Server {
private File file;
private Connection connection;
private Channel channel;
private static final String EXCHANGE_ROOM = "generalTopic";
private static final String EXCHANGE_SERVER = "serverTopic";
private static final String QUEUE_SERVER = "serverQueue";
private static String historique;
public Server() throws IOException, TimeoutException {
file = new File("historique.txt");
if (! file.exists()){
file.createNewFile();
historique = "";
updateHistorique("\n********************* CHAT HISTORY *********************");
}
else{
historique = loadHistorique();
}
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_SERVER, "topic");
channel.queueDeclare(QUEUE_SERVER, false, false, false, null);
}
public static void main(String[] argv) throws Exception {
try {
Server server = new Server();
System.out.println("************************************************************");
System.out.println("* IDS : RABBITMQ LAB *");
System.out.println("* CHAT SERVER *");
System.out.println("************************************************************\n");
//CallBack when a message is consumed;
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
if(message.endsWith(", welcome to our chat server !")) {
String[] params = message.split(" ");
String endhistorique= "\n********************************************************\n";
server.channel.basicPublish(EXCHANGE_SERVER, params[1], null, (historique+endhistorique).getBytes("UTF-8"));
}
else {
updateHistorique(message);
System.out.println(message);
}
};
//Binding queue to our exchanges:
server.channel.queueBind(QUEUE_SERVER, EXCHANGE_ROOM, "");
//start consuming callback
server.channel.basicConsume(QUEUE_SERVER, true, deliverCallback, consumerTag -> { });
boolean connected = true;
while(connected){
}
}
catch(Exception e){
}//try end
}//main end
private String SendHistorique(String type, String message, String dest) throws IOException{
String res = "";
try (BufferedReader b = new BufferedReader(new FileReader("historique.txt"))) {
String str;
Integer suiv;
while((str=b.readLine())!=null){
res=res+str+"\n";
if(res.length() > 1000){
suiv = res.indexOf("\n");
res = res.substring(suiv+1);
}
}
}
return res;
}
private static String loadHistorique() throws IOException{
String res = "";
try (BufferedReader b = new BufferedReader(new FileReader("historique.txt"))) {
String str;
Integer suiv;
while((str=b.readLine())!=null){
res=res+str+"\n";
if(res.length() > 1000){
suiv = res.indexOf("\n");
res = res.substring(suiv+1);
}
}
}
return res;
}
private static void updateHistorique(String message)throws IOException{
try (PrintWriter p = new PrintWriter(new FileWriter("historique.txt",true))) {
historique = historique + "\n"+ message;
p.println(message);
}
}
}