001 package com.croftsoft.apps.vaft.core; 002 003 import java.net.MalformedURLException; 004 import java.rmi.*; 005 import java.rmi.server.*; 006 import java.util.Enumeration; 007 import java.util.Vector; 008 009 import com.croftsoft.core.role.actor.*; 010 011 /********************************************************************* 012 * <P> 013 * @author 014 * <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A> 015 * @version 016 * 1998-04-26 017 *********************************************************************/ 018 019 public class Host extends UnicastRemoteObject implements 020 Actor, ActorRemote, HostRemote, SwapRemote, ShutdownRemote 021 ////////////////////////////////////////////////////////////////////// 022 ////////////////////////////////////////////////////////////////////// 023 { 024 025 private String rmi_object_name; 026 private String password; 027 private HostList hostList; 028 private Thread hostList_Thread; 029 private Vector thread_Vector; 030 031 ////////////////////////////////////////////////////////////////////// 032 ////////////////////////////////////////////////////////////////////// 033 034 /********************************************************************* 035 * Instantiates a Host then binds it to the registry. 036 *********************************************************************/ 037 public static Host bind_Host ( 038 String rmi_object_name, String password, HostList hostList ) 039 throws AlreadyBoundException, MalformedURLException, RemoteException 040 ////////////////////////////////////////////////////////////////////// 041 { 042 Host host = new Host ( rmi_object_name, password, hostList ); 043 //Naming.bind ( rmi_object_name, host ); 044 Naming.rebind ( rmi_object_name, host ); 045 return host; 046 } 047 048 public Host ( 049 String rmi_object_name, String password, HostList hostList ) 050 throws RemoteException 051 ////////////////////////////////////////////////////////////////////// 052 { 053 this.rmi_object_name = rmi_object_name; 054 this.password = password; 055 this.hostList = hostList; 056 057 hostList_Thread = new Thread ( hostList ); 058 hostList_Thread.start ( ); 059 060 this.thread_Vector = new Vector ( ); 061 } 062 063 ////////////////////////////////////////////////////////////////////// 064 ////////////////////////////////////////////////////////////////////// 065 066 public HostList getHostList ( ) { return hostList; } 067 068 ////////////////////////////////////////////////////////////////////// 069 // Interface methods 070 ////////////////////////////////////////////////////////////////////// 071 072 public String [ ] getRoles ( ) 073 ////////////////////////////////////////////////////////////////////// 074 { 075 return new String [ ] { 076 "com.orbs.pub.agent.role.Actor", 077 "com.orbs.pub.agent.role.ActorRemote", 078 "com.orbs.pub.app.agent.vaft.core.HostRemote", 079 "com.orbs.pub.app.agent.vaft.core.SwapRemote" }; 080 } 081 082 public String [ ] getRolesRemote ( ) throws RemoteException 083 ////////////////////////////////////////////////////////////////////// 084 { 085 return getRoles ( ); 086 } 087 088 public void host ( Actor actor ) throws RemoteException 089 ////////////////////////////////////////////////////////////////////// 090 { 091 String [ ] roles = actor.getRoles ( ); 092 if ( roles == null ) return; 093 for ( int i = 0; i < roles.length; i++ ) 094 { 095 if ( "java.lang.Runnable".equals ( roles [ i ] ) ) 096 { 097 Thread thread = new Thread ( ( Runnable ) actor ); 098 thread_Vector.addElement ( thread ); 099 thread.start ( ); 100 break; 101 } 102 } 103 } 104 105 public HostList swap ( HostList hostList ) throws RemoteException 106 ////////////////////////////////////////////////////////////////////// 107 { 108 return this.hostList.swap ( hostList ); 109 } 110 111 public synchronized boolean shutdown ( String password ) 112 throws RemoteException 113 ////////////////////////////////////////////////////////////////////// 114 { 115 if ( this.password != null ) 116 { 117 if ( !this.password.equals ( password ) ) return false; 118 } 119 120 try { Naming.unbind ( rmi_object_name ); } 121 catch ( Exception ex ) { } 122 123 hostList.setRunning ( false ); 124 hostList_Thread.interrupt ( ); 125 126 Enumeration e = thread_Vector.elements ( ); 127 while ( e.hasMoreElements ( ) ) 128 { 129 Thread thread = ( Thread ) e.nextElement ( ); 130 try { thread.stop ( ); } catch ( Exception ex ) { } 131 } 132 thread_Vector = null; 133 134 return true; 135 } 136 137 ////////////////////////////////////////////////////////////////////// 138 ////////////////////////////////////////////////////////////////////// 139 }