📄 agentmanagerimp.java
字号:
/* * $Source: /home/data/cvsroot/src/jacomma/platform/engine/AgentManagerImp.java,v $ * $Revision: 1.8 $ * $Date: 2000/10/28 20:09:08 $ * * This file is part of the jacomma framework * Copyright (c) 2000 Dimitrios Vyzovitis * mailto:dviz@egnatia.ee.auth.gr * * * * * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA */package jacomma.platform.engine;import java.util.Date;import java.util.List;import java.util.ArrayList;import java.util.Iterator;import java.util.Map;import java.util.HashMap;import java.util.Set;import java.util.HashSet;import java.util.StringTokenizer;import java.net.InetAddress;import jacomma.icm.type.Handle;import jacomma.icm.type.Request;import jacomma.icm.type.Message;import jacomma.icm.io.Connection;import jacomma.platform.core.Agent;import jacomma.platform.core.AgentObserver;import jacomma.platform.core.AgentManager;import jacomma.platform.core.AgentEvent;import jacomma.platform.core.RemoteAgentEvent;import jacomma.platform.core.MessagePattern;import jacomma.platform.core.RequestFailedException;import jacomma.util.Collections;import jacomma.util.CheckedException;/** * TBA **/public class AgentManagerImp implements AgentManager, AgentMonitor { ThreadGroup tg_; Map thread_to_agent_; Map agent_observers_; Map registered_agents_; MessageRouter router_; Agent self_; public AgentManagerImp(){} public Agent createAgent( String name, AgentObserver obs ) throws RequestFailedException { return createAgent( Handle.createHandle( name ), obs ); } public Agent createAgent( String name, AgentObserver obs, Date lt ) throws RequestFailedException { return createAgent( Handle.createHandle( name ), obs, lt ); } public Agent createAgent( Handle han, AgentObserver obs ) throws RequestFailedException { AgentImp ag = null; try { ag = _createAgent( han, obs ); ag.register(); return ag; } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmRegisterAgent, exc ); } catch ( RequestFailedException exc ) { _destroyAgent( ag ); throw exc; } } public Agent createAgent( Handle han, AgentObserver obs, Date lt ) throws RequestFailedException { AgentImp ag = null; try { ag = _createAgent( han, obs ); ag.register( lt ); return ag; } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmRegisterAgent, exc ); } catch ( RequestFailedException exc ) { _destroyAgent( ag ); throw exc; } } public Agent attachAgent( String name, AgentObserver obs ) throws RequestFailedException { return attachAgent( Handle.createHandle( name ), obs ); } public Agent attachAgent( String name, AgentObserver obs, Date lt ) throws RequestFailedException { return attachAgent( Handle.createHandle( name ), obs, lt ); } public Agent attachAgent( Handle han, AgentObserver obs ) throws RequestFailedException { AgentImp ag = null; try { ag = _createAgent( han, obs ); ag.attach( Request.commServer.getHome() ); return ag; } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmRegisterAgent, exc ); } catch ( RequestFailedException exc ) { _destroyAgent( ag ); throw exc; } } public Agent attachAgent( Handle han, AgentObserver obs, Date lt ) throws RequestFailedException { AgentImp ag = null; try { ag = _createAgent( han, obs ); ag.attach( Request.commServer.getHome(), lt ); return ag; } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmRegisterAgent, exc ); } catch ( RequestFailedException exc ) { _destroyAgent( ag ); throw exc; } } public synchronized void disconnect() throws RequestFailedException { List ags = new ArrayList( registered_agents_.keySet() ); ags.remove( self_ ); for ( Iterator it = ags.iterator(); it.hasNext(); ) { Agent ag = (Agent)it.next(); ((AgentObserver)agent_observers_.get( ag )).onDisconnect( ag ); } self_.detach(); } public synchronized void reconnect() throws RequestFailedException { self_.attach(); List ags = new ArrayList( registered_agents_.keySet() ); ags.remove( self_ ); for ( Iterator it = ags.iterator(); it.hasNext(); ) { Agent ag = (Agent)it.next(); ((AgentObserver)agent_observers_.get( ag )).onReconnect( ag ); } } public boolean ping( Handle han ) throws RequestFailedException { try { final Handle id = EngineUtilities.makeIdentifier( self_.getHandle() ); self_.send( Request.createPingRequest( id, han ) ); List resp = (List)self_.recv( RequestPattern .createListPattern( Request.icmPingAgent, EngineUtilities.makeIdentifierTest( id ) ) ) .getContent(); return Request.icmOk.equals( resp.get( 1 ) ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } } public synchronized List listManagedAgents() { // return a safe copy return new ArrayList( registered_agents_.values() ); } public List listAgents( ) throws RequestFailedException { try { Handle id = EngineUtilities.makeIdentifier( self_.getHandle() ); self_.send( Request.createListRequest( id ) ); List resp = (List)self_.recv( RequestPattern .createListPattern( Request.icmListAgents, EngineUtilities.makeIdentifierTest( id ) ) ) .getContent(); if ( !Request.icmFailed.equals( resp.get( 1 ) ) ) return (List)resp.get( 1 ); else throw new RequestFailedException( Request.icmListAgents, "Refused by commServer" ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } } public List listAgents( InetAddress host ) throws RequestFailedException { try { Handle id = EngineUtilities.makeIdentifier( self_.getHandle() ); self_.send( Request.createListRequest( id, host ) ); List resp = (List)self_.recv( RequestPattern. createListPattern( host, Request.icmListAgents, EngineUtilities.makeIdentifierTest( id ) ) ) .getContent(); if ( !Request.icmFailed.equals( resp.get( 1 ) ) ) return (List)resp.get( 1 ); else throw new RequestFailedException( Request.icmListAgents, "Refused by commServer" ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } } public Handle lookupLocation( Handle target ) throws RequestFailedException { try { Handle id = EngineUtilities.makeIdentifier( self_.getHandle() ); self_.send( Request.createLookupRequest( id, target ) ); List resp = (List)self_.recv( RequestPattern. createListPattern( Request.icmLookupLocation, EngineUtilities.makeIdentifierTest( id ) ) ) .getContent(); if ( !Request.icmFailed.equals( resp.get( 1 ) ) ) return (Handle)resp.get( 2 ); else throw new RequestFailedException( Request.icmLookupLocation, "Agent unknown" ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } } public Handle lookupLocation( Handle target, InetAddress host ) throws RequestFailedException { try { Handle id = EngineUtilities.makeIdentifier( self_.getHandle() ); self_.send( Request.createLookupRequest( id, target, host ) ); List resp = (List)self_.recv( RequestPattern. createListPattern( host, Request.icmLookupLocation, EngineUtilities.makeIdentifierTest( id ) ) ) .getContent(); if ( !Request.icmFailed.equals( resp.get( 1 ) ) ) return (Handle)resp.get( 2 ); else throw new RequestFailedException( Request.icmLookupLocation, "Agent unknown" ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmListAgents, exc ); } } public void monitorAgent( Handle han, AgentObserver obs ) throws RequestFailedException { try { _monitorAgent( han, Request.commServer, obs ); } catch ( InterruptedException exc ) { throw new RequestFailedException( Request.icmMonitorAgent, exc ); } catch ( java.io.IOException exc ) { throw new RequestFailedException( Request.icmMonitorAgent, exc );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -