1 / 87

Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1

Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1.

jaimie
Télécharger la présentation

Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1 Outline26.1 Introduction26.2 Step-by-Step26.3 First Example: SystemClock 26.3.1 SystemClock.idl 26.3.2 SystemClockImpl.java 26.3.3 SystemClockClient.java 26.3.4 Running the Example26.4 Technical/Architectural Overview26.5 CORBA Basics26.6 Example: AlarmClock 26.6.1 AlarmClock.idl 26.6.2 AlarmClockImpl.java 26.6.3 AlarmClockClient.java26.7 Distributed Exceptions

  2. Chapter 26: Common Object Request Broker Architecture(CORBA): Part 1 26.8 Case Study: Chat 26.8.1 chat.idl 26.8.2 ChatServerImpl.java 26.8.3 DeitelMessenger.java 26.8.4 Running Chat 26.8.5 Issues26.9 Comments and Comparisons26.10 Internet and World Wide Web Resources

  3. 26.1 Introduction • CORBA • Common Object Request Broker Architecture • Allows programs written in various languages to communicate with each other as would two processes in the same address space. • Features leveraged by clients: • Invocation transparency • Implementation transparency • Location transparency

  4. 26.1 Introduction (cont.) • IDL • Interface Definition Language • a pure description language. • enables developers to describe an interface they wish to use remotely in a language-independent fashion. • language specific compiler required for each participating language. • creates files required by each language for CORBA related tasks.

  5. 26.1 Introduction (cont.) • Components of Interoperability • Object reference pointing to a remote object. • comes from an object adapter • information contained: • location (network address) • reference to the adapter that created object reference • object ID • Parsing object reference to transmit • GIOP • Defines messages needed by ORBs to communicate with each other. • IIOP (Internet Inter-ORB Protocol)-GIOP implementation.

  6. 26.2 Step-by-Step • Distributed system implementation steps using Java and CORBA: • Analysis and Design • break problem into smaller fundamental services or subsystems • decide which subsystems to configure as distributed services • Defining the IDL • define functionality each service will expose • define data structures received and returned by each service • Implementing the servant • Implementing a client • Distribution of servant’s object reference • Running the servant implementation • Running the client

  7. 26.3 First Example: SystemClock • Provides a basic service by enabling client to query for current time. • Client requirements: • retrieve the current system time • display the retrieved time in GUI window

  8. 26.3.1 SystemClock.idl • SystemClock server IDL definition • Declares a single method currentTimeMillis

  9. denote single line comments module keyword maps given name to a Java package curly braces denote scope boundaries of block semicolon identifies end of block server type from client’s perspective and server implemented interface return type - maps to Java long ( single long maps to Java int) 1 // Fig. 26.1: systemclock.idl 2 // IDL definition of SystemClock. 3 4 module clock { 5 6 // The definition of the CORBA-enabled service 7 interface SystemClock { 8 longlong currentTimeMillis(); 9 }; 10 }; Fig. 26.1IDL definition for server SystemClockLines 1-2 and 6Line 4Lines 4, 10Lines 9-10Line 7

  10. 26.3.1 SystemClock.idl (cont.) • Everything declared in IDL file is public. • Java IDL compiler, idlj, compiles systemclock.idl with command: idlj –td c:\src –pkgPrefix clock com.deitel.advjhtp1.idl –fall systemclock.idl • Generates following files: • SystemClock.java • SystemClockOperations.java • _SystemClockImplBase.java

  11. declares public operations of server (generated from IDL) CORBA-defined types from which all CORBA-enabled objects inherit 1 package com.deitel.advjhtp1.idl.clock; 2 3 4 /** 5 * com/deitel/jhtp4/idl/clock/SystemClock.java 6 * Generated by the IDL-to-Java compiler (portable), version "3.0" 7 * from systemclock.idl 8 * Wednesday, February 28, 2001 8:24:01 PM PST 9 */ 10 11 public interface SystemClock extends SystemClockOperations, 12 org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity 13 { 14 } // interface SystemClock Fig. 26.2A Java interface generated by idlj.Line 12Line 11

  12. IDL file longlong mapped to Java long method defined in IDL file 1 package com.deitel.advjhtp1.idl.clock; 2 3 4 /** 5 * com/deitel/advjhtp1/idl/clock/SystemClockOperations.java 6 * Generated by the IDL-to-Java compiler (portable), version "3.0" 7 * from systemclock.idl 8 * Sunday, July 1, 2001 10:36:53 PM PDT 9 */ 10 11 12 // The definition of the CORBA-enabled service 13 publicinterface SystemClockOperations 14 { 15 long currentTimeMillis (); 16 } // interface SystemClockOperations Fig. 26.3 SystemClockOperations interface generated by idljLine 15Line 15

  13. 1 // Fig. 26.4: SystemClockImpl.java 2 // SystemClock service implementation. 3 4 package com.deitel.advjhtp1.idl.clock; 5 6 // OMG CORBA packages 7 import org.omg.CORBA.ORB; 8 import org.omg.CosNaming.*; 9 import org.omg.CosNaming.NamingContextPackage.*; 10 11 publicclass SystemClockImpl extends _SystemClockImplBase { 12 13 // return computer's current time in milliseconds 14 publiclong currentTimeMillis() 15 { 16 return System.currentTimeMillis(); 17 } 18 19 // initialize SystemClockImpl object by calling method register 20 public SystemClockImpl( String params[] ) throws Exception 21 { 22 register( "TimeServer", params ); 23 } 24 25 // register SystemClockImpl object with Naming Service 26 private void register( String corbaName, String params[] ) 27 throws org.omg.CORBA.ORBPackage.InvalidName, 28 org.omg.CosNaming.NamingContextPackage.InvalidName, 29 CannotProceed, NotFound 30 { Fig. 26.4Implementation of the SystemClock server

  14. creation of a new ORB object pass implementation object to ORB retrieves Naming Service object reference CORBA mechanism to cast one Object reference to another (in this case, NamingContext) naming context (identifies object reference in naming service) store reference to current object using provided naming context 31 // Check name of service. If name is null or blank 32 // do not attempt to bind to Naming Service. 33 if ( ( corbaName == null ) || 34 ( corbaName.trim().length() == 0 ) ) 35 throw new IllegalArgumentException( 36 "Registration name cannot be null or blank." ); 37 38 // create and initialize ORB 39 ORB orb = ORB.init( params, null ); 40 41 // register this object with ORB 42 orb.connect( this ); 43 44 // find Naming Service 45 org.omg.CORBA.Object corbaObject = 46 orb.resolve_initial_references( "NameService" ); 47 NamingContext naming = 48 NamingContextHelper.narrow( corbaObject ); 49 50 // create NameComponent array with path information to 51 // find this object 52 NameComponent namingComponent = 53 new NameComponent( corbaName, "" ); 54 NameComponent path[] = { namingComponent }; 55 56 // bind SystemClockImpl object with ORB 57 naming.rebind( path, this ); 58 System.out.println( "Rebind complete" ); 59 } 60 61 // main method to execute server 62 public static void main( String[] args ) throws Exception 63 { 64 // Create the SystemClock CORBA object. 65 SystemClock timeServer = new SystemClockImpl( args ); Fig. 26.4 Implementation of the SystemClock server.Line 39Line 42Lines 45-46Lines 47-48Lines 52-53Line 57

  15. 66 67 // Wait for requests from the outside. 68 java.lang.Object object = new java.lang.Object(); 69 70 // keep server alive 71 synchronized( object ) { 72 object.wait(); 73 } 74 } 75 } // end class SystemClockImpl Fig. 26.4 Implementation of the SystemClock server.

  16. 26.3.3 SystemClockClient.java • Represents the client that connects to SystemClock. • Connects to SystemClock service, requests current time and displays result.

  17. create ORB 1 // Fig. 26.5: SystemClockClient.java 2 // Client application for the SystemClock example. 3 4 package com.deitel.advjhtp1.idl.clock; 5 6 // Java core packages 7 import java.text.DateFormat; 8 import java.util.*; 9 10 // Java extension packages 11 import javax.swing.JOptionPane; 12 13 // OMG CORBA packages 14 import org.omg.CORBA.ORB; 15 import org.omg.CosNaming.*; 16 import org.omg.CosNaming.NamingContextPackage.*; 17 18 public class SystemClockClient implements Runnable { 19 private SystemClock timeServer; 20 21 // initialize client 22 public SystemTimeClient( String params[] ) throws Exception 23 { 24 connectToTimeServer( params ); 25 startTimer(); 26 } 27 28 // use NameService to connect to time server 29 private void connectToTimeServer( String params[] ) 30 throws org.omg.CORBA.ORBPackage.InvalidName, 31 org.omg.CosNaming.NamingContextPackage.InvalidName, 32 NotFound, CannotProceed 33 { 34 // connect to SystemClock server 35 ORB orb = ORB.init( params, null ); Fig. 26.5 Client that connects to SystemClock.Line 35

  18. obtain reference of object bound to name TimeServer obtain reference to Naming Service cast to type SystemClock invoke method on remote object 36 37 org.omg.CORBA.Object corbaObject = 38 orb.resolve_initial_references( "NameService" ); 39 NamingContext naming = 40 NamingContextHelper.narrow( corbaObject ); 41 42 // resolve object reference in naming 43 NameComponent nameComponent = 44 new NameComponent( "TimeServer", "" ); 45 NameComponent path[] = { nameComponent }; 46 corbaObject = naming.resolve( path ); 47 timeServer = SystemClockHelper.narrow( corbaObject ); 48 } 49 50 // start timer thread 51 private void startTimer() 52 { 53 Thread thread = new Thread( this ); 54 thread.start(); 55 } 56 57 // talk to server on regular basis and display results 58 public void run() 59 { 60 long time = 0; 61 Date date = null; 62 DateFormat format = 63 DateFormat.getTimeInstance( DateFormat.LONG ); 64 String timeString = null; 65 int response = 0; 66 67 while( true ) { 68 time = timeServer.currentTimeMillis(); 69 date = new Date( time ); 70 timeString = format.format( date ); Fig. 26.5 Client that connects to SystemClock.Line 37Line 44Line 47Line 68

  19. 71 72 response = JOptionPane.showConfirmDialog( null, timeString, 73 "SystemClock Example", JOptionPane.OK_CANCEL_OPTION ); 74 75 if ( response == JOptionPane.CANCEL_OPTION ) 76 break; // Get us out of here 77 } 78 79 System.exit( 0 ); 80 } 81 82 // main method to execute client application 83 public static void main( String args[] ) throws Exception 84 { 85 // create client 86 try { 87 new SystemClockClient( args ); 88 } 89 90 // process exceptions that occur while client executes 91 catch ( Exception exception ) { 92 System.out.println( 93 "Exception thrown by SystemClockClient:" ); 94 exception.printStackTrace(); 95 } 96 } 97 } // end of class SystemClockClient Fig. 26.5 Client that connects to SystemClock.

  20. 26.3.4 Running the Example • Ensure JDK 1.3 is installed on workstation and PATH environment variable includes JDK’s bin directory. • Steps to execute SystemClock example: • compile IDL file using idlj • example (source code in c:\src): idlj –pkgPrefix clock com.deitel.advjhtp1.idl –td c:\src -fall SystemClock.idl • implement and compile server code • implement and compile client code • run Naming Service • tnameserv provided included in JDK 1.3 as a CORBA Object Service Naming Service test tool implementation • example: tnameserv -ORBInitialPort 1050 • run server • java com.deitel.advjhtp1.idl.clock.SystemClockImpl –ORBInitialPort 1050

  21. 26.3.4 Running the Example (cont.) 6. run client java com.deitel.advjhtp1.idl.clock.SystemClockClient -ORBInitialPort 1050

  22. 26.4 Technical/Architectural Overview • ORB is the central mechanism of CORBA. • One ORB must exist for every object in a CORBA-enabled distributed system. • An ORB’s relationship to services in a distributed system is comparable to the relationship between a computer’s communication bus and devices connected to it. • Clients communicate with an ORB by using: • IDL compiler generated stub, • dynamic interface (using CORBA’s dynamic invocation API), or • ORB’s API

  23. 26.4 Technical/Architectural Overview (cont.) Fig. 26.6 Call path from a client to a distributed object.

  24. 26.4 Technical/Architectural Overview (cont.) • An ORB communicates with servants through • a static skeleton, • a dynamic interface, or • a servant’s object adapter.

  25. 26.4 Technical/Architectural Overview (cont.) Fig. 26.8 ORB request-interface structure. Courtesy of Object Management Group, Inc.

  26. 26.4 Technical/Architectural Overview (cont.) • CORBAservices are the baseline services available to all objects sitting on the ORB communication bus. • Naming Service • Event Management Service • Life Cycle Service • Persistent State Service • Transaction Service • Concurrency Service • Relationship Service • Externalization Service

  27. 26.4 Technical/Architectural Overview (cont.) • Query Service • Licensing Service • Property Service • Time Service • Security Service • Notification Service • Trader Service • Collections Service

  28. 26.4 Technical/Architectural Overview (cont.) • CORBAfacilities come in two groups: • horizontal facilities • target client-side functionality • vertical facilities • target domain-specific functionality

  29. 26.4 Technical/Architectural Overview (cont.) • Horizontal facilities specifications: • Mobile Agents Facility • Printing Facility • Internationalization Facility • Vertical facilities domain-specific services are being developed for various business areas: • Common Enterprise Models • Finance/Insurance • Electronic Commerce • Manufacturing • Healthcare • Telecommunications

  30. 26.4 Technical/Architectural Overview (cont.) • Transportation • Life Science Research • Utilities • C4I (Command, Control, Communications, Computers, and Intelligence) • Space

  31. 26.4 Technical/Architectural Overview (cont.) Fig. 26.7 Object Management Architecture reference model. Courtesy of Object Management Group, Inc.

  32. 26.4 Technical/Architectural Overview (cont.) • Applications Objects are the top layer of the OMA. • Application Objecs have the functionality not found at the domain layer, facilities layer or the services layer. • Basic concepts of distributed systems are the same regardless of the designed system’s size. • An understanding of the OMA means a head start in developing an architecture, because most pieces needed are defined in the OMA.

  33. 26.5 CORBA Basics

  34. maps directly to Java package maptest maps directly to a Java class named StructMap (StructMap may contain object references but is declared final, which prevents it from being subclassed) primitive types map to corresponding Java primitive types unbounded sequence maps to a Java array of type StructMap bounded sequence maps to StructMap array of size 5 1 /* 2 * Any comments located outside of the module declaration are 3 * ignored by the IDL compiler. This multi-line comment does 4 * not appear in any of the files generated by idlj. 5 */ 6 7 // This single-line comment is also ignored by the IDL compiler 8 9 module maptest { 10 11 // This comment appears in the generated files for StructMap 12 struct StructMap { 13 14 // This comment appears at start of the type declarations 15 boolean boolValue; 16 char charValue; 17 wchar wCharValue; 18 octet octetValue; 19 string stringValue; 20 wstring wStringValue; 21 short shortValue; 22 unsigned short uShortValue; 23 long longValue; 24 unsigned long uLongValue; 25 long long longLongValue; 26 unsigned long long uLongLongValue; 27 float floatValue; 28 double doubleValue; 29 30 // fixed fixedValue; not supported by JavaIDL 31 }; 32 33 typedefsequence <StructMap> StructMapSeq; 34 typedefsequence <StructMap, 5> BoundStructMapSeq; 35 Fig. 26.10 IDL file testing many of the IDL keywords and types.Line 9Line 12Lines 15-28Line 33Line 34

  35. maps to array of type int of size 5 maps to methods with signatures: public void anAttribute(int x) and public int anAttribute() maps to interface InterfaceName (exposes public functionality available to remote clients) maps to method with signature: public int roAttribute() maps to immutable int field with value 42 methods will pass a copy of argument, i.e., call-by-value (methods defined with keyword out pass references as arguments, i.e., call-by-reference) treats arguments as references, regardless of primitives or object types 36 typedeflong IntArray[ 5 ]; 37 38 // This comment appears above 39 // the interface declaration for interfaceName 40 interface InterfaceName { 41 42 // comment above the readwrite attribute 43 attributelong anAttribute; 44 readonlyattributelong roAttribute; 45 const long constantValue = 42; 46 47 // comment above the methods 48 void seqMethod( in StructMapSeq seq ); 49 void boundSeqMethod( in BoundStructMapSeq seq ); 50 void arrayMethod( in IntArray array ); 51 void intOutMethod( inout long intValue ); 52 }; 53 }; // end module maptest Fig. 26.10 IDL file testing many of the IDL keywords and types.Line 36Line 40Line 43Line 44Line 45Line48Line49Line 50Line 51

  36. cannot inherit from this class comments appearing in IDL file default constructor-all fields initialized to 0, false, or null 1 package maptest; 2 3 4 /** 5 * maptest/StructMap.java 6 * Generated by the IDL-to-Java compiler (portable), version "3.0" 7 * from maptest.idl 8 * Monday, May 14, 2001 4:18:09 PM PDT 9 */ 10 11 12 // This comment appears in the generated files for StructMap 13 public finalclass StructMap implements 14 org.omg.CORBA.portable.IDLEntity 15 { 16 17 // This comment appears at start of the type declarations 18 public boolean boolValue = false; 19 public char charValue = ( char ) 0; 20 public char wCharValue = ( char ) 0; 21 public byte octetValue = ( byte ) 0; 22 public String stringValue = null; 23 public String wStringValue = null; 24 public short shortValue = ( short ) 0; 25 public short uShortValue = ( short ) 0; 26 public int longValue = ( int ) 0; 27 public int uLongValue = ( int ) 0; 28 public long longLongValue = ( long ) 0; 29 public long uLongLongValue = ( long ) 0; 30 public float floatValue = ( float ) 0; 31 public double doubleValue = ( double ) 0; 32 33 public StructMap () 34 { 35 } // ctor Fig. 26.11 IDL-generated file StructMap.javaLine 33Lines 12, 17Line 13

  37. 36 37 public StructMap( boolean _boolValue, char _charValue, 38 char _wCharValue, byte _octetValue, String _stringValue, 39 String wStringValue, short _shortValue, 40 short _uShortValue, int _longValue, int _uLongValue, 41 long _longLongValue, long _uLongLongValue, 42 float _floatValue, double _doubleValue ) 43 { 44 boolValue = _boolValue; 45 charValue = _charValue; 46 wCharValue = _wCharValue; 47 octetValue = _octetValue; 48 stringValue = _stringValue; 49 wStringValue = _wStringValue; 50 shortValue = _shortValue; 51 uShortValue = _uShortValue; 52 longValue = _longValue; 53 uLongValue = _uLongValue; 54 longLongValue = _longLongValue; 55 uLongLongValue = _uLongLongValue; 56 floatValue = _floatValue; 57 doubleValue = _doubleValue; 58 } // ctor 59 60 } // class StructMap Fig. 26.11 IDL-generated file StructMap.java

  38. generated from IDL file interface declaration generated from attribute anAttribute generated from read-only attribute roAttribute generated method definitions 1 package maptest; 2 3 4 /** 5 * maptest/InterfaceNameOperations.java 6 * Generated by the IDL-to-Java compiler (portable), version "3.0" 7 * from maptest.idl 8 * Monday, May 14, 2001 4:18:09 PM PDT 9 */ 10 11 12 // the interface declaration for InterfaceName 13 public interface InterfaceNameOperations 14 { 15 // comment above the readwrite attribute 16 int anAttribute(); 17 18 // comment above the readwrite attribute 19 void anAttribute(int newAnAttribute); 20 int roAttribute(); 21 22 // comment above methods 23 void seqMethod( maptest.StructMap[] seq ); 24 void boundSeqMethod( maptest.StructMap[] seq ); 25 void arrayMethod( int[] array ); 26 void intOutMethod( org.omg.CORBA.IntHolder intValue ); 27 } // interface InterfaceNameOperations Fig. 26.12 IDL-generated file InterfaceNameOperations.javaLine 13Lines 16, 19Line 20Lines 23-26

  39. constants will always appear in file named after the interface declaration 1 package maptest; 2 3 /** 4 * maptest/InterfaceName.java 5 * Generated by the IDL-to-Java compiler (portable), version "3.0" 6 * from maptest.idl 7 * Monday, May 14, 2001 4:18:09 PM PDT 8 */ 9 10 // the interface declaration for InterfaceName 11 public interface InterfaceName extends InterfaceNameOperations, 12 org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity 13 { 14 public static final int constantValue = ( int ) ( 42 ); 15 } // interface InterfaceName Fig. 26.13 IDL-generated file InterfaceName.javaLine 14

  40. 26.5 CORBA Basics (cont.) • Method call types: • synchronous • client makes call on remote blocking method • standard method call on remote service • client makes call on remote non-blocking method • remote method qualified with oneway modifier in IDL file • remote method receives only in arguments • asynchronous • client makes synchronous call on remote method, remote service responds at a later time by calling method on client • must be qualified with oneway to prevent deadlock

  41. 26.5 CORBA Basics (cont.) Fig. 26.14 Deadlock caused by client calling a server that calls the client.

  42. 26.6 Example: AlarmClock • Example of a typical push-model application • client sets alarm on server • server notifies client each time internal alarm event ocurrs

  43. receive copy of listener stub declare non-blocking method 1 // Fig. 26.15: alarmclock1.idl 2 // The IDL for the AlarmClock example 3 4 module alarm { 5 interface AlarmListener { 6 void updateTime( in long long newTime ); 7 }; 8 9 interface AlarmClock { 10 conststringNAME = "AlarmClock"; 11 12 oneway void addAlarmListener( in string listenerName, 13 in AlarmListener listener ); 14 15 void setAlarm( instring listenerName, 16 in long long seconds ); 17 }; 18 }; Fig. 26.15 alarmclock1.idlLine 12Line 13

  44. register service with Naming Service 1 // Fig. 26.16: AlarmClockImpl.java 2 // Implementation of AlarmClock server. 3 4 package com.deitel.advjhtp1.idl.alarm; 5 6 // Java core packages 7 import java.util.*; 8 9 // Java extension packages 10 import org.omg.CORBA.ORB; 11 import org.omg.CosNaming.*; 12 import org.omg.CosNaming.NamingContextPackage.*; 13 14 public class AlarmClockImpl extends _AlarmClockImplBase { 15 16 // list contains name/alarm pairs of 17 // registered objects waiting for an alarm 18 private Hashtable alarmList = new Hashtable(); 19 20 // register AlarmClockImpl object with Naming Service 21 public void register( String corbaName, String params[] ) 22 throws org.omg.CORBA.ORBPackage.InvalidName, 23 org.omg.CosNaming.NamingContextPackage.InvalidName, 24 CannotProceed, NotFound 25 { 26 if ( ( corbaName == null ) || 27 ( corbaName.trim().length() == 0 ) ) 28 throw new IllegalArgumentException( 29 "Registration name cannot be null or blank"); 30 31 // create and initialize ORB 32 ORB orb = ORB.init( params, null ); 33 34 // register this object with ORB 35 orb.connect( this ); Fig. 26.16 AlarmClockImpl.java is the AlarmClock server implementation.Line 21

  45. add listener to internal list 36 37 // retrieve reference to Naming Service 38 org.omg.CORBA.Object corbaObject = 39 orb.resolve_initial_references( "NameService" ); 40 NamingContext naming = 41 NamingContextHelper.narrow( corbaObject ); 42 43 // create NameComponent array with path information to 44 // find this object 45 NameComponent namingComponent = 46 new NameComponent( corbaName, "" ); 47 NameComponent path[] = { namingComponent }; 48 49 // bind AlarmClockImpl object with ORB 50 naming.rebind( path, this ); 51 System.out.println( "Rebind complete" ); 52 } 53 54 // method used by clients wanting to register 55 // as callback/listener objects 56 public void addAlarmListener( String listenerName, 57 AlarmListener listener ) 58 throws DuplicateNameException 59 { 60 if ( listenerName == null || 61 listenerName.trim().length() == 0 ) 62 throw new IllegalArgumentException( 63 "Name cannot be null or blank"); 64 else 65 66 if ( alarmList.get( listenerName ) != null ) 67 throw new IllegalArgumentException( 68 "Name is already registered, please choose another" ); Fig. 26.16 AlarmClockImpl.java is the AlarmClock server implementation.Line 56

  46. schedule an alarm for time specified by remote client 69 else 70 71 if ( listener == null ) 72 throw new IllegalArgumentException( 73 "Listener cannot be null" ); 74 75 // create new Timer and save it under listener name 76 alarmList.put( listenerName, new AlarmTimer( listener ) ); 77 } 78 79 // Set an alarm for a client. If client not registered 80 // throw a runtime exception. 81 public void setAlarm( String name, long seconds ) 82 { 83 // get alarm for particular client 84 AlarmTimer timer = ( AlarmTimer ) alarmList.get( name ); 85 86 if ( timer == null ) 87 throw new IllegalArgumentException( 88 "No clock found for the incoming name" ); 89 else 90 timer.schedule( new TaskWrapper(timer.getListener(), 91 seconds), seconds * 1000 ); 92 } 93 94 // main method to execute AlarmClock server 95 public static void main( String args[] ) throws Exception 96 { 97 AlarmClockImpl alarm = new AlarmClockImpl(); 98 alarm.register( AlarmClock.NAME, args ); 99 100 java.lang.Object object = new java.lang.Object(); 101 Fig. 26.16 AlarmClockImpl.java is the AlarmClock server implementation.Line 90

  47. calls client when alarm expires 102 // keep server alive 103 synchronized( object ) { 104 object.wait(); 105 } 106 } 107 108 // Every listener get an AlarmTimer assigned to them. 109 private class AlarmTimer extends Timer { 110 111 // The listener this Timer is assigned to. 112 private AlarmListener listener; 113 114 public AlarmTimer( AlarmListener l ) 115 { 116 listener = l; 117 } 118 119 // Accessor method so we can get to the listener 120 // object reference. 121 public AlarmListener getListener() 122 { 123 return listener; 124 } 125 } // end of private inner class TaskWrapper 126 127 // TaskWrapper takes care of calling our clients 128 // when their alarm expires. 129 private class TaskWrapper extends TimerTask { 130 131 // The reference to our listener 132 private AlarmListener listener; 133 private long seconds; 134 Fig. 26.16 AlarmClockImpl.java is the AlarmClock server implementation.Line 129

  48. call-back 135 // TaskWrapper needs to know who to call and 136 // how long was the alarm set (in seconds). 137 public TaskWrapper( AlarmListener l, long s ) 138 { 139 listener = l; 140 seconds = s; 141 } 142 143 public void run() 144 { 145 // Go wake them up! 146 listener.updateTime(seconds); 147 148 // Discard this TaskWrapper. When the client 149 // wants a new alarm we create a new TaskWrapper. 150 this.cancel(); 151 } 152 } // end private inner class TaskWrapper 153 } // end class AlarmClockImpl Fig. 26.16 AlarmClockImpl.java is the AlarmClock server implementation.Line 146

  49. 1 // Fig. 26.17: ClockClientGUI.java 2 // GUI used by the AlarmClockClient. 3 4 package com.deitel.advjhtp1.idl.alarm; 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 10 // Java extension packages 11 import javax.swing.*; 12 13 public class ClockClientGUI extends JFrame { 14 private JLabel outputLabel; 15 16 // set up GUI 17 public ClockClientGUI() 18 { 19 super( "Clock GUI" ); 20 21 outputLabel = 22 new JLabel( "The alarm has not gone off..." ); 23 getContentPane().add( outputLabel, BorderLayout.NORTH ); 24 25 setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 26 setResizable( false ); 27 Dimension screenSize = 28 Toolkit.getDefaultToolkit().getScreenSize(); 29 setSize( new Dimension( 450, 100 ) ); 30 setLocation( ( screenSize.width - 450 ) / 2, 31 ( screenSize.height - 100 ) / 2 ); 32 } 33 Fig. 26.17 ClockClientGUI informs the user when the alarm has sounded.

  50. 34 // set label's text 35 public void setText( String message ) 36 { 37 outputLabel.setText( message ); 38 } 39 40 } // end of class ClockClientGUI Fig. 26.17 ClockClientGUI informs the user when the alarm has sounded.

More Related