chat fini
This commit is contained in:
22
README.md
22
README.md
@@ -19,12 +19,28 @@ Vous pouvez le partager avec votre binome et l'encadrant de TP.
|
||||
|
||||
## Rapport
|
||||
|
||||
Votre rapport doit être écrit ici en markdown.
|
||||
|
||||
Votre rapport doit être écrit ici en markdown. :/
|
||||
Vous trouverez la syntaxe de markdown ici : https://docs.gitlab.com/ee/user/markdown.html
|
||||
|
||||
Placez vos images dans le répertoire images si nécessaire.
|
||||
|
||||
### Binôme :
|
||||
Thibaut ROCHAS, Tuan Minh VU
|
||||
|
||||
### Introduction
|
||||
|
||||
On a utilisé CloudAMQP, qui semblait être la solution la plus simple pour commencer le TP rapidement.
|
||||
Les exemples ont tous été testés, avec notre serveur RabitMQ.
|
||||
Avec l’aide des exemples et du cours, on a pu faire la partie Date sans grande difficulté.
|
||||
Ensuite, le chat n’était pas très compliqué, on a fait le bonus espion.
|
||||
|
||||
### Déroulement du TP
|
||||
|
||||
#### Exemples
|
||||
|
||||
#### Date
|
||||
|
||||
#### Chat
|
||||
|
||||
|
||||
|
||||
## Faire des diagrammes
|
||||
|
||||
76
src/main/java/fr/istic/chat/clientChat.java
Normal file
76
src/main/java/fr/istic/chat/clientChat.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package fr.istic.chat;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.rabbitmq.client.AMQP;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.DeliverCallback;
|
||||
/*
|
||||
* & 'C:\Program Files\Java\jre1.8.0_431\bin\java.exe' '-cp' 'C:\Users\Thibaut\AppData\Local\Temp\cp_223j5v7zmzailham0c6y3u9rd.jar' 'fr.istic.chat.clientChat' sujet.test A
|
||||
*/
|
||||
public class clientChat {
|
||||
private static final String EXCHANGE_NAME = "chat";
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
|
||||
String canal = argv[0];
|
||||
String name = argv[1];
|
||||
System.out.println("canal : " + canal);
|
||||
System.out.println("name : " + name);
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
|
||||
|
||||
|
||||
receiveMessage(channel, canal, name);
|
||||
|
||||
sendMessage(connection ,channel, canal, name);
|
||||
}
|
||||
|
||||
|
||||
private static void receiveMessage(Channel channel, String canal, String name) throws Exception{
|
||||
|
||||
String queueName = channel.queueDeclare().getQueue();
|
||||
channel.queueBind(queueName, EXCHANGE_NAME, canal);
|
||||
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
|
||||
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||
String message = new String(delivery.getBody(), "UTF-8");
|
||||
Object senderName = (delivery.getProperties().getHeaders().get("sender"));
|
||||
String routing = delivery.getEnvelope().getRoutingKey();
|
||||
System.out.println(routing+"#"+senderName+">" + message);
|
||||
};
|
||||
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
|
||||
});
|
||||
}
|
||||
|
||||
private static void sendMessage(Connection connection, Channel channel, String canal, String name) throws Exception{
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("sender", name);
|
||||
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().headers(headers).build();
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
boolean test = true;
|
||||
while(test){
|
||||
String message = scanner.nextLine();
|
||||
test = message.compareTo("\\exit")!=0;
|
||||
if(test){
|
||||
channel.basicPublish(EXCHANGE_NAME, canal, props, message.getBytes("UTF-8"));
|
||||
//System.out.println("<- ["+name+"](vous) : "+"'" + message + "'");
|
||||
}
|
||||
}
|
||||
channel.close();
|
||||
connection.close();
|
||||
scanner.close();
|
||||
System.out.println("exit");
|
||||
}
|
||||
}
|
||||
35
src/main/java/fr/istic/chat/espion.java
Normal file
35
src/main/java/fr/istic/chat/espion.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package fr.istic.chat;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.DeliverCallback;
|
||||
|
||||
public class espion {
|
||||
private static final String EXCHANGE_NAME = "chat";
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
|
||||
|
||||
|
||||
|
||||
String queueName = channel.queueDeclare().getQueue();
|
||||
channel.queueBind(queueName, EXCHANGE_NAME, "#");
|
||||
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
|
||||
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||
String message = new String(delivery.getBody(), "UTF-8");
|
||||
Object senderName = (delivery.getProperties().getHeaders().get("sender"));
|
||||
String routing = delivery.getEnvelope().getRoutingKey();
|
||||
System.out.println(routing+"#"+senderName+">" + message);
|
||||
};
|
||||
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,32 +13,28 @@ public class EnvoyerDate {
|
||||
public static void main(String[] argv) throws Exception {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
try (Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel()) {
|
||||
while(true){
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
|
||||
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
|
||||
while(true){
|
||||
|
||||
String message = argv.length < 1 ? getDate()
|
||||
: String.join(" ", argv);
|
||||
String message = argv.length < 1 ? getDate()
|
||||
: String.join(" ", argv);
|
||||
|
||||
channel.basicPublish(EXCHANGE_NAME, "locale", null, message.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
channel.basicPublish(EXCHANGE_NAME, "locale", null, message.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
|
||||
String messageGMT = argv.length < 1 ? getDateGMT()
|
||||
: String.join(" ", argv);
|
||||
String messageGMT = argv.length < 1 ? getDateGMT()
|
||||
: String.join(" ", argv);
|
||||
|
||||
channel.basicPublish(EXCHANGE_NAME, "gmt", null, messageGMT.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + messageGMT + "'");
|
||||
channel.basicPublish(EXCHANGE_NAME, "gmt", null, messageGMT.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + messageGMT + "'");
|
||||
|
||||
try {
|
||||
Thread.sleep(1000); // Attend 1 seconde (1000 millisecondes)
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Thread.sleep(1000); // Attend 1 seconde (1000 millisecondes)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.DeliverCallback;
|
||||
|
||||
|
||||
public class RecevoirDate {
|
||||
private static final String EXCHANGE_NAME = "direct_logs";
|
||||
|
||||
@@ -13,8 +14,8 @@ public class RecevoirDate {
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
|
||||
|
||||
String queueName = channel.queueDeclare().getQueue();
|
||||
//la clé: locale
|
||||
channel.queueBind(queueName, EXCHANGE_NAME, "locale");
|
||||
|
||||
@@ -13,33 +13,31 @@ public class EnvoyerDate {
|
||||
public static void main(String[] argv) throws Exception {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
try (Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel()) {
|
||||
while(true){
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
|
||||
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
|
||||
while(true){
|
||||
|
||||
String message = argv.length < 1 ? getDate()
|
||||
: String.join(" ", argv);
|
||||
String message = argv.length < 1 ? getDate()
|
||||
: String.join(" ", argv);
|
||||
|
||||
channel.basicPublish(EXCHANGE_NAME, "date.locale", null, message.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
channel.basicPublish(EXCHANGE_NAME, "date.locale", null, message.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + message + "'");
|
||||
|
||||
String messageGMT = argv.length < 1 ? getDateGMT()
|
||||
: String.join(" ", argv);
|
||||
String messageGMT = argv.length < 1 ? getDateGMT()
|
||||
: String.join(" ", argv);
|
||||
|
||||
channel.basicPublish(EXCHANGE_NAME, "date.gmt", null, messageGMT.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + messageGMT + "'");
|
||||
|
||||
try {
|
||||
Thread.sleep(1000); // Attend 1 seconde (1000 millisecondes)
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
channel.basicPublish(EXCHANGE_NAME, "date.gmt", null, messageGMT.getBytes("UTF-8"));
|
||||
System.out.println(" [x] Sent '" + messageGMT + "'");
|
||||
|
||||
try {
|
||||
Thread.sleep(1000); // Attend 1 seconde (1000 millisecondes)
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDate(){
|
||||
|
||||
32
src/main/java/fr/istic/date/topic/RecevoirToutesDate.java
Normal file
32
src/main/java/fr/istic/date/topic/RecevoirToutesDate.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package fr.istic.date.topic;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.DeliverCallback;
|
||||
|
||||
public class RecevoirToutesDate {
|
||||
private static final String EXCHANGE_NAME = "topic_log";
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setUri("amqps://cyvthtfj:O8LmaXkX5mVB0oFZN9TobaK8rX9wEhol@whale.rmq.cloudamqp.com/cyvthtfj");
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
|
||||
String queueName = channel.queueDeclare().getQueue();
|
||||
//la clé: date
|
||||
channel.queueBind(queueName, EXCHANGE_NAME, "date.#");
|
||||
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
|
||||
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||
String message = new String(delivery.getBody(), "UTF-8");
|
||||
System.out.println(" [x] Received '" + message + "'");
|
||||
};
|
||||
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user