001: // $Id: LOSS.java,v 1.3 2004/09/23 16:29:41 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Event;
006: import org.jgroups.Message;
007: import org.jgroups.View;
008: import org.jgroups.stack.Protocol;
009: import org.jgroups.util.Util;
010:
011: import java.util.Vector;
012:
013: /**
014: * Example of a protocol layer. Contains no real functionality, can be used as a template.
015: */
016:
017: public class LOSS extends Protocol {
018: final Vector members = new Vector();
019: final long i = 0;
020: boolean drop_next_msg = false;
021:
022: /** All protocol names have to be unique ! */
023: public String getName() {
024: return "LOSS";
025: }
026:
027: /** Just remove if you don't need to reset any state */
028: public void reset() {
029: }
030:
031: // public void up(Event evt) {
032: // Message msg;
033:
034: // switch(evt.getType()) {
035:
036: // case Event.MSG:
037: // msg=(Message)evt.getArg();
038: // if(msg.getDest() != null && !((Address)msg.getDest()).isMulticastAddress()) {
039: // // System.err.println("LOSS.up(): not dropping msg as it is unicast !");
040: // break;
041: // }
042:
043: // i++;
044:
045: // int r=((int)(Math.random() * 1000)) % 10;
046:
047: // if(r != 0 && i % r == 0) { // drop
048: // System.out.println("####### LOSS.up(): dropping message " +
049: // Util.printEvent(evt));
050: // return;
051: // }
052:
053: // break;
054: // }
055:
056: // passUp(evt); // Pass up to the layer above us
057: // }
058:
059: public void down(Event evt) {
060: Message msg;
061:
062: switch (evt.getType()) {
063:
064: case Event.TMP_VIEW:
065: case Event.VIEW_CHANGE:
066: Vector new_members = ((View) evt.getArg()).getMembers();
067: synchronized (members) {
068: members.removeAllElements();
069: if (new_members != null && new_members.size() > 0)
070: for (int i = 0; i < new_members.size(); i++)
071: members.addElement(new_members.elementAt(i));
072: }
073: passDown(evt);
074: break;
075:
076: case Event.MSG:
077: if (drop_next_msg) {
078: drop_next_msg = false;
079: msg = (Message) evt.getArg();
080:
081: if (msg.getDest() != null
082: && !msg.getDest().isMulticastAddress()) {
083: break;
084: }
085:
086: System.out.println("###### LOSS.down(): dropping msg "
087: + Util.printMessage(msg));
088:
089: return;
090: }
091: break;
092:
093: case Event.DROP_NEXT_MSG:
094: drop_next_msg = true;
095: break;
096: }
097:
098: passDown(evt); // Pass on to the layer below us
099: }
100:
101: }
|