dodaję jeszcze pliki, które działają jako kalkulator przez RMI, więc potrzebuję połączyć te 2 projekty w jeden, tylko nie bardzo wiem jak:
serwer:
package server;
import rmi.Calculator;
import gui.GUI;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Calculator {
private GUI gui;
public Server(GUI g) {
gui = g;
}
public void bindStub() {
try {
LocateRegistry.createRegistry(1099);
Calculator stub = (Calculator) UnicastRemoteObject.exportObject((Remote) this, 1099);
Registry reg = LocateRegistry.getRegistry();
reg.bind("Calculator", stub);
gui.getTextArea().setText(">>>>> Serwer gotowy do pracy.");
} catch(Exception e) {
gui.getTextArea().setText(gui.getTextArea().getText() + "\n>>>>> Wystąpił błąd: " + e.toString());
}
}
@Override
public float add(float a, float b) throws RemoteException {
gui.getTextArea().setText(gui.getTextArea().getText() +
"\n>>>>> Dodawanie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
//System.out.println("Dodawanie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
return a+b;
}
@Override
public float sub(float a, float b) throws RemoteException {
gui.getTextArea().setText(gui.getTextArea().getText() +
"\n>>>>> Odejmowanie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
//System.out.println("Odejmowanie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
return a-b;
}
@Override
public float mul(float a, float b) throws RemoteException {
gui.getTextArea().setText(gui.getTextArea().getText() +
"\n>>>>> Mnożenie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
//System.out.println("Mnożenie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
return a*b;
}
@Override
public float div(float a, float b) throws RemoteException {
gui.getTextArea().setText(gui.getTextArea().getText() +
"\n>>>>> Dzielenie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
//System.out.println("Dzielenie zostało zdalnie wywołane dla argumentów: a = " + a + " b = " + b);
return a/b;
}
public int[] ciag(int parseInt) throws RemoteException {
int[] tab=new int[parseInt];
tab[0]=3;
for(int i=1;i<parseInt;i++){
tab[i]=2*(tab[i-1]);
}
return tab;
}
}
package gui;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JTextArea;
import server.*;
public class GUI extends javax.swing.JFrame {
private Server server;
public GUI() {
initComponents();
server = new Server(this);
server.bindStub();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
ScrollPane = new javax.swing.JScrollPane();
TextArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Sever");
ScrollPane.setEnabled(false);
TextArea.setColumns(20);
TextArea.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
TextArea.setRows(5);
TextArea.setText(">>>>> Serwer gotowy do pracy.");
TextArea.setDisabledTextColor(new java.awt.Color(0, 0, 0));
TextArea.setEnabled(false);
ScrollPane.setViewportView(TextArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
public JTextArea getTextArea() {
return TextArea;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane ScrollPane;
private javax.swing.JTextArea TextArea;
// End of variables declaration//GEN-END:variables
}
RMI:
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
public int[] ciag(int parseInt) throws RemoteException;
public float add(float a, float b) throws RemoteException;
public float sub(float a, float b) throws RemoteException;
public float mul(float a, float b) throws RemoteException;
public float div(float a, float b) throws RemoteException;
}
Klient:
package client;
import rmi.Calculator;
import gui.GUI;
import java.awt.Color;
import java.io.InputStreamReader;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private GUI gui;
private Calculator stub;
public Client(GUI g) {
gui = g;
}
public Calculator getStub() {
return stub;
}
public void connectServer() {
try {
Registry reg = LocateRegistry.getRegistry();
Calculator stub = (Calculator) reg.lookup("Calculator");
this.stub = stub;
} catch(Exception e) {
gui.getResultText().setDisabledTextColor(Color.red);
gui.getResultText().setText("Brak połączenia z serwerem.");
}
}
}
package gui;
import client.Client;
import java.awt.Color;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
public class GUI extends javax.swing.JFrame {
private Client client;
public GUI() {
initComponents();
client = new Client(this);
client.connectServer();
}
private boolean checkVariables() {
if(!aTextField.getText().isEmpty())
return true;
else {
resultTextField.setDisabledTextColor(Color.red);
resultTextField.setText("Podaj brakujące zmienne.");
return false;
}
}
private void evaluate(String s) {
float result = 0;
try {
switch(s) {
case "add":
result = client.getStub().add(Float.parseFloat(aTextField.getText()), Float.parseFloat(bTextField.getText()));
break;
case "sub":
result = client.getStub().sub(Float.parseFloat(aTextField.getText()), Float.parseFloat(bTextField.getText()));
break;
case "mul":
result = client.getStub().mul(Float.parseFloat(aTextField.getText()), Float.parseFloat(bTextField.getText()));
break;
case "div":
result = client.getStub().div(Float.parseFloat(aTextField.getText()), Float.parseFloat(bTextField.getText()));
break;
case "ciag":
int[] wynik=new int[Integer.parseInt(aTextField.getText())];
wynik=client.getStub().ciag(Integer.parseInt(aTextField.getText()));
jTextArea1.setText(Arrays.toString(wynik));
break;
default:
break;
}
resultTextField.setDisabledTextColor(Color.black);
resultTextField.setText(Float.toString(result));
} catch (RemoteException ex) {
resultTextField.setDisabledTextColor(Color.red);
resultTextField.setText("Błąd połączenia.");
}
}
private void displayVarError() {
resultTextField.setDisabledTextColor(Color.red);
resultTextField.setText("Podaj brakujące zmienne.");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
OperationsPanel = new javax.swing.JPanel();
AddButton = new javax.swing.JButton();
SubPanel = new javax.swing.JButton();
MultPanel = new javax.swing.JButton();
DivPanel = new javax.swing.JButton();
DataPanel = new javax.swing.JPanel();
aLabel = new javax.swing.JLabel();
aTextField = new javax.swing.JTextField();
bTextField = new javax.swing.JTextField();
bLabel = new javax.swing.JLabel();
resultTextField = new javax.swing.JTextField();
DivPanel1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculator");
setResizable(false);
OperationsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Operacje"));
AddButton.setText("Dodawanie");
AddButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddButtonActionPerformed(evt);
}
});
SubPanel.setText("Odejmowanie");
SubPanel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubPanelActionPerformed(evt);
}
});
MultPanel.setText("Mnożenie");
MultPanel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MultPanelActionPerformed(evt);
}
});
DivPanel.setText("Dzielenie");
DivPanel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DivPanelActionPerformed(evt);
}
});
javax.swing.GroupLayout OperationsPanelLayout = new javax.swing.GroupLayout(OperationsPanel);
OperationsPanel.setLayout(OperationsPanelLayout);
OperationsPanelLayout.setHorizontalGroup(
OperationsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(OperationsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(OperationsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(AddButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(SubPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addComponent(MultPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
.addComponent(DivPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
.addContainerGap())
);
OperationsPanelLayout.setVerticalGroup(
OperationsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(OperationsPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(AddButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SubPanel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(MultPanel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
.addComponent(DivPanel))
);
DataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Dane"));
aLabel.setText("Podaj a:");
bLabel.setText("Podaj b:");
resultTextField.setDisabledTextColor(new java.awt.Color(0, 0, 0));
resultTextField.setEnabled(false);
javax.swing.GroupLayout DataPanelLayout = new javax.swing.GroupLayout(DataPanel);
DataPanel.setLayout(DataPanelLayout);
DataPanelLayout.setHorizontalGroup(
DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(DataPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(DataPanelLayout.createSequentialGroup()
.addGroup(DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(aLabel)
.addComponent(aTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bLabel)
.addComponent(bTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(resultTextField, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
DataPanelLayout.setVerticalGroup(
DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(DataPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bLabel)
.addComponent(aLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(DataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(aTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(resultTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
DivPanel1.setText("Ciag");
DivPanel1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DivPanel1ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(OperationsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(311, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(DataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addComponent(DivPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(DataPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(OperationsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DivPanel1)
.addContainerGap(96, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
if(checkVariables())
evaluate("add");
else
displayVarError();
}//GEN-LAST:event_AddButtonActionPerformed
private void SubPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubPanelActionPerformed
if(checkVariables())
evaluate("sub");
else
displayVarError();
}//GEN-LAST:event_SubPanelActionPerformed
private void MultPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MultPanelActionPerformed
if(checkVariables())
evaluate("mul");
else
displayVarError();
}//GEN-LAST:event_MultPanelActionPerformed
private void DivPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DivPanelActionPerformed
if(checkVariables())
evaluate("div");
else
displayVarError();
}//GEN-LAST:event_DivPanelActionPerformed
private void DivPanel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DivPanel1ActionPerformed
if(checkVariables())
evaluate("ciag");
else
displayVarError();
}//GEN-LAST:event_DivPanel1ActionPerformed
public JTextField getResultText() {
return resultTextField;
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton AddButton;
private javax.swing.JPanel DataPanel;
private javax.swing.JButton DivPanel;
private javax.swing.JButton DivPanel1;
private javax.swing.JButton MultPanel;
private javax.swing.JPanel OperationsPanel;
private javax.swing.JButton SubPanel;
private javax.swing.JLabel aLabel;
private javax.swing.JTextField aTextField;
private javax.swing.JLabel bLabel;
private javax.swing.JTextField bTextField;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField resultTextField;
// End of variables declaration//GEN-END:variables
}