开发者

how to customize compareTo method to consider both direction flow

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;
    }

}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