import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.Registry;
import java.rmi.registry.*;
import java.rmi.registry.LocateRegistry;
import java.net.*;
import java.io.*;
public class ChatRMI extends javax.swing.JFrame {
/** Creates new form ChatRMI */
public ChatRMI() {
initComponents();
// Server();
// Cliente();
}
static class hilo extends Thread
{
public void run()
{
while(true)
{
try {message=exportObj.getMsg();} catch(RemoteException e){}
if( !(message.compareTo("")==0)){
jTextArea1.append(message+"n");
exportObj.MSG="";
}
}
}
}
public void Cliente(){
try{
int RMIPort;
// String portnum;
//portnum="8080";
RMIPort=Integer.parseInt(portnum);
String registryURL="rmi://"+hostName+":"+portnum+"/hello";
InterfaceChat h=(InterfaceChat)Naming.lookup(registryURL);
h.sendMsg(myname+" dice: "+"n"+jTextField1.getText());
}catch(Exception ex){ex.printStackTrace();}
}
public void Server(){
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
String portNum,registryURL;
try{
// RMIPortNum=8080;//Integer.parseInt(jTextField2.getText());
startRegistry(RMIPortNum);
exportObj =new ChatImp();
registryURL="rmi://127.0.0.1:"+RMIPortNum+"/hello";
Naming.rebind(registryURL,exportObj);
//listregistry
try{
String[] names=Naming.list(registryURL);
for(int i=0;i<names.length;i++)
//jTextArea1.append("n"+names[i]);
jTextArea1.append("n Conectadon");
}catch(RemoteException e){}
a.start();
}catch(Exception re){}
}
private static void startRegistry(int RMIPortNum)throws RemoteException{
try{
Registry registry=LocateRegistry.createRegistry(RMIPortNum);
registry.list();
}catch(RemoteException ex){}
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Mensajero");
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setFont(new java.awt.Font("Verdana", 1, 11));
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setBackground(new java.awt.Color(204, 204, 255));
jButton1.setText("Send");
jButton1.setEnabled(false);
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jTextField1.setEnabled(false);
jButton2.setBackground(new java.awt.Color(153, 204, 255));
jButton2.setText("Conectar");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jLabel1.setText("Nick:");
jLabel2.setText("Host:");
jLabel3.setText("P Origen:");
jLabel4.setText("P Destino:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField5)
.addComponent(jTextField2)
.addComponent(jTextField4)
.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// TODO add your handling code here:
jTextField1.setEnabled(true);
jButton1.setEnabled(true);
hostName=jTextField2.getText();
myname=jTextField5.getText();
jButton2.setVisible(false);
jTextField2.setVisible(false);
RMIPortNum=Integer.parseInt(jTextField3.getText());
portnum=jTextField4.getText();
jTextField3.setVisible(false);
jTextField4.setVisible(false);
jTextField5.setVisible(false);
jLabel1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
Server();
}//GEN-LAST:event_jButton2MouseClicked
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
men=jTextField1.getText();
Cliente();
jTextArea1.append(myname+" dice:"+"n"+men+"n");
jTextField1.setText("");
}//GEN-LAST:event_jButton1MouseClicked
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatRMI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration//GEN-END:variables
public String men=new String("");
public static ChatImp exportObj;
public static String message=new String("");
public hilo a=new hilo();
String hostName;
int RMIPortNum;
String portnum=new String("");
String myname=new String("");
/*****INTERFACE*****/
import java.rmi.*;
public interface InterfaceChat extends Remote{
public void sendMsg(String Msg)throws RemoteException;
public String getMsg()throws RemoteException;
//public void setName(String name)throws RemoteException;
}
/**********IMP**********/
import java.rmi.*;
import java.rmi.server.*;
public class ChatImp extends UnicastRemoteObject implements InterfaceChat{
public ChatImp()throws RemoteException{
super();
}
public String MSG=new String("");
// public String name=new String("");
public void sendMsg(String Msg)throws RemoteException{
this.MSG=Msg;
}
public String getMsg()throws RemoteException{
return MSG;
}
/* public void setName(String name)throws RemoteException{
this.name=name;
}
*/
}
}