📄 network.java
字号:
/* * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies * Copyright (C) 1996 - 2000 BULL * Copyright (C) 1996 - 2000 INRIA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser 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. * * Initial developer(s): Dyade * Contributor(s): ScalAgent Distributed Technologies */package fr.dyade.aaa.agent;import java.io.*;import java.util.Vector;import org.objectweb.util.monolog.api.BasicLevel;import org.objectweb.util.monolog.api.Logger;import fr.dyade.aaa.util.Arrays;/** * The <code>Network</code> abstract class provides .. */public abstract class Network implements MessageConsumer, NetworkMBean { /** * Period of time in ms between two activations of watch-dog thread, * default value is 1000L (1 second). * This value can be adjusted for all network components by setting * <code>WDActivationPeriod</code> global property or for a particular * network by setting <code>\<DomainName\>.WDActivationPeriod</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ long WDActivationPeriod = 1000L; /** * Gets the WDActivationPeriod value. * * @return the WDActivationPeriod value */ public long getWDActivationPeriod() { return WDActivationPeriod; } /** * Sets the WDActivationPeriod value. * * @param WDActivationPeriod the WDActivationPeriod value */ public void setWDActivationPeriod(long WDActivationPeriod) { this.WDActivationPeriod = WDActivationPeriod; } /** * Number of try at stage 1, default value is 30. * This value can be adjusted for all network components by setting * <code>WDNbRetryLevel1</code> global property or for a particular * network by setting <code>\<DomainName\>.WDNbRetryLevel1</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ int WDNbRetryLevel1 = 30; /** * Gets the WDNbRetryLevel1 value. * * @return the WDNbRetryLevel1 value */ public int getWDNbRetryLevel1() { return WDNbRetryLevel1; } /** * Sets the WDNbRetryLevel1 value. * * @param WDNbRetryLevel1 the WDNbRetryLevel1 value */ public void setWDNbRetryLevel1(int WDNbRetryLevel1) { this.WDNbRetryLevel1 = WDNbRetryLevel1; } /** * Period of time in ms between two connection try at stage 1, default * value is WDActivationPeriod divided by 2. * This value can be adjusted for all network components by setting * <code>WDRetryPeriod1</code> global property or for a particular * network by setting <code>\<DomainName\>.WDRetryPeriod1</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ long WDRetryPeriod1 = WDActivationPeriod/2; /** * Gets the WDRetryPeriod1 value. * * @return the WDRetryPeriod1 value */ public long getWDRetryPeriod1() { return WDRetryPeriod1; } /** * Sets the WDRetryPeriod1 value. * * @param WDRetryPeriod1 the WDRetryPeriod1 value */ public void setWDRetryPeriod1(long WDRetryPeriod1) { this.WDRetryPeriod1 = WDRetryPeriod1; } /** * Number of try at stage 2, default value is 55. * This value can be adjusted for all network components by setting * <code>WDNbRetryLevel2</code> global property or for a particular * network by setting <code>\<DomainName\>.WDNbRetryLevel2</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ int WDNbRetryLevel2 = 55; /** * Gets the WDNbRetryLevel2 value. * * @return the WDNbRetryLevel2 value */ public int getWDNbRetryLevel2() { return WDNbRetryLevel2; } /** * Sets the WDNbRetryLevel2 value. * * @param WDNbRetryLevel2 the WDNbRetryLevel2 value */ public void setWDNbRetryLevel2(int WDNbRetryLevel2) { this.WDNbRetryLevel2 = WDNbRetryLevel2; } /** * Period of time in ms between two connection try at stage 2, default * value is 5000L (5 seconds). * This value can be adjusted for all network components by setting * <code>WDRetryPeriod2</code> global property or for a particular * network by setting <code>\<DomainName\>.WDRetryPeriod2</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ long WDRetryPeriod2 = 5000L; /** * Gets the WDRetryPeriod2 value. * * @return the WDRetryPeriod2 value */ public long getWDRetryPeriod2() { return WDRetryPeriod2; } /** * Sets the WDRetryPeriod2 value. * * @param WDRetryPeriod2 the WDRetryPeriod2 value */ public void setWDRetryPeriod2(long WDRetryPeriod2) { this.WDRetryPeriod2 = WDRetryPeriod2; } /** * Period of time in ms between two connection try at stage 3, default * value is 60000L (1 minute). * This value can be adjusted for all network components by setting * <code>WDRetryPeriod3</code> global property or for a particular * network by setting <code>\<DomainName\>.WDRetryPeriod3</code> * specific property. * <p> * Theses properties can be fixed either from <code>java</code> launching * command, or in <code>a3servers.xml</code> configuration file. */ long WDRetryPeriod3 = 60000L; /** * Gets the WDRetryPeriod3 value. * * @return the WDRetryPeriod3 value */ public long getWDRetryPeriod3() { return WDRetryPeriod3; } /** * Sets the WDRetryPeriod3 value. * * @param WDRetryPeriod3 the WDRetryPeriod3 value */ public void setWDRetryPeriod3(long WDRetryPeriod3) { this.WDRetryPeriod3 = WDRetryPeriod3; } protected Logger logmon = null; /** Id. of local server. */ protected short sid; /** Index of local server in status and matrix arrays. */ protected int idxLS; /** * List of id. for all servers in the domain, this list is sorted and * is used as index for internal tables. */ protected short[] servers; /** Filename for servers storage */ transient protected String serversFN = null; /** Logical timestamp information for messages in domain, stamp[idxLS)] * for messages sent, and stamp[index(id] for messages received. */ private int[] stamp; /** Buffer used to optimise transactions*/ private byte[] stampbuf = null; /** */ private int[] bootTS = null; /** Filename for boot time stamp storage */ transient protected String bootTSFN = null; /** The component's name as it appears in logging. */ protected String name; /** The domain name. */ protected String domain; /** The communication port. */ protected int port; /** The <code>MessageVector</code> associated with this network component. */ protected MessageVector qout; /** * Returns this session's name. * * @return this session's name. */ public final String getName() { return name; } /** * Returns the corresponding domain's name. * * @return this domain's name. */ public final String getDomainName() { return domain; } /** * Returns a string representation of this consumer. * * @return A string representation of this consumer. */ public String toString() { StringBuffer strbuf = new StringBuffer(); strbuf.append("(").append(super.toString()); strbuf.append(",name=").append(getName()); if (qout != null) strbuf.append(",qout=").append(qout.size()); if (servers != null) { for (int i=0; i<servers.length; i++) { strbuf.append(",(").append(servers[i]).append(','); strbuf.append(stamp[i]).append(')'); } } strbuf.append(")"); return strbuf.toString(); } /** * Creates a new network component. This simple constructor is required in * order to use <code>Class.newInstance()</code> method during configuration. * The configuration of component is then done by <code>init</code> method. */ public Network() { } /** * Insert a message in the <code>MessageQueue</code>. * This method is used during initialisation to restore the component * state from persistent storage. * * @param msg the message */ public void insert(Message msg) { qout.insert(msg); } /** * Saves information to persistent storage. */ public void save() throws IOException {} /** * Restores component's information from persistent storage. * If it is the first load, initializes it. */ public void restore() throws Exception { sid = AgentServer.getServerId(); idxLS = index(sid); // Loads the logical clock. stampbuf = AgentServer.getTransaction().loadByteArray(name); if (stampbuf == null) { // Creates the new stamp array and the boot time stamp, stampbuf = new byte[4*servers.length]; stamp = new int[servers.length]; bootTS = new int[servers.length]; // Then initializes them for (int i=0; i<servers.length; i++) { if (i != idxLS) { stamp[i] = -1; bootTS[i] = -1; } else { stamp[i] = 0; bootTS[i] = (int) (System.currentTimeMillis() /1000L); } } // Save the servers configuration and the logical time stamp. AgentServer.getTransaction().save(servers, serversFN); AgentServer.getTransaction().save(bootTS, bootTSFN); AgentServer.getTransaction().saveByteArray(stampbuf, name); } else { // Loads the domain configurations short[] s = (short[]) AgentServer.getTransaction().load(serversFN); bootTS = (int[]) AgentServer.getTransaction().load(bootTSFN); stamp = new int[s.length]; for (int i=0; i<stamp.length; i++) { stamp[i] = ((stampbuf[(i*4)+0] & 0xFF) << 24) + ((stampbuf[(i*4)+1] & 0xFF) << 16) + ((stampbuf[(i*4)+2] & 0xFF) << 8) + (stampbuf[(i*4)+3] & 0xFF); } // Joins with the new domain configuration: if ((servers != null) && !Arrays.equals(servers, s)) { for (int i=0; i<servers.length; i++) logmon.log(BasicLevel.DEBUG, "servers[" + i + "]=" + servers[i]); for (int i=0; i<s.length; i++) logmon.log(BasicLevel.DEBUG, "servers[" + i + "]=" + s[i]); throw new IOException("Network configuration changed"); } } } /** * Initializes a new network component. This method is used in order to * easily creates and configure a Network component from a class name. * So we can use the <code>Class.newInstance()</code> method for create * (whitout any parameter) the component, then we can initialize it with * this method.<br> * This method initializes the logical clock for the domain. * * @param name The domain name. * @param port The listen port. * @param servers The list of servers directly accessible from this * network interface. */ public void init(String name, int port, short[] servers) throws Exception { this.name = AgentServer.getName() + '.' + name; qout = new MessageVector(this.name, AgentServer.getTransaction().isPersistent()); this.domain = name; this.port = port;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -