Advertisement
MerAll

RmiServer.java

Jul 21st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package merrmiserver;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Desktop;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.net.InetAddress;
  10. import java.net.MalformedURLException;
  11. import java.net.NetworkInterface;
  12. import java.net.Socket;
  13. import java.net.SocketException;
  14. import java.net.URISyntaxException;
  15. import java.net.URL;
  16. import java.net.UnknownHostException;
  17. import java.rmi.Naming;
  18. import java.rmi.RemoteException;
  19. import java.rmi.registry.LocateRegistry;
  20. import java.rmi.server.UnicastRemoteObject;
  21. import java.util.Enumeration;
  22.  
  23. import javax.swing.Box;
  24. import javax.swing.BoxLayout;
  25. import javax.swing.JFrame;
  26. import javax.swing.JLabel;
  27. import javax.swing.JPanel;
  28. import javax.swing.JTextArea;
  29.  
  30. /**3-Piece demo of RMI
  31.  * merrmiserver.RmiServer.java
  32.  * merrmiserver.RmiServerIntf.java
  33.  * merrmiclient.RmiClient.java
  34.  *
  35.  *
  36.  * This has an initial handshake in the reverse direction; client will act as
  37.  * server and server as client to exchange IP for future RMI calls.
  38.  *
  39.  * 1. Start the RmiClient.  This will instantiate a second Server that will stop and
  40.  * wait for a Client connection from the RmiServer's own second Client, which will feed it the
  41.  * RmiServer's IP.
  42.  * 2. Start the RmiServer.  This'll spawn the secondary client to send the IP to the RmiClient,
  43.  * and will then start the Server to wait for that client to connect.
  44.  * 3. Let the RmiClient proceed to connect to the RmiServer on the established port and invoke the remote
  45.  * method laid out in the RmiServerIntf class.
  46.  * @author Meredith
  47.  *
  48.  */
  49. public class RmiServer
  50.     extends UnicastRemoteObject
  51.     implements RmiServerIntf {
  52.    
  53.  
  54.     private static JTextArea jta;
  55.     private static JLabel jl1;
  56.     private static JLabel jl2;
  57.  
  58.     public RmiServer() throws RemoteException {
  59.         super(0);    // required to avoid the 'rmic' step, see below
  60.         JFrame jf = new JFrame();
  61.         jf.setSize(300,300);
  62.         JPanel jp = new JPanel();
  63.         jp.setSize(jf.getSize());
  64.         jf.setLayout(new BorderLayout());
  65.         jf.add(jp,BorderLayout.CENTER);
  66.         jta = new JTextArea();
  67.         jta.setSize(jp.getWidth()-10,jp.getHeight()/2);
  68.        
  69.         jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
  70.         jl1 = new JLabel();
  71.         jl2 = new JLabel();
  72.         jp.add(Box.createVerticalGlue());
  73.        
  74.         JPanel labelpan = new JPanel();
  75.         labelpan.add(jl1);
  76.         labelpan.add(Box.createHorizontalGlue());
  77.         jp.add(labelpan);
  78.        
  79.         jp.add(Box.createVerticalStrut(10));
  80.        
  81.         JPanel labelpan2 = new JPanel();
  82.         labelpan2.add(jl2);
  83.         labelpan2.add(Box.createHorizontalGlue());
  84.         jp.add(labelpan2);
  85.        
  86.         jp.add(Box.createVerticalStrut(10));
  87.         jp.add(jta);
  88.         jp.add(Box.createVerticalGlue());
  89.         jf.setVisible(true);
  90.        
  91.         jl1.setText("RMI server started");
  92.  
  93.         try { //create registry
  94.             LocateRegistry.createRegistry(1099);
  95.             System.out.println("java RMI registry created.");
  96.         } catch (RemoteException e) {
  97.            System.out.println("java RMI registry already exists.");
  98.         }
  99.  
  100.        
  101.        
  102.  
  103.         // Bind this object instance to the name "RmiServer"
  104.         String ipToServeOn=dafuqIsMyIP();
  105.         System.out.println(String.format("//%s/RmiServer",ipToServeOn));
  106.         try {
  107.             Naming.rebind(String.format("//%s/RmiServer",ipToServeOn), this);
  108.         } catch (MalformedURLException e) {
  109.             e.printStackTrace();
  110.         }
  111.         jl2.setText("PeerServer bound in registry");
  112.        
  113.         IPSenderClient ipsc = new IPSenderClient(ipToServeOn);
  114.     }
  115.  
  116.  
  117.     @Override
  118.     public void browsePage(URL url){
  119.           if(Desktop.isDesktopSupported())
  120.        {
  121.            try {
  122.             Desktop.getDesktop().browse(url.toURI());
  123.         } catch (IOException | URISyntaxException e) {
  124.             e.printStackTrace();
  125.         }
  126.        }
  127.     }
  128.  
  129.     public static void main(String args[]) throws Exception {
  130.        
  131.           //Instantiate RmiServer
  132.         RmiServer obj = new RmiServer();
  133.      
  134.        
  135.     }
  136.    
  137.     public static String dafuqIsMyIP()
  138.     {
  139.         String ip=null,ip0=null;
  140.         try {
  141.             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  142.             while (interfaces.hasMoreElements()) {
  143.                 NetworkInterface iface = interfaces.nextElement();
  144.                 // filters out 127.0.0.1 and inactive interfaces
  145.                 if (iface.isLoopback() || !iface.isUp())
  146.                     continue;
  147.  
  148.                 Enumeration<InetAddress> addresses = iface.getInetAddresses();
  149.                 while(addresses.hasMoreElements()) {
  150.                     InetAddress addr = addresses.nextElement();
  151.                    
  152.                     ip = addr.getHostAddress();
  153.                     if(ip0==null){
  154.                         ip0=ip;
  155.                         if(ip0.contains(":")){ip0="["+ip0+"]"; /*ipv6*/}
  156.                     }
  157.                     jta.append(iface.getDisplayName() + " " + ip+"\n");
  158.                 }
  159.             }
  160.         } catch (SocketException e) {
  161.             throw new RuntimeException(e);
  162.         }
  163.         System.out.println(ip0);
  164.         return ip0;
  165.     }
  166.  
  167.     /*Connects to class that'll have to make the RMI calls to give it the proper IP*/
  168.     class IPSenderClient{
  169.         int portNum;
  170.         public IPSenderClient(String serviceIP){
  171.            
  172.             portNum=4444;
  173.             try (
  174.                     Socket sSocket = new Socket("localhost", portNum);
  175.                     PrintWriter out = new PrintWriter(sSocket.getOutputStream(), true);
  176.                     BufferedReader in = new BufferedReader(
  177.                         new InputStreamReader(sSocket.getInputStream()));
  178.                 ){
  179.                 out.println(serviceIP);
  180.             } catch (IOException e) {
  181.                 e.printStackTrace();
  182.             }
  183.         }
  184.        
  185.        
  186.     }
  187.    
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement