// $Id: LOOPBACK.java,v 1.31 2009/09/06 13:51:07 belaban Exp $

package org.jgroups.protocols;


import org.jgroups.Address;
import org.jgroups.Event;
import org.jgroups.Message;
import org.jgroups.PhysicalAddress;
import org.jgroups.stack.IpAddress;
import org.jgroups.util.Util;


/**
 Makes copies of outgoing messages, swaps sender and receiver and sends the message back up the stack.
 */
public class LOOPBACK extends TP {
    private String group_addr=null;
    private final PhysicalAddress physical_addr=new IpAddress(12345);
    private Address local_addr=null;

    public LOOPBACK() {
    }


    public String toString() {
        return "LOOPBACK(local address: " + local_addr + ')';
    }

    public void sendMulticast(byte[] data, int offset, int length) throws Exception {
    }

    public void sendUnicast(PhysicalAddress dest, byte[] data, int offset, int length) throws Exception {
    }

    public String getInfo() {
        return null;
    }

    protected PhysicalAddress getPhysicalAddress() {
        return physical_addr;
    }

    /*------------------------------ Protocol interface ------------------------------ */

    public void destroy() {
        try {
            timer.stop();
        }
        catch(InterruptedException e) {
            e.printStackTrace();
        }
    }



    /**
     * 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 Object down(Event evt) {
        if(log.isTraceEnabled())
            log.trace("event is " + evt + ", group_addr=" + group_addr +
                      ", time is " + System.currentTimeMillis() + ", hdrs: " + Util.printEvent(evt));

        switch(evt.getType()) {

        case Event.MSG:
            Message msg=(Message)evt.getArg();
            Message rsp=msg.copy();
            if(rsp.getSrc() == null)
                rsp.setSrc(local_addr);

            //dest_addr=msg.getDest();
            //rsp.setDest(local_addr);
            //rsp.setSrc(dest_addr != null ? dest_addr : local_addr);
            up(new Event(Event.MSG, rsp));
            break;

        case Event.SET_LOCAL_ADDRESS:
            local_addr=(Address)evt.getArg();
            break;

        case Event.CONNECT:
        case Event.CONNECT_WITH_STATE_TRANSFER:    
        case Event.CONNECT_USE_FLUSH:
        case Event.CONNECT_WITH_STATE_TRANSFER_USE_FLUSH: 	
            group_addr=(String)evt.getArg();
            break;
        }
        return null;
    }



    /*--------------------------- End of Protocol interface -------------------------- */


}
@@ -20,17 +20,17 @@
 va,v 1.3
-1
+2
  2009/09
@@ -34,27 +34,32 @@
 /09/
-06 13:51:07 belaban
+10 21:28:46 rachmatowicz
  Exp
@@ -95,16 +95,123 @@
 cols;%0A%0A%0A
+import java.net.InetAddress;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Properties;%0A%0A
 import o
@@ -1255,16 +1255,781 @@
 --- */%0A%0A
+    public void init() throws Exception %7B%0A        super.init();%0A        %0A        // the bind address determination moved from TP%0A        Properties props = new Properties() ;%0A        if (bind_addr_str != null)%0A        %09props.put(%22bind_addr%22, bind_addr_str) ;%0A        if (bind_interface_str != null)%0A        props.put(%22bind_interface%22, bind_interface_str) ;%0A        bind_addr = Util.getBindAddress(props) ;%0A%0A        // the diagnostics determination moved from TP%0A        diagnostics_addr_str = DEFAULT_IPV4_DIAGNOSTICS_ADDR_STR ;        %0A                %0A        if(bind_addr != null) %7B%0A            Map%3CString, Object%3E m=new HashMap%3CString, Object%3E(1);%0A            m.put(%22bind_addr%22, bind_addr);%0A            up(new Event(Event.CONFIG, m));%0A        %7D%0A%0A    %7D    %0A    %0A
     publ

Move processing of bind_addr, bind_interface and diagnostics_addr in LOOPBACK to init(). (JGRP-978)