tp.java

来自「JGRoups源码」· Java 代码 · 共 1,778 行 · 第 1/5 页

JAVA
1,778
字号
    /* ------------------------------------------------------------------------------- */    /*------------------------------ Protocol interface ------------------------------ */    public void init() throws Exception {        super.init();        if(bind_addr != null) {            Map m=new HashMap(1);            m.put("bind_addr", bind_addr);            passUp(new Event(Event.CONFIG, m));        }    }    /**     * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads     */    public void start() throws Exception {        timer=stack.timer;        if(timer == null)            throw new Exception("timer is null");        if(enable_diagnostics) {            diag_handler=new DiagnosticsHandler();            diag_handler.start();        }        if(use_incoming_packet_handler) {            incoming_packet_queue=new Queue();            incoming_packet_handler=new IncomingPacketHandler();            incoming_packet_handler.start();        }        if(loopback) {            incoming_msg_queue=new Queue();            incoming_msg_handler=new IncomingMessageHandler();            incoming_msg_handler.start();        }        if(use_outgoing_packet_handler) {            outgoing_queue=new BoundedLinkedQueue(outgoing_queue_max_size);            outgoing_packet_handler=new OutgoingPacketHandler();            outgoing_packet_handler.start();        }        if(enable_bundling) {            bundler=new Bundler();        }        passUp(new Event(Event.SET_LOCAL_ADDRESS, local_addr));    }    public void stop() {        if(diag_handler != null) {            diag_handler.stop();            diag_handler=null;        }        // 1. Stop the outgoing packet handler thread        if(outgoing_packet_handler != null)            outgoing_packet_handler.stop();        // 2. Stop the incoming packet handler thread        if(incoming_packet_handler != null)            incoming_packet_handler.stop();        // 3. Finally stop the incoming message handler        if(incoming_msg_handler != null)            incoming_msg_handler.stop();    }    /**     * Setup the Protocol instance according to the configuration string     * @return true if no other properties are left.     *         false if the properties still have data in them, ie ,     *         properties are left over and not handled by the protocol stack     */    public boolean setProperties(Properties props) {        super.setProperties(props);        boolean ignore_systemprops=Util.isBindAddressPropertyIgnored();        String str=Util.getProperty(new String[]{Global.BIND_ADDR, Global.BIND_ADDR_OLD}, props, "bind_addr",                             ignore_systemprops, null);        if(str != null) {            try {                bind_addr=InetAddress.getByName(str);            }            catch(UnknownHostException unknown) {                if(log.isFatalEnabled()) log.fatal("(bind_addr): host " + str + " not known");                return false;            }            props.remove("bind_addr");        }        str=props.getProperty("use_local_host");        if(str != null) {            use_local_host=new Boolean(str).booleanValue();            props.remove("use_local_host");        }        str=props.getProperty("bind_to_all_interfaces");        if(str != null) {            receive_on_all_interfaces=new Boolean(str).booleanValue();            props.remove("bind_to_all_interfaces");            log.warn("bind_to_all_interfaces has been deprecated; use receive_on_all_interfaces instead");        }        str=props.getProperty("receive_on_all_interfaces");        if(str != null) {            receive_on_all_interfaces=new Boolean(str).booleanValue();            props.remove("receive_on_all_interfaces");        }        str=props.getProperty("receive_interfaces");        if(str != null) {            try {                receive_interfaces=parseInterfaceList(str);                props.remove("receive_interfaces");            }            catch(Exception e) {                log.error("error determining interfaces (" + str + ")", e);                return false;            }        }        str=props.getProperty("send_on_all_interfaces");        if(str != null) {            send_on_all_interfaces=new Boolean(str).booleanValue();            props.remove("send_on_all_interfaces");        }        str=props.getProperty("send_interfaces");        if(str != null) {            try {                send_interfaces=parseInterfaceList(str);                props.remove("send_interfaces");            }            catch(Exception e) {                log.error("error determining interfaces (" + str + ")", e);                return false;            }        }        str=props.getProperty("bind_port");        if(str != null) {            bind_port=Integer.parseInt(str);            props.remove("bind_port");        }        str=props.getProperty("port_range");        if(str != null) {            port_range=Integer.parseInt(str);            props.remove("port_range");        }        str=props.getProperty("loopback");        if(str != null) {            loopback=Boolean.valueOf(str).booleanValue();            props.remove("loopback");        }        str=props.getProperty("discard_incompatible_packets");        if(str != null) {            discard_incompatible_packets=Boolean.valueOf(str).booleanValue();            props.remove("discard_incompatible_packets");        }        // this is deprecated, just left for compatibility (use use_incoming_packet_handler)        str=props.getProperty("use_packet_handler");        if(str != null) {            use_incoming_packet_handler=Boolean.valueOf(str).booleanValue();            props.remove("use_packet_handler");            if(warn) log.warn("'use_packet_handler' is deprecated; use 'use_incoming_packet_handler' instead");        }        str=props.getProperty("use_incoming_packet_handler");        if(str != null) {            use_incoming_packet_handler=Boolean.valueOf(str).booleanValue();            props.remove("use_incoming_packet_handler");        }        str=props.getProperty("use_outgoing_packet_handler");        if(str != null) {            use_outgoing_packet_handler=Boolean.valueOf(str).booleanValue();            props.remove("use_outgoing_packet_handler");        }        str=props.getProperty("outgoing_queue_max_size");        if(str != null) {            outgoing_queue_max_size=Integer.parseInt(str);            props.remove("outgoing_queue_max_size");            if(outgoing_queue_max_size <= 0) {                if(log.isWarnEnabled())                    log.warn("outgoing_queue_max_size of " + outgoing_queue_max_size + " is invalid, setting it to 1");                outgoing_queue_max_size=1;            }        }        str=props.getProperty("max_bundle_size");        if(str != null) {            int bundle_size=Integer.parseInt(str);            if(bundle_size > max_bundle_size) {                if(log.isErrorEnabled()) log.error("max_bundle_size (" + bundle_size +                        ") is greater than largest TP fragmentation size (" + max_bundle_size + ')');                return false;            }            if(bundle_size <= 0) {                if(log.isErrorEnabled()) log.error("max_bundle_size (" + bundle_size + ") is <= 0");                return false;            }            max_bundle_size=bundle_size;            props.remove("max_bundle_size");        }        str=props.getProperty("max_bundle_timeout");        if(str != null) {            max_bundle_timeout=Long.parseLong(str);            if(max_bundle_timeout <= 0) {                if(log.isErrorEnabled()) log.error("max_bundle_timeout of " + max_bundle_timeout + " is invalid");                return false;            }            props.remove("max_bundle_timeout");        }        str=props.getProperty("enable_bundling");        if(str != null) {            enable_bundling=Boolean.valueOf(str).booleanValue();            props.remove("enable_bundling");        }        str=props.getProperty("use_addr_translation");        if(str != null) {            use_addr_translation=Boolean.valueOf(str).booleanValue();            props.remove("use_addr_translation");        }        str=props.getProperty("enable_diagnostics");        if(str != null) {            enable_diagnostics=Boolean.valueOf(str).booleanValue();            props.remove("enable_diagnostics");        }        str=props.getProperty("diagnostics_addr");        if(str != null) {            diagnostics_addr=str;            props.remove("diagnostics_addr");        }        str=props.getProperty("diagnostics_port");        if(str != null) {            diagnostics_port=Integer.parseInt(str);            props.remove("diagnostics_port");        }        if(enable_bundling) {            //if (use_outgoing_packet_handler == false)              //  if(warn) log.warn("enable_bundling is true; setting use_outgoing_packet_handler=true");            // use_outgoing_packet_handler=true;        }        return true;    }    /**     * This prevents the up-handler thread to be created, which essentially is superfluous:     * messages are received from the network rather than from a layer below.     * DON'T REMOVE !     */    public void startUpHandler() {    }    /**     * handle the UP event.     * @param evt - the event being send from the stack     */    public void up(Event evt) {        switch(evt.getType()) {        case Event.CONFIG:            passUp(evt);            if(log.isDebugEnabled()) log.debug("received CONFIG event: " + evt.getArg());            handleConfigEvent((HashMap)evt.getArg());            return;        }        passUp(evt);    }    /**     * Caller by the layer above this layer. Usually we just put this Message     * into the send queue and let one or more worker threads handle it. A worker thread     * then removes the Message from the send queue, performs a conversion and adds the     * modified Message to the send queue of the layer below it, by calling down()).     */    public void down(Event evt) {        if(evt.getType() != Event.MSG) {  // unless it is a message handle it and respond            handleDownEvent(evt);            return;        }        Message msg=(Message)evt.getArg();        if(header != null) {            // added patch by Roland Kurmann (March 20 2003)            // msg.putHeader(name, new TpHeader(channel_name));            msg.putHeader(name, header);        }        // Because we don't call Protocol.passDown(), we notify the observer directly (e.g. PerfObserver).        // This way, we still have performance numbers for TP        if(observer != null)            observer.passDown(evt);        setSourceAddress(msg); // very important !! listToBuffer() will fail with a null src address !!        if(trace) {            StringBuffer sb=new StringBuffer("sending msg to ").append(msg.getDest()).                    append(" (src=").append(msg.getSrc()).append("), headers are ").append(msg.getHeaders());            log.trace(sb.toString());        }        // Don't send if destination is local address. Instead, switch dst and src and put in up_queue.        // If multicast message, loopback a copy directly to us (but still multicast). Once we receive this,        // we will discard our own multicast message        Address dest=msg.getDest();        boolean multicast=dest == null || dest.isMulticastAddress();        if(loopback && (multicast || dest.equals(local_addr))) {            Message copy=msg.copy();            // copy.removeHeader(name); // we don't remove the header            copy.setSrc(local_addr);            // copy.setDest(dest);            if(trace) log.trace(new StringBuffer("looping back message ").append(copy));            try {                incoming_msg_queue.add(copy);            }            catch(QueueClosedException e) {                // log.error("failed adding looped back message to incoming_msg_queue", e);            }            if(!multicast)                return;        }        try {            if(use_outgoing_packet_handler)                outgoing_queue.put(msg);            else                send(msg, dest, multicast);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?