Вы находитесь на странице: 1из 12

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

983

1 2 3 4 5 6 7 8

// Fig. 20.1: TemperatureServer.java // TemperatureServer interface definition import java.rmi.*; public interface TemperatureServer extends Remote { public WeatherInfo[] getWeatherInfo() throws RemoteException; }

Fig. 20.1

TemperatureServer interface.

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

984

REMOTE METHOD INVOCATION (RMI)

CHAPTER 20

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

// Fig. 20.2: TemperatureServerImpl.java // TemperatureServerImpl definition import java.rmi.*; import java.rmi.server.*; import java.util.*; import java.io.*; import java.net.*; public class TemperatureServerImpl extends UnicastRemoteObject implements TemperatureServer { private WeatherInfo weatherInformation[]; public TemperatureServerImpl() throws RemoteException { super(); updateWeatherConditions(); } // get weather information from NWS private void updateWeatherConditions() throws RemoteException { try { System.err.println( "Updating weather information..." ); // Traveler's Forecast Web Page URL url = new URL( "http://iwin.nws.noaa.gov/iwin/us/traveler.html" ); BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) ); String separator = "</PRE><HR> <BR><PRE>"; // locate first horizontal line on Web page while ( !in.readLine().startsWith( separator ) ) ; // do nothing // s1 is the day format and s2 is the night format String s1 = "CITY WEA HI/LO WEA HI/LO"; String s2 = "CITY WEA LO/HI WEA LO/HI"; String inputLine = ""; // locate header that begins weather information do { inputLine = in.readLine(); } while ( !inputLine.equals( s1 ) && !inputLine.equals( s2 ) );

Fig. 20.2

Class TemperatureServerImpl (part 1 of 3).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

985

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 Fig. 20.2

Vector cityVector = new Vector(); inputLine = in.readLine(); // get first city's info

while ( !inputLine.equals( "" ) ) { // create WeatherInfo object for city WeatherInfo w = new WeatherInfo( inputLine.substring( 0, 16 ), inputLine.substring( 16, 22 ), inputLine.substring( 23, 29 ) ); cityVector.addElement( w ); // add to Vector inputLine = in.readLine(); // get next city's info } // create array to return to client weatherInformation = new WeatherInfo[ cityVector.size() ]; for ( int i = 0; i < weatherInformation.length; i++ ) weatherInformation[ i ] = ( WeatherInfo ) cityVector.elementAt( i ); System.err.println( "Finished Processing Data." ); in.close(); // close connection to NWS server } catch( java.net.ConnectException ce ) { System.err.println( "Connection failed." ); System.exit( 1 ); } catch( Exception e ) { e.printStackTrace(); System.exit( 1 ); } } // implementation for TemperatureServer interface method public WeatherInfo[] getWeatherInfo() { return weatherInformation; } public static void main( String args[] ) throws Exception { System.err.println( "Initializing server: please wait." ); // create server object TemperatureServerImpl temp = new TemperatureServerImpl(); // bind TemperatureServerImpl object to the rmiregistry String serverObjectName = "//localhost/TempServer"; Class TemperatureServerImpl (part 2 of 3).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

986

REMOTE METHOD INVOCATION (RMI)

CHAPTER 20

107 108 109 110 111 } Fig. 20.2

Naming.rebind( serverObjectName, temp ); System.err.println( "The Temperature Server is up and running." ); }

Class TemperatureServerImpl (part 3 of 3).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

989

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 20.3: WeatherInfo.java // WeatherInfo class definition import java.rmi.*; import java.io.Serializable; public class WeatherInfo implements Serializable { private String cityName; private String temperature; private String description; public WeatherInfo( String city, String desc, String temp ) { cityName = city; temperature = temp; description = desc; } public String getCityName() { return cityName; } public String getTemperature() { return temperature; } public String getDescription() { return description; } }

Fig. 20.3

WeatherInfo class definition.

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

990

REMOTE METHOD INVOCATION (RMI)

CHAPTER 20

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

// Fig. 20.4: TemperatureClient.java // TemperatureClient definition import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.rmi.*; public class TemperatureClient extends JFrame { public TemperatureClient( String ip ) { super( "RMI TemperatureClient..." ); getRemoteTemp( ip ); setSize( 625, 567 ); setResizable( false ); show(); } // obtain weather information from TemperatureServerImpl // remote object private void getRemoteTemp( String ip ) { try { // name of remote server object bound to rmi registry String serverObjectName = "//" + ip + "/TempServer"; // lookup TemperatureServerImpl remote object // in rmiregistry TemperatureServer mytemp = ( TemperatureServer ) Naming.lookup( serverObjectName ); // get weather information from server WeatherInfo weatherInfo[] = mytemp.getWeatherInfo(); WeatherItem w[] = new WeatherItem[ weatherInfo.length ]; ImageIcon headerImage = new ImageIcon( "images/header.jpg" ); JPanel p = new JPanel();

