我目前正在尝试创建一个使用Proxy和Subject Observer模式的多播聊天程序,但我无法让它工作。我目前的问题是代理主题和具体主题没有沟通。我知道,代码有点粗糙,但在这里。
代理主题
package client;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.Vector;
import sitted.Message;
import sitted.Observer;
import sitted.Server;
import sitted.Subject;
public class ChatSubjectProxy implements Subject{
MulticastSocket sock;
DatagramSocket sendSock;
Vector<Observer> observers = new Vector<Observer>();
InetAddress ADDRESS;
int sendPort, recvPort;
public ChatSubjectProxy(InetAddress address, int recvPort, int sendPort){
this.sendPort = sendPort;
this.recvPort = recvPort;
try {
sendSock = new DatagramSocket();
ADDRESS = address;
sock = new MulticastSocket(recvPort);
sock.joinGroup(ADDRESS);
Thread t = new Thread(){
public void run(){
String msg = "";
while(true){
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
sock.receive(packet);
msg= new String(packet.getData());
if(msg!=null){
notifyObservers(recieve(msg));
}
} catch (IOException e) {
notifyObservers(new Message("SERVER","IO EXCEPTION: LOCAL PROXY"));
e.printStackTrace();
}
}
}
};
t.start();
} catch (UnknownHostException e) {
notifyObservers(new Message("SERVER","UNKNOWN HOST EXCEPTION: LOCAL PROXY"));
e.printStackTrace();
} catch (IOException e) {
notifyObservers(new Message("SERVER","IO EXCEPTION: LOCAL PROXY"));
e.printStackTrace();
}
}
@Override
public void remove(Observer obs) {
observers.remove(obs);
}
@Override
public void add(Observer obs) {
observers.add(obs);
}
@Override
public void notifyObservers(Message msg) {
for(Observer obs: observers){
obs.update(msg);
}
}
@Override
public void send(Message msg) {
String s = msg.getUser()+","+msg.getMessage();
byte[] buffer = s.getBytes();
System.out.println(s);
try {
DatagramPacket packet = new DatagramPacket(buffer,buffer.length,ADDRESS,sendPort);
sendSock.send(packet);
} catch (IOException e) {
notifyObservers(new Message("SERVER","IO EXCEPTION: LOCAL PROXY"));
e.printStackTrace();
}
}
public Message recieve(String s){
String token[] = s.split(",",2);
return(new Message(token[0],token[1]));
}
}
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;
import sitted.Message;
具体主题
public class ChatSubject{
Vector<Message> log = new Vector<Message>();
Vector<Socket> sockets = new Vector<Socket>();
Vector<BufferedReader> readers = new Vector<BufferedReader>();
Vector<PrintWriter> writers = new Vector<PrintWriter>();
String name;
PrintWriter writer;
DatagramSocket socket;
InetAddress address;
InetAddress returnAddr;
int port;
int sendPort;
public ChatSubject(String name, InetAddress address){
this.name = name;
this.address = address;
try {
socket = new DatagramSocket(0);
port = socket.getLocalPort();
System.out.println("SUBJECT PORT: "+port);
DatagramSocket temp = new DatagramSocket(0);
sendPort = temp.getLocalPort();
temp.setReuseAddress(true);
temp.close();
Thread t = new Thread(){
public void run(){
while(true){
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
socket.receive(packet);
returnAddr = packet.getAddress();
receiveMessage(new String(packet.getData()));
}catch (IOException e) {
e.printStackTrace();
sendMessage("SERVER: IO EXCEPTION");
}
}
}
};
t.start();
} catch (IOException e) {
sendMessage("SERVER: IO EXCEPTION");
e.printStackTrace();
}
}
public void receiveMessage(String s) throws IOException{
String token[] = s.split(",",2);
log.add(new Message(token[0],token[1]));
System.out.println(new Message(token[0],token[1]));
sendMessage(s);
}
public void sendMessage(String s){
try {
byte[] buffer = s.getBytes();
DatagramPacket packet = new DatagramPacket(buffer,buffer.length,returnAddr,sendPort);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
sendMessage("SERVER: IO EXCEPTION");
}
}
public String getName(){
return name;
}
public InetAddress getAddress(){
return address;
}
public int getSendPort(){
return sendPort;
}
public int getRecvPort(){
return port;
}
}