0

このロジックでコードをカスタマイズする必要がある場合

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

双方向フローを検討するため、送信元から宛先へのパケットと宛先から送信元へのパケットはフローに属します。

コードを変更するにはどうすればよいですか?

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

}
4

2 に答える 2

0

単なる推測ですが、あなたが探しているのは、次の行に沿ったものだと思います(nullチェック、型チェック、およびエラー処理は、ユーザーへの演習として残されています):

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

これは、マシン 1 のポート b からマシン 2 のポート a への接続は、マシン 1 のポート a からマシン 2 のポート b への接続と同じではないという前提に基づいていることに注意してください。

于 2010-08-13T18:54:51.077 に答える
0

compareTo メソッドを大幅に簡素化できます。文字列を比較しているだけで、すべての文字列を連結して 1 つの比較にすると、同じ結果が得られます。次の例では、おまけとして toString() 実装を追加しています。

@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());
}

より高いパフォーマンスが必要な場合は、フローを構築してプライベート フィールドに格納するときに、連結された文字列を構築することを検討してください。

于 2010-08-13T19:02:31.927 に答える