Fig. 20.4

TemperatureClient class definition (part 1 of 2).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

991

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

// determine number of rows for the GridLayout; // add 3 to accommodate the two header JLabels // and balance the columns p.setLayout( new GridLayout( ( w.length + 3 ) / 2, 2 ) ); p.add( new JLabel( headerImage ) ); // header 1 p.add( new JLabel( headerImage ) ); // header 2 for ( int i = 0; i < w.length; i++ ) { w[ i ] = new WeatherItem( weatherInfo[ i ] ); p.add( w[ i ] ); } getContentPane().add( new JScrollPane( p ), BorderLayout.CENTER ); } catch ( java.rmi.ConnectException ce ) { System.err.println( "Connection to server failed. " + "Server may be temporarily unavailable." ); } catch ( Exception e ) { e.printStackTrace(); System.exit( 1 ); } } public static void main( String args[] ) { TemperatureClient gt = null; // if no sever IP address or host name specified, // use "localhost"; otherwise use specified host if ( args.length == 0 ) gt = new TemperatureClient( "localhost" ); else gt = new TemperatureClient( args[ 0 ] ); gt.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }

Fig. 20.4

TemperatureClient class definition (part 2 of 2).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

993

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

// Fig. 20.5: WeatherItem.java // WeatherItem definition import java.awt.*; import javax.swing.*; public class WeatherItem extends JLabel { private static ImageIcon weatherImages[], backgroundImage; private final static String weatherConditions[] = { "SUNNY", "PTCLDY", "CLOUDY", "MOCLDY", "TSTRMS", "RAIN", "SNOW", "VRYHOT", "FAIR", "RNSNOW", "SHWRS", "WINDY", "NOINFO", "MISG" }; private final static String weatherImageNames[] = { "sunny", "pcloudy", "mcloudy", "mcloudy", "rain", "rain", "snow", "vryhot", "fair", "rnsnow", "showers", "windy", "noinfo", "noinfo" }; // static initializer block to load weather images static { backgroundImage = new ImageIcon( "images/back.jpg" ); weatherImages = new ImageIcon[ weatherImageNames.length ]; for ( int i = 0; i < weatherImageNames.length; ++i ) weatherImages[ i ] = new ImageIcon( "images/" + weatherImageNames[ i ] + ".jpg" ); } // instance variables private ImageIcon weather; private WeatherInfo weatherInfo; public WeatherItem( WeatherInfo w ) { weather = null; weatherInfo = w; // locate image for city's weather condition for ( int i = 0; i < weatherConditions.length; ++i ) if ( weatherConditions[ i ].equals( weatherInfo.getDescription().trim() ) ) { weather = weatherImages[ i ]; break; } // // // if pick the "no info" image if either there is no weather info or no image for the current weather condition ( weather == null ) { weather = weatherImages[ weatherImages.length - 1 ]; System.err.println( "No info for: " + weatherInfo.getDescription() );

} }

Fig. 20.5

WeatherItem class definition (part 1 of 2).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

994

REMOTE METHOD INVOCATION (RMI)

CHAPTER 20

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

public void paintComponent( Graphics g ) { super.paintComponent( g ); backgroundImage.paintIcon( this, g, 0, 0 ); Font f = new Font( "SansSerif", Font.BOLD, 12 ); g.setFont( f ); g.setColor( Color.white ); g.drawString( weatherInfo.getCityName(), 10, 19 ); g.drawString( weatherInfo.getTemperature(), 130, 19 ); weather.paintIcon( this, g, 253, 1 ); } // make WeatherItem's preferred size the width and height of // the background image public Dimension getPreferredSize() { return new Dimension( backgroundImage.getIconWidth(), backgroundImage.getIconHeight() ); } }

Fig. 20.5

WeatherItem class definition (part 2 of 2).

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

995

Fig. 20.6

The rmiregistry running.

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

996

REMOTE METHOD INVOCATION (RMI)

CHAPTER 20

Fig. 20.7

The TemperatureServerImpl remote object executing.

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

CHAPTER 20

REMOTE METHOD INVOCATION (RMI)

997

Fig. 20.8

TemperatureClient running.

from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company

Вам также может понравиться