implementata chat per cuori solitari
This commit is contained in:
58
rmi_chat_l_hearts/Client.java
Normal file
58
rmi_chat_l_hearts/Client.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.rmi.server.UnicastRemoteObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public class Client extends UnicastRemoteObject implements ClientInterface {
|
||||||
|
|
||||||
|
public Client( String iname, String isex, ServerInterface is) throws RemoteException {
|
||||||
|
name = iname;
|
||||||
|
sex = isex;
|
||||||
|
s = is;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void compagno(ClientInterface in) throws RemoteException {
|
||||||
|
compagno = in;
|
||||||
|
System.out.println("hai un nuovo partner");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dici(Message m) throws RemoteException {
|
||||||
|
System.out.println(m.getFrom() +": " + m.getM());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void quit(String name) throws RemoteException {
|
||||||
|
System.out.println(name + " non e' piu il tuo partner");
|
||||||
|
compagno = null;
|
||||||
|
setCompagno(s.iscriviti( (ClientInterface)this,sex));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientInterface getCompagno() {
|
||||||
|
return compagno;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSex() {
|
||||||
|
return sex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompagno(ClientInterface compagno) {
|
||||||
|
if (compagno != null) {
|
||||||
|
this.compagno = compagno;
|
||||||
|
System.out.println("hai un nuovo partner");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClientInterface compagno = null;
|
||||||
|
private final String name;
|
||||||
|
private final String sex;
|
||||||
|
private ServerInterface s;
|
||||||
|
}
|
||||||
22
rmi_chat_l_hearts/ClientInterface.java
Normal file
22
rmi_chat_l_hearts/ClientInterface.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import rmi_chat.*;
|
||||||
|
|
||||||
|
import java.rmi.Remote;
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public interface ClientInterface extends Remote {
|
||||||
|
|
||||||
|
|
||||||
|
public static final String UOMO = "UOMO";
|
||||||
|
public static final String DONNA = "DONNA";
|
||||||
|
|
||||||
|
void compagno (ClientInterface in ) throws RemoteException;
|
||||||
|
void dici (Message m) throws RemoteException;
|
||||||
|
void quit (String name) throws RemoteException;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
76
rmi_chat_l_hearts/LaunchClient.java
Normal file
76
rmi_chat_l_hearts/LaunchClient.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.rmi.Naming;
|
||||||
|
import java.rmi.NotBoundException;
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public class LaunchClient {
|
||||||
|
|
||||||
|
private final static String name = "alfio"; //instead of vm args
|
||||||
|
private static String sex; //instead of vm args
|
||||||
|
|
||||||
|
public static void main (String [] args) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
if (Math.random() >= 0.5)
|
||||||
|
sex = "UOMO";
|
||||||
|
else
|
||||||
|
sex = "DONNA";
|
||||||
|
|
||||||
|
System.out.println("Sono un " + sex + " e mi chiamo " +name);
|
||||||
|
ServerInterface s = (ServerInterface) Naming.lookup("rmi://localhost/server");
|
||||||
|
ClientInterface c = new Client(name,sex,s);
|
||||||
|
((Client)c).setCompagno(s.iscriviti(c, ((Client) c).getSex()));
|
||||||
|
|
||||||
|
BufferedReader i = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
while (true) {
|
||||||
|
String cmd = command(i);
|
||||||
|
if (!cmd.equals("!quit")) {
|
||||||
|
if ( ((Client)c).getCompagno() != null)
|
||||||
|
((Client)c).getCompagno().dici(new Message( ((Client)c).getName() , cmd ));
|
||||||
|
else
|
||||||
|
System.out.println("non hai ancora un partner. Impossibile inviare messaggi");
|
||||||
|
} else {
|
||||||
|
if ( ((Client)c).getCompagno() != null) {
|
||||||
|
((Client) c).getCompagno().quit(((Client) c).getName());
|
||||||
|
}else{
|
||||||
|
s.disiscriviti(c, ((Client) c).getSex());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (NotBoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String command (BufferedReader i){
|
||||||
|
try {
|
||||||
|
return i.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "!quit";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
28
rmi_chat_l_hearts/LaunchServer.java
Normal file
28
rmi_chat_l_hearts/LaunchServer.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.rmi.registry.LocateRegistry;
|
||||||
|
import java.rmi.registry.Registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public class LaunchServer{
|
||||||
|
|
||||||
|
|
||||||
|
public static void main (String[] args){
|
||||||
|
|
||||||
|
try {
|
||||||
|
ServerInterface s = new Server();
|
||||||
|
Registry r = LocateRegistry.getRegistry();
|
||||||
|
r.rebind("server",s);
|
||||||
|
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
26
rmi_chat_l_hearts/Message.java
Normal file
26
rmi_chat_l_hearts/Message.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 10/12/2014.
|
||||||
|
*/
|
||||||
|
public class Message implements Serializable{
|
||||||
|
|
||||||
|
public Message(String ifrom, String im){
|
||||||
|
from = ifrom;
|
||||||
|
m = im;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFrom() {
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getM() {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final String from;
|
||||||
|
private final String m;
|
||||||
|
|
||||||
|
}
|
||||||
73
rmi_chat_l_hearts/Server.java
Normal file
73
rmi_chat_l_hearts/Server.java
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.rmi.server.UnicastRemoteObject;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public class Server extends UnicastRemoteObject implements ServerInterface {
|
||||||
|
|
||||||
|
public Server () throws RemoteException{
|
||||||
|
u = new ArrayList<ClientInterface>();
|
||||||
|
d = new ArrayList<ClientInterface>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientInterface iscriviti(ClientInterface myself, String sex) throws RemoteException {
|
||||||
|
|
||||||
|
if (sex.equals(UOMO) && d.size() > 0 && u.size() == 0) {
|
||||||
|
ClientInterface donna = d.remove(0);
|
||||||
|
donna.compagno(myself);
|
||||||
|
System.out.println("L'ultimo uomo entrato ha subito ricevuto una donna");
|
||||||
|
return donna;
|
||||||
|
}
|
||||||
|
if (sex.equals(DONNA) && u.size() > 0 && d.size() == 0) {
|
||||||
|
ClientInterface uomo = u.remove(0);
|
||||||
|
uomo.compagno(myself);
|
||||||
|
System.out.println("L'ultima donna entrata ha subito ricevuto un uomo");
|
||||||
|
return uomo;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( sex.equals(UOMO)) {
|
||||||
|
u.add(myself);
|
||||||
|
System.out.println("Aggiunto un uomo");
|
||||||
|
|
||||||
|
}else {
|
||||||
|
d.add(myself);
|
||||||
|
System.out.println("Aggiunto una donna");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (u.size() > 0 && d.size() > 0){
|
||||||
|
ClientInterface uomo = u.remove(0);
|
||||||
|
ClientInterface donna = d.remove(0);
|
||||||
|
donna.compagno(uomo);
|
||||||
|
uomo.compagno(donna);
|
||||||
|
|
||||||
|
System.out.println("Accoppiato un uomo e una donna");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disiscriviti(ClientInterface myself, String sex) throws RemoteException {
|
||||||
|
if ( sex.equals(UOMO)) {
|
||||||
|
u.remove(myself);
|
||||||
|
System.out.println("Rimosso un uomo");
|
||||||
|
|
||||||
|
}else {
|
||||||
|
d.remove(myself);
|
||||||
|
System.out.println("Rimosso una donna");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayList<ClientInterface> u;
|
||||||
|
|
||||||
|
private ArrayList<ClientInterface> d;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
17
rmi_chat_l_hearts/ServerInterface.java
Normal file
17
rmi_chat_l_hearts/ServerInterface.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package rmi_chat_l_hearts;
|
||||||
|
|
||||||
|
import java.rmi.Remote;
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Giovanni on 11/12/2014.
|
||||||
|
*/
|
||||||
|
public interface ServerInterface extends Remote {
|
||||||
|
|
||||||
|
public static final String UOMO = "UOMO";
|
||||||
|
public static final String DONNA = "DONNA";
|
||||||
|
|
||||||
|
ClientInterface iscriviti (ClientInterface myself, String sex) throws RemoteException;
|
||||||
|
void disiscriviti (ClientInterface myself, String sex) throws RemoteException;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user