Transport UDP+ajout user à la liste
このコミットが含まれているのは:
コミット
9e8e9396ed
20個のファイルの変更、293行の追加、5行の削除
バイナリ
chat/bin/controller/Agent.class
ノーマルファイル
バイナリ
chat/bin/controller/Agent.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/controller/Test.class
ノーマルファイル
バイナリ
chat/bin/controller/Test.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/controller/package-info.class
ノーマルファイル
バイナリ
chat/bin/controller/package-info.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリファイルは表示されません。
バイナリファイルは表示されません。
バイナリ
chat/bin/model/Message.class
ノーマルファイル
バイナリ
chat/bin/model/Message.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/model/MessagePseudo.class
ノーマルファイル
バイナリ
chat/bin/model/MessagePseudo.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/network/BroadcastInput.class
ノーマルファイル
バイナリ
chat/bin/network/BroadcastInput.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/network/BroadcastOutput.class
ノーマルファイル
バイナリ
chat/bin/network/BroadcastOutput.class
ノーマルファイル
バイナリファイルは表示されません。
バイナリ
chat/bin/network/Manager.class
ノーマルファイル
バイナリ
chat/bin/network/Manager.class
ノーマルファイル
バイナリファイルは表示されません。
42
chat/src/controller/Agent.java
ノーマルファイル
42
chat/src/controller/Agent.java
ノーマルファイル
|
@ -0,0 +1,42 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
|
||||
import model.*;
|
||||
import network.*;
|
||||
|
||||
public class Agent {
|
||||
private Contact user;
|
||||
private BroadcastInput BIn;
|
||||
private BroadcastOutput BOut;
|
||||
private ListeContacts list;
|
||||
|
||||
|
||||
public Agent(InetAddress address, int portIn, int portOut) throws SocketException {
|
||||
this.user = new Contact("Bob", address, portIn);
|
||||
this.BIn = new BroadcastInput(user.getAddress(), portIn);
|
||||
this.BOut = new BroadcastOutput(user, portOut);
|
||||
this.list = ListeContacts.getInstance();
|
||||
}
|
||||
|
||||
|
||||
//Envoi sur 5001 et reçoit sur 5000
|
||||
public void testSendBroadcast() throws IOException, InterruptedException {
|
||||
BIn.start();
|
||||
MessagePseudo msg = new MessagePseudo(this.user.getAddress(), Tools.getLocalIp(), user.getPort(), 14000, 1, "Michel");
|
||||
BOut.send(msg, user.getAddress(), user.getPort());
|
||||
Thread.sleep(10);
|
||||
MessagePseudo msgRecu = BIn.getMessage();
|
||||
handleMessage(msgRecu);
|
||||
}
|
||||
|
||||
public void handleMessage(MessagePseudo msg) {
|
||||
if(!list.pseudoExist(msg.getPseudo())) {
|
||||
Contact newUser = new Contact(msg.getPseudo(), msg.getAddressSrc(), msg.getPortSrc());
|
||||
list.addContact(newUser);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
12
chat/src/controller/Test.java
ノーマルファイル
12
chat/src/controller/Test.java
ノーマルファイル
|
@ -0,0 +1,12 @@
|
|||
package controller;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import network.Tools;
|
||||
|
||||
public class Test {
|
||||
public static void main(String args[]) throws IOException, InterruptedException {
|
||||
Agent agent1 = new Agent(Tools.getLocalIp(), 14000, 14001);
|
||||
agent1.testSendBroadcast();
|
||||
}
|
||||
}
|
1
chat/src/controller/package-info.java
ノーマルファイル
1
chat/src/controller/package-info.java
ノーマルファイル
|
@ -0,0 +1 @@
|
|||
package controller;
|
|
@ -7,14 +7,16 @@ public class Contact {
|
|||
|
||||
private String pseudo;
|
||||
private String status;
|
||||
private int port;
|
||||
|
||||
private InetAddress address;
|
||||
|
||||
|
||||
public Contact(String pseudo, InetAddress address) {
|
||||
public Contact(String pseudo, InetAddress address, int port) {
|
||||
this.pseudo = pseudo;
|
||||
this.address = address;
|
||||
this.status = "En ligne";
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
|
@ -41,4 +43,12 @@ public class Contact {
|
|||
this.status = status;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,8 +21,8 @@ public class ListeContacts {
|
|||
return instance;
|
||||
}
|
||||
|
||||
public static void createUser(String pseudo) {
|
||||
user = new Contact(pseudo, Tools.getLocalIp());
|
||||
public static void createUser(String pseudo, int port) {
|
||||
user = new Contact(pseudo, Tools.getLocalIp(), port);
|
||||
}
|
||||
|
||||
public boolean contactExist(Contact contact) {
|
||||
|
@ -34,16 +34,25 @@ public class ListeContacts {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean pseudoExist(String pseudo) {
|
||||
for(Contact c : listeContact) {
|
||||
if(c.getPseudo().equals(pseudo)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addContact(Contact contact) {
|
||||
if(!contactExist(contact)){
|
||||
System.out.println("Pseudo : "+user.getPseudo()+"ajouté\n");
|
||||
System.out.println("Pseudo : "+contact.getPseudo()+" ajouté\n");
|
||||
listeContact.add(contact);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteContact(Contact contact) {
|
||||
if(contactExist(contact)){
|
||||
System.out.println("Pseudo : "+user.getPseudo()+"supprimé\n");
|
||||
System.out.println("Pseudo : "+contact.getPseudo()+"supprimé\n");
|
||||
listeContact.remove(contact);
|
||||
}
|
||||
}
|
||||
|
|
75
chat/src/model/Message.java
ノーマルファイル
75
chat/src/model/Message.java
ノーマルファイル
|
@ -0,0 +1,75 @@
|
|||
package model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.*;
|
||||
|
||||
public class Message implements Serializable{
|
||||
|
||||
protected InetAddress addressSrc;
|
||||
protected InetAddress addressDest;
|
||||
protected int portSrc;
|
||||
protected int portDest;
|
||||
protected int typeMessage;
|
||||
|
||||
|
||||
public Message(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage) {
|
||||
this.addressSrc = addressSrc;
|
||||
this.addressDest = addressDest;
|
||||
this.portSrc = portSrc;
|
||||
this.portDest = portDest;
|
||||
this.typeMessage = typeMessage;
|
||||
}
|
||||
|
||||
|
||||
public InetAddress getAddressSrc() {
|
||||
return addressSrc;
|
||||
}
|
||||
|
||||
|
||||
public void setAddressSrc(InetAddress addressSrc) {
|
||||
this.addressSrc = addressSrc;
|
||||
}
|
||||
|
||||
|
||||
public InetAddress getAddressDest() {
|
||||
return addressDest;
|
||||
}
|
||||
|
||||
|
||||
public void setAddressDest(InetAddress addressDest) {
|
||||
this.addressDest = addressDest;
|
||||
}
|
||||
|
||||
|
||||
public int getPortSrc() {
|
||||
return portSrc;
|
||||
}
|
||||
|
||||
|
||||
public void setPortSrc(int portSrc) {
|
||||
this.portSrc = portSrc;
|
||||
}
|
||||
|
||||
|
||||
public int getPortDest() {
|
||||
return portDest;
|
||||
}
|
||||
|
||||
|
||||
public void setPortDest(int portDest) {
|
||||
this.portDest = portDest;
|
||||
}
|
||||
|
||||
|
||||
public int getTypeMessage() {
|
||||
return typeMessage;
|
||||
}
|
||||
|
||||
|
||||
public void setTypeMessage(int typeMessage) {
|
||||
this.typeMessage = typeMessage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
23
chat/src/model/MessagePseudo.java
ノーマルファイル
23
chat/src/model/MessagePseudo.java
ノーマルファイル
|
@ -0,0 +1,23 @@
|
|||
package model;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class MessagePseudo extends Message{
|
||||
|
||||
private String pseudo;
|
||||
|
||||
public MessagePseudo(InetAddress addressSrc, InetAddress addressDest, int portSrc, int portDest, int typeMessage,String pseudo) {
|
||||
super(addressSrc, addressDest, portSrc, portDest, typeMessage);
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
|
||||
}
|
59
chat/src/network/BroadcastInput.java
ノーマルファイル
59
chat/src/network/BroadcastInput.java
ノーマルファイル
|
@ -0,0 +1,59 @@
|
|||
package network;
|
||||
|
||||
import model.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class BroadcastInput extends Thread{
|
||||
private DatagramSocket socket;
|
||||
private ArrayList<MessagePseudo> messageReceived;
|
||||
|
||||
public BroadcastInput(InetAddress localAddress, int port) throws SocketException {
|
||||
this.socket = new DatagramSocket(port, localAddress);
|
||||
this.socket.setBroadcast(true);
|
||||
this.messageReceived = new ArrayList<MessagePseudo>();
|
||||
}
|
||||
|
||||
|
||||
public MessagePseudo getMessage() {
|
||||
return this.messageReceived.remove(0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
byte[] buffer = new byte[5000];
|
||||
while(true) {
|
||||
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
|
||||
try {
|
||||
socket.receive(inPacket);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Packet UDP non reçu\n");
|
||||
e.printStackTrace();
|
||||
}
|
||||
/* Charset charset = StandardCharsets.US_ASCII;
|
||||
String pseudo = charset.decode(ByteBuffer.wrap(inPacket.getData())).toString();
|
||||
MessagePseudo msg = new MessagePseudo(null, null, 0, 0, 0, "");
|
||||
msg.setPseudo(pseudo);*/
|
||||
ByteArrayInputStream byteInStream = new ByteArrayInputStream(inPacket.getData());
|
||||
ObjectInputStream objectInStream;
|
||||
try {
|
||||
objectInStream = new ObjectInputStream(byteInStream);
|
||||
MessagePseudo msg = (MessagePseudo) objectInStream.readObject();
|
||||
messageReceived.add(msg);
|
||||
System.out.println("Message reçu, pseudo = "+msg.getPseudo()+" et num port = "+msg.getPortSrc()+"\n");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Objet pas créé");
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println("Objet pas lu");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
38
chat/src/network/BroadcastOutput.java
ノーマルファイル
38
chat/src/network/BroadcastOutput.java
ノーマルファイル
|
@ -0,0 +1,38 @@
|
|||
package network;
|
||||
import model.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.*;
|
||||
|
||||
|
||||
|
||||
|
||||
public class BroadcastOutput {
|
||||
private Contact user;
|
||||
private DatagramSocket socket;
|
||||
private int port;
|
||||
|
||||
public BroadcastOutput(Contact user, int port) throws SocketException {
|
||||
this.user = user;
|
||||
this.port = port;
|
||||
this.socket = new DatagramSocket(port, user.getAddress());
|
||||
this.socket.setBroadcast(true);
|
||||
}
|
||||
|
||||
|
||||
public void send(MessagePseudo msg, InetAddress addressDest, int portDest) throws IOException {
|
||||
byte[] buffer = "".getBytes();
|
||||
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream);
|
||||
objectOutStream.writeObject(msg);
|
||||
objectOutStream.close();
|
||||
buffer = byteOutStream.toByteArray();
|
||||
DatagramPacket packet = new DatagramPacket(buffer,buffer.length,msg.getAddressDest(),msg.getPortDest());
|
||||
this.socket.send(packet);
|
||||
System.out.println("Message envoyé, pseudo = "+msg.getPseudo()+" et num port destination = "+portDest+"\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
19
chat/src/network/Manager.java
ノーマルファイル
19
chat/src/network/Manager.java
ノーマルファイル
|
@ -0,0 +1,19 @@
|
|||
package network;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
|
||||
public class Manager {
|
||||
private static int port;
|
||||
|
||||
|
||||
private DatagramSocket broadcast;
|
||||
|
||||
private HashMap<String, Integer> portMap;
|
||||
|
||||
//network
|
||||
|
||||
|
||||
}
|
読み込み中…
新しいイシューから参照