Results 1 to 3 of 3

Thread: Using RMI

  1. #1
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Using RMI

    Hello!

    I am using RMI and I am trying to somehow start class "ServiceBrowser" from my browser but i don't know what to writte on HTML :
    <HTML>
    <BODY>
    <a href= "RMI/SecondRMI/ServiceBrowser">Start it!</a>
    </BODY>
    </HTML>

    I am using wamp Sercer and root directory is www.I am getting error 404. and my ServiceBrowser.class is in the www/RMI/SecondRMI/ folder.
    ServiceBrowser:


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.rmi.*;
    import java.awt.event.*;
    
     public class ServiceBrowser {
    	JPanel mainPanel;
    	JComboBox<Object> serviceList;
    	ServiceServer server;
    
    	public void buildGUI() {
    
    		JFrame frame = new JFrame("RMI Browser");
    		mainPanel = new JPanel();
    		frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
    		
    		Object[] services = getServicesList();
    		
    		serviceList = new JComboBox<Object>(services);
    		frame.getContentPane().add(BorderLayout.NORTH, serviceList);
    		serviceList.addActionListener(new MyListListener());
    		
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    
    
    	void loadService(Object serviceSelection) {
    		try {
    		Service svc = server.getService(serviceSelection);
    		
    		mainPanel.removeAll();
    		mainPanel.add(svc.getGuiPanel());
    		mainPanel.validate();
    		mainPanel.repaint();
    		}catch(Exception ex) {
    			ex.printStackTrace();
    		}
     	}
    
    
    	Object[] getServicesList() {
    		Object obj =null;	
    		Object[] services = null;	
    		try {
    	obj = Naming.lookup("rmi://127.0.0.1/RMI/SecondRMI/ServiceServer");
    	} catch (Exception ex){
    	   ex.printStackTrace();	
    	} 
    	server = (ServiceServer) obj;
    	
    
    	try {
    		services = server.getServiceList();
    		} catch (Exception ex) {
    		ex.printStackTrace();
    		}
    	return services;
    	}
    
    		class MyListListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    		
    		Object selection = serviceList.getSelectedItem();
    		loadService(selection);
    		
    	}
       }
    
    	public static void main(String[] args) {
    		new ServiceBrowser().buildGUI();
    	}
       }
    Other classer used by ServiceBrowser are also in www/RMI/SecondRMI/ folder.
    Thanks a lot

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Using RMI

    From what I read in the JavaDoc for the class called "Naming", you don't specify the "rmi:" part of the URL, instead, like the following example:

    "The Naming class provides methods for storing and obtaining references to remote objects in a remote object registry. Each method of the Naming class takes as one of its arguments a name that is a java.lang.String in URL format (without the scheme component) of the form:"

    //host:port/name

    instead of:

    rmi://127.0.0.1/RMI/SecondRMI/ServiceServer

    I'm not sure if this would make a difference.

    I couldn't get your code to compile because it refers to missing source code. If you want to make a SSCCE, I'll look at your code again:

    Short, Self Contained, Correct Example

    -- Kaydell
    Last edited by kaydell2; 09-30-2013 at 06:59 AM.

  3. #3
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Using RMI

    I changed the rmi://127.0.0.1/RMI/SecondRMI/ServiceServer to //127.0.0.1:80/ServiceServer, there is still the same error 404, if you could please look at the whole code:

    interface service:
    Java Code:
    import java.rmi.*;
    import javax.swing.*;
    import java.io.*;
    
    	 public interface Service extends Serializable {
    		public JPanel getGuiPanel();
    	}
    Interface ServiceServer:
    Java Code:
    import java.rmi.*;
    import javax.swing.*;
    import java.io.*;
    
    	public interface ServiceServer extends Remote {
    		Object[] getServiceList() throws RemoteException;
    		Service getService(Object serviceKey) throws RemoteException;
    		}
    class ServiceServerImpl:
    Java Code:
    import java.rmi.*;
    import java.util.*;
    import java.rmi.server.*;
    
    	public class ServiceServerImpl extends UnicastRemoteObject implements ServiceServer {
    		private static final long serialVersionUID = 546544599369756453L;
    		HashMap<String, Object> serviceList;
    		
    		public ServiceServerImpl() throws RemoteException {
    		setUpServices();
    	}
    
    		private void setUpServices() {
    			serviceList= new HashMap<String, Object>();
    			serviceList.put("Dice Rolling Service", new DiceService());
    			serviceList.put("Day of the Week Service", new DayOfTheWeekService());
    			serviceList.put("Visual Music Service", new MiniMusicService());
    	}
    		public Object[] getServiceList() {
    			System.out.println("in remote");
    			return serviceList.keySet().toArray();
    	}
    		public Service getService(Object serviceKey) throws RemoteException {
    			Service theService = (Service) serviceList.get(serviceKey);
    			return theService;
    	}
    
    
    	public static void main(String[] args) {
    		try {
    		Naming.rebind("ServiceServer", new ServiceServerImpl());
    	} catch(Exception ex) {
    		ex.printStackTrace();
    	}
    		System.out.println("Remote service is running");
    	}
        }
    class DiceService:
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    		public class DiceService implements Service {	
    				 private static final long serialVersionUID = 1235465445656456453L;
    
    			JLabel label;
    			JComboBox<String> numOfDice;
    		public JPanel getGuiPanel() {
    
    			JPanel panel = new JPanel();
    			JButton button = new JButton ("Roll 'em!");
    			String[] choices = {"1", "2", "3", "4", "5"};
    			numOfDice = new JComboBox<String>(choices);
    			label = new JLabel("dice values here");
    			button.addActionListener(new RollEmListener());
    			panel.add(numOfDice);
    			panel.add(button);
    			panel.add(label);
    			return panel;
    	}
    
    	public class RollEmListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    		String diceOutput = "";
    		String selection = (String) numOfDice.getSelectedItem();
    		int numToRoll = Integer.parseInt(selection);
    			for(int i=0; i<numToRoll; i++) {
    		int r = (int) ((Math.random()*6) +1);
    		diceOutput +=(" " +r);
    		}
    	label.setText(diceOutput);
    		}
    	}
    }
    class MiniMusicService:
    Java Code:
    import javax.sound.midi.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    	public class MiniMusicService implements Service {
    		
    			MyDrawPanel myPanel;
    			
    			public JPanel getGuiPanel()  {
    		JPanel mainPanel = new JPanel();
    		myPanel = new MyDrawPanel();
    		JButton playItButton = new JButton("Play it");
    		playItButton.addActionListener(new PlayItListener());
    		mainPanel.add(myPanel);
    		mainPanel.add(playItButton);
    		return mainPanel;
    	}
    
    		public class PlayItListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    		
    		try {
    			Sequencer sequencer = MidiSystem.getSequencer();
    			sequencer.open();
    			
    			sequencer.addControllerEventListener(myPanel, new int[] {127});
    			Sequence seq = new Sequence(Sequence.PPQ, 4);
    			Track track = seq.createTrack();
    			
    		for(int i = 0; i<100; i+=4) {
    			
    			int rNum = (int) ((Math.random()*50) +1);
    			if(rNum<38) {
    			track.add(makeEvent(144, 1, rNum, 100,i));
    			track.add(makeEvent(176, 1, 127, 0, i));
    			track.add(makeEvent(128,1,rNum,100, i+2));
    			}
    		}
    
    			sequencer.setSequence(seq);
    			sequencer.start();
    			sequencer.setTempoInBPM(220);
    			} catch (Exception ex) {ex.printStackTrace();}
    		}
    	}
    
    		public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    			MidiEvent event = null;
    			try {
    			ShortMessage a = new ShortMessage();
    			a.setMessage(comd, chan, one,two);
    			event = new MidiEvent(a,tick);
    			
    			}catch(Exception e) {}	
    		return event;
    	}
    
    		class MyDrawPanel extends JPanel implements ControllerEventListener {
    			
    			boolean msg = false;
    			
    			public void controlChange(ShortMessage event) {
    			msg = true;
    			repaint();
    		}
    			public Dimension getPreferredSize() {
    			return new Dimension(300,300);
    		}
    			public void paintComponent(Graphics g) {
    			if(msg) {
    			
    			Graphics2D g2 = (Graphics2D) g;
    			
    			int r= (int) (Math.random()*250);
    			int gr = (int) (Math.random()*250);
    			int b = (int) (Math.random()*250);
    			
    			g.setColor(new Color(r,gr,b));
    			
    			int ht = (int) ((Math.random()*120) +10);
    			int width = (int) ((Math.random()*120) +10);
    			
    			int x = (int) ((Math.random()*40) + 10);	
    			int y = (int) ((Math.random()*40) + 10);
    			
    			g.fillRect(x,y,ht, width);
    			msg = false;
    			}
    		}
    	}
    }
    class DayOfTheWeekService:
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    	public class DayOfTheWeekService implements Service {
    		private static final long serialVersionUID = 1254456564564533344L;
    		JLabel outputLabel;
    		JComboBox<String> month;
    		JTextField day;
    		JTextField year;
    	public JPanel getGuiPanel() {
    		JPanel panel = new JPanel();
    		JButton button = new JButton("do it!");
    		button.addActionListener(new DoItListener());
    		outputLabel  = new JLabel("date appears here");
    		DateFormatSymbols dateStuff = new DateFormatSymbols();
    		month  = new JComboBox<String>(dateStuff.getMonths());
    		day = new JTextField(8);
    		year = new JTextField(8);	
    		JPanel inputPanel = new JPanel(new GridLayout(3,2));
    		inputPanel.add(new JLabel("Month"));
    		inputPanel.add(month);
    		inputPanel.add(new JLabel("Day"));
    		inputPanel.add(day);
    		inputPanel.add(new JLabel("Year"));
    		inputPanel.add(year);
    		panel.add(inputPanel);
    		panel.add(button);
    		panel.add(outputLabel);
    		return panel;
    	}
    
    		
    		public class DoItListener implements ActionListener {
    			public void actionPerformed(ActionEvent ev) {
    			int monthNum = month.getSelectedIndex();
    			int dayNum = Integer.parseInt(day.getText());
    			int yearNum = Integer.parseInt(year.getText());
    			Calendar c = Calendar.getInstance();
    			c.set(Calendar.MONTH,monthNum);
    			c.set(Calendar.DAY_OF_MONTH,dayNum);
    			c.set(Calendar.YEAR,yearNum);
    			Date date = c.getTime();
    			String dayOfWeek = (new SimpleDateFormat("EEEE")).format(date);		
    			outputLabel.setText(dayOfWeek);
    			}
    		}
    	}
    class ServiceBrowser:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.rmi.*;
    import java.awt.event.*;
    
     public class ServiceBrowser {
    	JPanel mainPanel;
    	JComboBox<Object> serviceList;
    	ServiceServer server;
    
    	public void buildGUI() {
    
    		JFrame frame = new JFrame("RMI Browser");
    		mainPanel = new JPanel();
    		frame.getContentPane().add(BorderLayout.CENTER,mainPanel);
    		
    		Object[] services = getServicesList();
    		
    		serviceList = new JComboBox<Object>(services);
    		frame.getContentPane().add(BorderLayout.NORTH, serviceList);
    		serviceList.addActionListener(new MyListListener());
    		
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    
    
    	void loadService(Object serviceSelection) {
    		try {
    		Service svc = server.getService(serviceSelection);
    		
    		mainPanel.removeAll();
    		mainPanel.add(svc.getGuiPanel());
    		mainPanel.validate();
    		mainPanel.repaint();
    		}catch(Exception ex) {
    			ex.printStackTrace();
    		}
     	}
    
    
    	Object[] getServicesList() {
    		Object obj =null;	
    		Object[] services = null;	
    		try {
    	obj = Naming.lookup("//127.0.0.1:80/ServiceServer");
    	} catch (Exception ex){
    	   ex.printStackTrace();	
    	} 
    	server = (ServiceServer) obj;
    	
    
    	try {
    		services = server.getServiceList();
    		} catch (Exception ex) {
    		ex.printStackTrace();
    		}
    	return services;
    	}
    
    		class MyListListener implements ActionListener {
    		public void actionPerformed(ActionEvent ev) {
    		
    		Object selection = serviceList.getSelectedItem();
    		loadService(selection);
    		
    	}
       }
    
    	public static void main(String[] args) {
    		new ServiceBrowser().buildGUI();
    	}
       }
    Thanks,
    Ivan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •