Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package merrmiserver;
- import java.awt.BorderLayout;
- import java.awt.Desktop;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.MalformedURLException;
- import java.net.NetworkInterface;
- import java.net.Socket;
- import java.net.SocketException;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.rmi.Naming;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.Enumeration;
- import javax.swing.Box;
- import javax.swing.BoxLayout;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- /**3-Piece demo of RMI
- * merrmiserver.RmiServer.java
- * merrmiserver.RmiServerIntf.java
- * merrmiclient.RmiClient.java
- *
- *
- * This has an initial handshake in the reverse direction; client will act as
- * server and server as client to exchange IP for future RMI calls.
- *
- * 1. Start the RmiClient. This will instantiate a second Server that will stop and
- * wait for a Client connection from the RmiServer's own second Client, which will feed it the
- * RmiServer's IP.
- * 2. Start the RmiServer. This'll spawn the secondary client to send the IP to the RmiClient,
- * and will then start the Server to wait for that client to connect.
- * 3. Let the RmiClient proceed to connect to the RmiServer on the established port and invoke the remote
- * method laid out in the RmiServerIntf class.
- * @author Meredith
- *
- */
- public class RmiServer
- extends UnicastRemoteObject
- implements RmiServerIntf {
- private static JTextArea jta;
- private static JLabel jl1;
- private static JLabel jl2;
- public RmiServer() throws RemoteException {
- super(0); // required to avoid the 'rmic' step, see below
- JFrame jf = new JFrame();
- jf.setSize(300,300);
- JPanel jp = new JPanel();
- jp.setSize(jf.getSize());
- jf.setLayout(new BorderLayout());
- jf.add(jp,BorderLayout.CENTER);
- jta = new JTextArea();
- jta.setSize(jp.getWidth()-10,jp.getHeight()/2);
- jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
- jl1 = new JLabel();
- jl2 = new JLabel();
- jp.add(Box.createVerticalGlue());
- JPanel labelpan = new JPanel();
- labelpan.add(jl1);
- labelpan.add(Box.createHorizontalGlue());
- jp.add(labelpan);
- jp.add(Box.createVerticalStrut(10));
- JPanel labelpan2 = new JPanel();
- labelpan2.add(jl2);
- labelpan2.add(Box.createHorizontalGlue());
- jp.add(labelpan2);
- jp.add(Box.createVerticalStrut(10));
- jp.add(jta);
- jp.add(Box.createVerticalGlue());
- jf.setVisible(true);
- jl1.setText("RMI server started");
- try { //create registry
- LocateRegistry.createRegistry(1099);
- System.out.println("java RMI registry created.");
- } catch (RemoteException e) {
- System.out.println("java RMI registry already exists.");
- }
- // Bind this object instance to the name "RmiServer"
- String ipToServeOn=dafuqIsMyIP();
- System.out.println(String.format("//%s/RmiServer",ipToServeOn));
- try {
- Naming.rebind(String.format("//%s/RmiServer",ipToServeOn), this);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- jl2.setText("PeerServer bound in registry");
- IPSenderClient ipsc = new IPSenderClient(ipToServeOn);
- }
- @Override
- public void browsePage(URL url){
- if(Desktop.isDesktopSupported())
- {
- try {
- Desktop.getDesktop().browse(url.toURI());
- } catch (IOException | URISyntaxException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String args[]) throws Exception {
- //Instantiate RmiServer
- RmiServer obj = new RmiServer();
- }
- public static String dafuqIsMyIP()
- {
- String ip=null,ip0=null;
- try {
- Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
- while (interfaces.hasMoreElements()) {
- NetworkInterface iface = interfaces.nextElement();
- // filters out 127.0.0.1 and inactive interfaces
- if (iface.isLoopback() || !iface.isUp())
- continue;
- Enumeration<InetAddress> addresses = iface.getInetAddresses();
- while(addresses.hasMoreElements()) {
- InetAddress addr = addresses.nextElement();
- ip = addr.getHostAddress();
- if(ip0==null){
- ip0=ip;
- if(ip0.contains(":")){ip0="["+ip0+"]"; /*ipv6*/}
- }
- jta.append(iface.getDisplayName() + " " + ip+"\n");
- }
- }
- } catch (SocketException e) {
- throw new RuntimeException(e);
- }
- System.out.println(ip0);
- return ip0;
- }
- /*Connects to class that'll have to make the RMI calls to give it the proper IP*/
- class IPSenderClient{
- int portNum;
- public IPSenderClient(String serviceIP){
- portNum=4444;
- try (
- Socket sSocket = new Socket("localhost", portNum);
- PrintWriter out = new PrintWriter(sSocket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(sSocket.getInputStream()));
- ){
- out.println(serviceIP);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement