views:

40

answers:

2

if I need to customize my code with this logic

if this.srcAddr=other.srcAddr or
this.src.Addr = other.sdstAddr
this.srcPort=other.srcPort
this.srcPort=other.dstPort

because I am going to consider bi-directional flow, a packet from source to destination and a packet from destination to source belong to a flow.

How should I change my code?

package myclassifier;
public class Flows implements Comparable<Flows> {

    String srcAddr, dstAddr, srcPort, dstPort, protocol;

    public Flows(String sIP, String dIP){
        this.srcAddr = sIP;
        this.dstAddr = dIP;
    }

    public int compareTo(Flows other) {
            int res = (this.srcAddr.compareTo(other.srcAddr));
            if (res != 0) {
                return res;
            }
            res = this.dstAddr.compareTo(other.dstAddr);
            if (res != 0) {
                return res;
            }
            res = this.srcPort.compareTo(other.srcPort);
            if (res != 0) {
                return res;
            }
            res = this.dstPort.compareTo(other.dstPort);
            if (res != 0) {
                return res;
            }
            return this.protocol.compareTo(other.protocol);
    }

    @Override
    public int hashCode() {

        final int prime = 31;
        int result = 1;
        result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
        result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
        result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
        result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
        return result;

    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;

        if (getClass() != obj.getClass())
            return false;

        Flows other = (Flows) obj;

        if (dstAddr == null) {
            if (other.dstAddr != null)
                return false;
        } else if (!dstAddr.equals(other.dstAddr))
            return false;

        if (dstPort == null) {
            if (other.dstPort != null)
                return false;
        } else if (!dstPort.equals(other.dstPort))
            return false;

        if (srcAddr == null) {
            if (other.srcAddr != null)
                return false;
        } else if (!srcAddr.equals(other.srcAddr))
            return false;

        if (srcPort == null) {
            if (other.srcPort != null)
                return false;
        } else if (!srcPort.equals(other.srcPort))
            return false;

        return true;
    }

}
A: 

Just a guess, but I think what you're looking for is something along the lines of the following (null checking, type checking and error handling left as an exercise to the user):

return ((this.srcAddr.equals(other.srcAddr) && this.srcPort.equals(other.srcPort) || 
        (this.srcAddr.equals(other.dstAddr) && this.srcPort.equals(other.dstPort));

Note that this is based on the assumption that a connection from machine 1 port b to machine 2 port a is not the same as a connection from machine 1 port a to machine 2 port b.

Angelo Genovese
exactly is the one i m thinking of.
Red Lion
Just return the expression you used in your `if` instead of having these superfluous return statements.
Willi
updated, sorry I was half asleep when I wrote that originally.
Angelo Genovese
A: 

You can simplify the compareTo Method dramatically. You're just comparing Strings and you'll have the same result if you just concatenate all String and to one single compare. The following example adds a toString() implementation as a bonus:

@Override
public String toString() {
  return String.format("[%s, %s, %s, %s, %s]", srcAddr, dstAddr, srcPort, dstPort, protocol);
}

public int compareTo(Flows other) {
  if (other == null)
    return 0;   // the necessary null check was missing in your code

  return toString().compareTo(other.toString());
}

If you need more performance, consider constructing the concatenated String while you construct a flow and store it in a private field.

Andreas_D
sorry i do not totally get it. how does it work? if i give it following two streams srcAddr=x,dstAddr=y,srcPort=12345,dstPort=80,pro=tcp AND srcAddr=y,dstAddr=x,srcPort=80,dstPort=12345,pro=tcpdoes it assume they are the same?
Red Lion
No it wouldn't.
Willi
@Red Lion - sorry for the confusion, my text is not an answer to your 'vice-versa' problem, it just simplifies your current code. But your last example clarified what you want - you want a test that considers both flows (from your example) as equal.
Andreas_D