Hej alle sammen.
Jeg har en opgave som skal afleveres idag før kl. 24.00.
jeg er færdig med en del men har et problem som jeg ikke kan løse.
Opgaven er at jeg skal lave en chat system i java med RMI.
Jeg har lavet gui'en men jeg har problemer med "Send" knappen.
Når jeg skriver en tekst og sender det, skal teksten sendes til skærmen hvor alle kan se det. Men når jeg klikker på "Send" knappen sker der ikke noget. Jeg sætter pris på hvis der er nogen der hurtig kan hjælpe for jeg har ikke meget tid tilbage.
Hej alle sammen.
Jeg har en opgave som skal afleveres idag før kl. 24.00.
jeg er færdig med en del men har et problem som jeg ikke kan løse.
Opgaven er at jeg skal lave en chat system i java med RMI.
Jeg har lavet gui'en men jeg har problemer med "Send" knappen.
Når jeg skriver en tekst og sender det, skal teksten sendes til skærmen hvor alle kan se det. Men når jeg klikker på "Send" knappen sker der ikke noget. Jeg sætter pris på hvis der er nogen der hurtig kan hjælpe for jeg har ikke meget tid tilbage.
Koderne er:
package MessageExchangeSystem;
import java.rmi.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChooseUser extends JFrame implements ClientInterface{
private ServerInterface server;
private TextArea chatTextArea;
private JButton sendButton;
private JTextField chatTextField;
private String partnerUsername;
private String username;
public ChooseUser(String username, String partnerUsername, ServerInterface server){
super("Talking to " + username);
this.server = server;
this.partnerUsername = partnerUsername;
this.username = username;
this.createGUI();
//this.connect();
}
//FIX IT
public void passMessage(String l){
}
private void connect(){
try{
server = (ServerInterface)Naming.lookup("rmi://127.0.0.1:3333/Server");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
private void createGUI(){
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
sendButton = new JButton("Send");
chatTextArea = new TextArea();
chatTextField = new JTextField(30);
JPanel cp = new JPanel(new FlowLayout());
cp.add(chatTextField);
cp.add(sendButton);
add(chatTextArea, BorderLayout.NORTH);
add(cp, BorderLayout.CENTER);
sendButton.addActionListener(new ButtonHandler());
chatTextField.addActionListener(new ButtonHandler());
WindowListener windowListener = new WindowAdapter()
{
public void windowClosing ( WindowEvent w )
{
ChooseUser.this.dispose();
}
};
this.addWindowListener( windowListener );
pack();
setVisible(true);
}
//public static void main(String[] args){
//new ChooseUser(partnerUsername);
//}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == chatTextField || source == sendButton){
try{
server.sendToUser("[" + username + "]: " + chatTextField.getText(), partnerUsername);
}catch(Exception ex){
chatTextArea.append(ex.getMessage());
}
}
}
}
}
package MessageExchangeSystem;
import java.util.ArrayList;
public class ClientDB {
private ArrayList<ClientInfo> clients = new ArrayList<ClientInfo>();
public void addClient(ClientInfo ci){
clients.add(ci);
}
public boolean authenticate(String username, String password){
boolean ath = false;
for(int i = 0 ; i < clients.size(); i++){
if(clients.get(i).getUserName() == username){
if(clients.get(i).getPassword() == password){
ath = true;
}
}
}
return ath;
}
public void setActive(String username){
for(int i = 0 ; i<clients.size(); i++){
if(clients.get(i).getUserName() == username){
clients.get(i).setOnline();
}
}
}
public void logout(String username){
for(int i = 0 ; i<clients.size(); i++){
if(clients.get(i).getUserName() == username){
clients.get(i).setOffline();
}
}
}
public boolean clientExists(String name){
boolean h = false;
for(int i = 0 ; i<clients.size(); i++){
if(clients.get(i).getUserName()==name){
h = true;
}
}
return h;
}
public void addMessageToUser(String message, String user){
for(int i = 0; i < clients.size(); i++){
if(clients.get(i).getUserName() == user){
clients.get(i).addMessage(message);
}
}
}
public ClientInfo checkMessages(String username){
ClientInfo ret = null;
for(int i = 0; i < clients.size(); i++){
if(clients.get(i).getUserName() == username){
ret = clients.get(i);
}
}
return ret;
}
}
package MessageExchangeSystem;
import java.util.ArrayList;
public class ClientInfo {
private String userName;
private String password;
private boolean isOnline = false;
ArrayList<String> messages = new ArrayList<String>();
public ClientInfo(String userName, String password){
this.userName = userName;
this.password = password;
}
public void addMessage(String msg){
messages.add(msg);
}
public String getLastMessage(){
String a = messages.get(messages.size()-1);
messages.remove(messages.size()-1);
return a;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public void setOnline(){
isOnline = true;
}
public void setOffline(){
isOnline = false;
}
}
package MessageExchangeSystem;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ClientInterface extends Remote{
public void passMessage(String message)
throws RemoteException;
}
package MessageExchangeSystem;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame implements ClientInterface{
ServerInterface server;
private JTextField usernameField;
private JTextField passwordField;
private JButton loginButton;
private JButton registerButton;
public Login(){
super("Login");
createGUI();
connect();
}
public void createGUI(){
setSize(150,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
GridLayout g = new GridLayout(3,0);
JPanel f1 = new JPanel(new FlowLayout());
JPanel f2 = new JPanel(new FlowLayout());
JPanel f3 = new JPanel(new FlowLayout());
JPanel f4 = new JPanel(new FlowLayout());
JPanel mid = new JPanel();
JLabel loginLabel = new JLabel("LOGIN");
JLabel registerLabel = new JLabel("Do you want to register? Click here:");
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
usernameField = new JTextField(15);
passwordField = new JTextField(15);
loginButton = new JButton("Login");
registerButton = new JButton("Register");
f1.add(usernameLabel);
f1.add(usernameField);
f2.add(passwordLabel);
f2.add(passwordField);
f3.setAlignmentY(-1);
f3.add(loginButton);
f4.add(registerLabel);
f4.add(registerButton);
mid.setLayout(g);
mid.add(f1);
mid.add(f2);
mid.add(f3);
add(loginLabel,BorderLayout.NORTH);
add(mid, BorderLayout.CENTER);
add(f4, BorderLayout.SOUTH);
registerButton.addActionListener(new ButtonHandler());
loginButton.addActionListener(new ButtonHandler());
pack();
setVisible(true);
}
private void connect(){
try{
server = (ServerInterface)Naming.lookup("rmi://127.0.0.1:3333/Server");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public void passMessage(String l){}
public static void main(String[] args){
new Login();
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == registerButton){
new Register(Login.this, server);
Login.this.setVisible(false);
}
if(source == loginButton){
boolean log = true;
try{
log = server.login(usernameField.getText(), passwordField.getText());
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
if(log){
Client cl = new Client(usernameField.getText(), server);
try{
server.addOnlineClient(cl);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
Login.this.setVisible(false);
Login.this.dispose();
}else{
JOptionPane.showMessageDialog(null, "Login error, please try again!!!");
}
}
}
}
}
package MessageExchangeSystem;
import java.rmi.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Register extends JFrame{
private Login l;
private ServerInterface server;
private JButton registerButton;
private JTextField usernameField;
private JTextField passwordField;
public Register(Login l, ServerInterface server){
super("REGISTER");
this.l = l;
this.server = server;
createGUI();
//connect();
}
public void createGUI(){
setSize(150,150);
setLayout(new BorderLayout());
GridLayout g = new GridLayout(2,0);
JPanel f1 = new JPanel(new FlowLayout());
JPanel f2 = new JPanel(new FlowLayout());
JPanel mid = new JPanel();
JLabel registerLabel = new JLabel("REGISTER");
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
usernameField = new JTextField(15);
passwordField = new JTextField(15);
registerButton = new JButton("Register");
f1.add(usernameLabel);
f1.add(usernameField);
f2.add(passwordLabel);
f2.add(passwordField);
mid.setLayout(g);
mid.add(f1);
mid.add(f2);
add(registerLabel, BorderLayout.NORTH);
add(mid, BorderLayout.CENTER);
add(registerButton, BorderLayout.SOUTH);
registerButton.addActionListener(new ButtonHandler());
this.setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE );
WindowListener windowListener = new WindowAdapter()
{
public void windowClosing(WindowEvent w) {
l.setVisible(true);
Register.this.dispose();
}
};
this.addWindowListener( windowListener );
setVisible(true);
pack();
}
private void connect(){
try{
server = (ServerInterface)Naming.lookup("rmi://127.0.0.1:3333/Server");
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
boolean success = false;
try{
success = server.register(usernameField.getText(), passwordField.getText());
if(success){
JOptionPane.showMessageDialog(null, "REGISTRATION SUCCESSFUL!!!");
l.setVisible(true);
Register.this.dispose();
}else{
JOptionPane.showMessageDialog(null, "REGISTRATION FAILED, TRY AGAIN!!!");
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}
package MessageExchangeSystem;
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.rmi.registry.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Server extends UnicastRemoteObject implements ServerInterface{
ClientDB db;
ClientInfo temp = null;
ArrayList<Client> online = new ArrayList<Client>();
public Server() throws RemoteException{
System.out.println("Creating Server Object");
bindIt();
db = new ClientDB();
System.out.println("Server running.");
}
public void addOnlineClient(Client c){
online.add(c);
if(db.clientExists(c.getName())){
db.setActive(c.getName());
}
}
public void removeOnlineClient(Client c){
for(int i = 0; i < online.size(); i++){
if(online.get(i) == c){
online.remove(i);
}
}
}
public boolean userExists(String name){
boolean hh = false;
if(db.clientExists(name)){
hh = true;
}
return hh;
}
public boolean register(String username, String password) throws RemoteException,IOException{
boolean check = false;
temp = new ClientInfo( username, password );
db.addClient(temp);
if(db.authenticate(username, password)){
check = true;
}
temp = null;
return check;
}
public boolean login(String username, String password){
boolean check = false;
check = db.authenticate(username, password);
if(check){
db.setActive(username);
}
return check;
}
public void sendToAll(String message){
for(int i = 0; i<online.size(); i++){
online.get(i).passMessage(message);
}
}
private void bindIt(){
try{
Naming.bind("rmi://127.0.0.1:3333/Server", this);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
public void sendToUser(String message, String username){
db.addMessageToUser(message, username);
}
public ClientInfo checkMessages(String username){
ClientInfo a = db.checkMessages(username);
return a;
}
public static void main(String args[]) throws Exception{
java.rmi.registry.LocateRegistry.createRegistry(3333);
new Server();
}
}
package MessageExchangeSystem;
import java.rmi.*;
import java.io.*;
public interface ServerInterface extends Remote {
public boolean register(String username, String password)
throws RemoteException,IOException;
public boolean login(String username, String password)
throws RemoteException,IOException;
public void sendToAll(String message)
throws RemoteException,IOException;
public void addOnlineClient(Client c)
throws RemoteException,IOException;
public void removeOnlineClient(Client c)
throws RemoteException,IOException;
public boolean userExists(String name)
throws RemoteException,IOException;
public void sendToUser(String message, String username)
throws RemoteException,IOException;
public ClientInfo checkMessages(String username)
throws RemoteException,IOException;
}