Wed Jun 10, 2009 6:03 pm
/*this class implements the RMI example client, that calls the
*Remote Methods, from the server.
*/
//java rmi imports
import java.io.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import java.rmi.AlreadyBoundException;
import java.rmi.server.UnicastRemoteObject;
//swing imports
import java.util.Vector;
import javax.swing.JList;
import javax.swing.JTextArea;
public class RmiExampleClient implements RmiExampleClientMethods{
//stub
RmiExampleServerMethods stub;
//stubc
RmiExampleClientMethods stubc;
//registry, hostname and port
Registry registry;
String hostName = "localhost";
String username;
int port = 1099;
//chat are where to place text
JTextArea chatArea;
JList listArea;
//methods to make available
String[] methodsNames = {"Say Something", "ReceiveClients", "sendMsg", "receiveMsg","receiveFile"};
//clients online variable
Vector<String> usersOnline = new Vector<String>();
//private windows opened
Vector<Privado> openedPrivateWindows = new Vector<Privado>();
//Constructor Method
/*-------- ------------------------------------*/
public RmiExampleClient(String hostname, int port, JTextArea chatArea, JList listArea, String username) {
//update global variables
this.hostName = hostname;
this.port = port;
this.chatArea = chatArea;
this.listArea = listArea;
this.username = username;
}
/*-------------------- ------------------------------*/
// User methods
/*--------------------- ---------------------------*/
public void run() throws RemoteException, NotBoundException, AlreadyBoundException {
//declare stubc - Client stub
stubc = (RmiExampleClientMethods) UnicastRemoteObject.exportObject(this, 0);
//get RMI registry information
registry = LocateRegistry.getRegistry(hostName, port);
//register client methods
registerMethods();
}
public void checkMethod(String methodName) throws RemoteException, NotBoundException {
//check RMI Remote Method - first look for the service name in the registry
stub = (RmiExampleServerMethods) registry.lookup(methodName);
}
public String callGreatings() throws RemoteException {
//call the method
return (stub.serverHello());
}
public String callEcoGirl() throws RemoteException {
//call the method
return (stub.ecoGirl("Teste Eco!"));
}
public boolean callLogin(String username) throws RemoteException {
//call the method
return (stub.Login(username) );
}
public void callreceiveMsg(String username , String msg) throws RemoteException {
stub.ReceiveMsg(username, msg) ;
}
public void callreceivePrivateMsg(String username , String msg) throws RemoteException {
stubc.receivePrivateMessage(username, msg);
}
public void registerMethods() throws RemoteException, AlreadyBoundException {
//register all methods, if they are not alreay registered
for (int i = 0; i < methodsNames.length; i++) {
//register method named "x"
registry.bind(methodsNames[i]+ "_" + username, stubc);
}
}
//metodo que permite enviar um ficheiro
public boolean EnviarFicheiro(String destinatario, String nome){
try{
//procura o metodo clienteOnline
RmiExampleServerMethods stub = (RmiExampleServerMethods) registry.lookup("clienteOnline");
//verifica se o destinatario esta online
if ( stub.clienteOnline(destinatario)) {
//procura o metodo para receber a ficheiro juntamente com o destinatario do ficheiro
stubc = (RmiExampleClientMethods) registry.lookup("ReceberFicheiro" + destinatario);
//create file object
File file = new File("imagem.jpg");
//ler do ficheiro que se quer enviar
InputStream fin = new FileInputStream(file);
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int) file.length()];
/*
* To read content of the file in byte array, use
* int read(byte[] byteArray) method of java FileInputStream class.
*
*/
fin.read(fileContent);
//chama o metodo para receber o ficheiro
stubc.ReceberFicheiro(nome, fileContent);
}
return (true);
}
catch(Exception e){
return false;
}
}
//metodo que permite receber um ficheiro e identifica o emmisor
public boolean ReceberFicheiro(String nome, byte[] data) throws RemoteException {
try {
// Open the file that is the first
// command line parameter
FileOutputStream fstream = new FileOutputStream(nome);
// Get the object of DataInputStream
DataOutputStream out = new DataOutputStream(fstream);
//write to file
out.write(data, 0, data.length);
//close file
out.close();
fstream.close();
} catch (Exception e) {//Catch exception if any
chatArea.append("Error Receiving file: " + e.getMessage());
return (false);
}
return(true);
}
/*----------------------------------------------*/
//RMI methods implementation
/*--------------------------------------------*/
//RMI Method that returns a message string
public String clientHello() {
//update text box
chatArea.append("Method " + methodsNames[0] + "was called.\n");
return ("Hello from the Client");
}
public boolean receiveClients(Vector usersOnline){
if (usersOnline != null){
this.usersOnline = usersOnline;
//print the users list in GUI
printClientList(usersOnline);
return (true);
}
else{
//vector sent was empty
return false;
}
}
public void sendMensageAll(String msg)
{
}
private void printClientList(Vector usersOnline){
listArea.setListData(usersOnline);
}
public void receiveMessage(String username, String message){
if( message.trim().length() != 0){
chatArea.append(username + message);
}
}
public boolean receivePrivateMessage(String username,String message){
//check if there is already a opened windows for this username
for ( int i=0; i< openedPrivateWindows.size(); i++ ){
//check username
if ( openedPrivateWindows.elementAt(i).getUsername().equals(username)){
//send message to window
openedPrivateWindows.elementAt(i).receiveMessage(message);
return true;
}
}
//there is no opened window! Let's make one
openedPrivateWindows.addElement( new Privado(username, this) );
//send message to window
openedPrivateWindows.elementAt( openedPrivateWindows.size() ).receiveMessage(message);
return false;
}
/*public void SendList(){
listArea.append("vector");
}
*/
public String sendMsg(String msg) {
//update text box
chatArea.append("Method " + methodsNames[0] + "was called.\n");
return (msg);
}
/*---------------------------------------------*/
}
Thu Jun 11, 2009 7:47 pm
Fri Jun 12, 2009 9:22 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.