Source code: org/vrspace/server/object/Mirror.java
1 package org.vrspace.server.object;
2
3 import java.util.*;
4
5 import org.vrspace.server.*;
6 import org.vrspace.util.*;
7 import org.vrspace.attributes.*;
8
9 /**
10 A Mirror connects to the remote host on startup, and then then simply mirrors (copies) all seen objects to local database
11 */
12 // implements Admin?
13 public class Mirror extends Gate implements Daemon, Observer {
14 public String login;
15 public String password;
16
17 private Connection conn;
18 //private Server server;
19 private boolean active = false;
20 protected Dispatcher dispatcher;
21 public void initialized() {
22 Logger.logInfo( "Dispatcher initialized, "+getID()+"connecting to remote host..." );
23 try {
24 conn = new Connection(host, port, login, password);
25 conn.addObserver( this );
26 Logger.logInfo( "Mirror "+db_id+" connected to "+host+":"+port+" as "+login );
27 } catch ( Throwable t ) {
28 Logger.logError( t );
29 }
30 }
31 public void startup( Dispatcher d ) {
32 this.dispatcher = d;
33 }
34 public void shutdown() {
35 try {
36 // send 'offline'
37 conn.close();
38 Logger.logInfo( "Gate "+db_id+" disconnected from "+host+":"+port );
39 } catch ( Throwable t ) {
40 Logger.logError( t );
41 }
42 }
43 /**
44 Adds observer.
45 */
46 public void addObserver( Observer o ) {
47 if ( dispatcher == null && o instanceof Client ) {
48 Client c = (Client) o;
49 dispatcher = c.getDispatcher();
50 //server = dispatcher.getServer( c );
51 // send 'online' - how?
52 //conn.send( "/my online true" );
53 Logger.logInfo( "Mirror "+db_id+" initialized, accepting connections from "+host+":"+port+" as "+login );
54 }
55 super.addObserver( o );
56 }
57 /**
58 Process remote message - TODO
59 */
60 public void update( Observable conn, Object msg ) {
61 if ( msg instanceof String ) {
62 Logger.logDebug( "Mirror "+db_id+" - Remote message: "+msg );
63 // analyse this msg
64 if ( dispatcher == null ) {
65 // send 'busy'
66 Logger.logWarning( "Remote message before first observer: "+msg );
67 } else {
68 if ( active == false ) {
69 active = true;
70 Logger.logInfo( "Mirror "+db_id+" started" );
71 }
72 try {
73 //conn.deleteObserver(this);
74 /*
75 PipedSession s = new PipedSession( (Connection)conn, server, dispatcher ); // server? - TEMP DISABLED
76 conn.addObserver( s );
77 Thread thread = new Thread( s, "Mirror "+db_id+" to "+s.getId());
78 thread.start();
79 */
80 /*
81 conn = new Connection(host, port, login, password);
82 conn.addObserver( this );
83 */
84 } catch ( Throwable t ) {
85 Logger.logError( t );
86 }
87 }
88 } else if ( msg instanceof Throwable ) {
89 Logger.logError( (Throwable) msg );
90 } else {
91 Logger.logError( ""+msg );
92 }
93 }
94 }