1 3 package org.jgroups.protocols; 4 5 6 import org.jgroups.Address; 7 import org.jgroups.Event; 8 import org.jgroups.Message; 9 import org.jgroups.View; 10 import org.jgroups.stack.IpAddress; 11 import org.jgroups.stack.Protocol; 12 import org.jgroups.util.Util; 13 14 import java.util.Vector ; 15 16 17 20 public class LOOPBACK extends Protocol { 21 private Address local_addr=null; 22 private String group_addr=null; 23 private final Vector members=new Vector (); 24 25 26 public LOOPBACK() { 27 ; 28 } 29 30 31 public String toString() { 32 return "Protocol LOOPBACK(local address: " + local_addr + ')'; 33 } 34 35 36 37 38 39 40 public String getName() { 41 return "LOOPBACK"; 42 } 43 44 45 46 public void init() throws Exception { 47 53 local_addr = new IpAddress(12345); 55 } 56 57 public void start() throws Exception { 58 passUp(new Event(Event.SET_LOCAL_ADDRESS, local_addr)); 59 } 60 61 62 68 public void down(Event evt) { 69 Message msg, rsp; 70 Address dest_addr; 71 72 if(log.isTraceEnabled()) 73 log.trace("event is " + evt + ", group_addr=" + group_addr + 74 ", time is " + System.currentTimeMillis() + ", hdrs: " + Util.printEvent(evt)); 75 76 switch(evt.getType()) { 77 78 case Event.MSG: 79 msg=(Message)evt.getArg(); 80 if(msg.getSrc() == null) 81 msg.setSrc(local_addr); 82 rsp=msg.copy(); 83 up(new Event(Event.MSG, rsp)); 87 break; 88 89 case Event.TMP_VIEW: 90 case Event.VIEW_CHANGE: 91 synchronized(members) { 92 members.removeAllElements(); 93 Vector tmpvec=((View)evt.getArg()).getMembers(); 94 for(int i=0; i < tmpvec.size(); i++) 95 members.addElement(tmpvec.elementAt(i)); 96 } 97 break; 98 99 case Event.GET_LOCAL_ADDRESS: passUp(new Event(Event.SET_LOCAL_ADDRESS, local_addr)); 101 break; 102 103 case Event.CONNECT: 104 group_addr=(String )evt.getArg(); 105 passUp(new Event(Event.CONNECT_OK)); 106 break; 107 108 case Event.DISCONNECT: 109 passUp(new Event(Event.DISCONNECT_OK)); 110 break; 111 112 case Event.PERF: 113 passUp(evt); 114 break; 115 } 116 } 117 118 119 120 121 122 123 } 124 | Popular Tags |