Академический Документы
Профессиональный Документы
Культура Документы
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
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
985
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
CHAPTER 20
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
990
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
994
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
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
995
Fig. 20.6
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
996
CHAPTER 20
Fig. 20.7
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company
CHAPTER 20
997
Fig. 20.8
TemperatureClient running.
from Java: How To Program, 3e by Deitel & Deitel 2000 Prentice Hall, Inc. A Pearson Company