pcap: Cleanup protocol enums
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core / src / org / eclipse / linuxtools / internal / pcap / core / protocol / PcapProtocol.java
index 4de6396fe9db9b5581c10ad5186928040a80780b..d810dbf12c5686b28d55983406cf41b75aa57616 100644 (file)
@@ -13,7 +13,7 @@
 package org.eclipse.linuxtools.internal.pcap.core.protocol;
 
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
 
 /**
@@ -28,7 +28,7 @@ public enum PcapProtocol {
      * The Pcap Protocol is not a real protocol but is used as an helper to
      * generate Pcap packets.
      */
-    PCAP("Packet Capture", "pcap", PcapProtocolValues.LAYER_0, false), //$NON-NLS-1$ //$NON-NLS-2$
+    PCAP("Packet Capture", "pcap", Layer.LAYER_0, false), //$NON-NLS-1$ //$NON-NLS-2$
 
     // Layer 1
     // Should always be empty.
@@ -37,23 +37,23 @@ public enum PcapProtocol {
     /**
      * The description of the Ethernet II Protocol.
      */
-    ETHERNET_II("Ethernet II", "eth", PcapProtocolValues.LAYER_2, true), //$NON-NLS-1$ //$NON-NLS-2$
+    ETHERNET_II("Ethernet II", "eth", Layer.LAYER_2, true), //$NON-NLS-1$ //$NON-NLS-2$
 
     // Layer 3
     /**
      * The description of the Internet Protocol Version 4.
      */
-    IPV4("Internet Protocol Version 4", "ipv4", PcapProtocolValues.LAYER_3, true), //$NON-NLS-1$ //$NON-NLS-2$
+    IPV4("Internet Protocol Version 4", "ipv4", Layer.LAYER_3, true), //$NON-NLS-1$ //$NON-NLS-2$
 
     // Layer 4
     /**
      * The description of the Transmission Control Protocol.
      */
-    TCP("Transmission Control Protocol", "tcp", PcapProtocolValues.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$
+    TCP("Transmission Control Protocol", "tcp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$
     /**
      * The description of the User Datagram Protocol.
      */
-    UDP("User Datagram Protocol", "udp", PcapProtocolValues.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$
+    UDP("User Datagram Protocol", "udp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$
 
     // Layer 5
 
@@ -66,15 +66,54 @@ public enum PcapProtocol {
      * a "payload packet". This is considered to be on layer 7 since its always
      * the most encapsulated packet if present.
      */
-    UNKNOWN("Payload", "???", PcapProtocolValues.LAYER_7, false); //$NON-NLS-1$ //$NON-NLS-2$
+    UNKNOWN("Payload", "???", Layer.LAYER_7, false); //$NON-NLS-1$ //$NON-NLS-2$
+
+
+    /**
+     * Enum that lists constants related to protocols/layers.
+     *
+     * See http://en.wikipedia.org/wiki/OSI_model#Description_of_OSI_layers.
+     *
+     * @author Vincent Perot
+     */
+    public static enum Layer {
+
+        /**
+         * Layer 0. This layer is not an OSI layer but is used as an helper to store
+         * the pseudo-protocol PCAP.
+         */
+        LAYER_0,
+
+        /** Layer 1 of the OSI model */
+        LAYER_1,
+
+        /** Layer 2 of the OSI model */
+        LAYER_2,
+
+        /** Layer 3 of the OSI model */
+        LAYER_3,
+
+        /** Layer 4 of the OSI model */
+        LAYER_4,
+
+        /** Layer 5 of the OSI model */
+        LAYER_5,
+
+        /** Layer 6 of the OSI model */
+        LAYER_6,
+
+        /** Layer 7 of the OSI model */
+        LAYER_7;
+    }
+
 
     // Fields
     private final String fName;
     private final String fShortName;
-    private final int fLayer;
+    private final Layer fLayer;
     private final boolean fSupportsStream;
 
-    private PcapProtocol(String name, String shortName, int layer, boolean supportsStream) {
+    private PcapProtocol(String name, String shortName, Layer layer, boolean supportsStream) {
         fName = name;
         fShortName = shortName;
         fLayer = layer;
@@ -104,7 +143,7 @@ public enum PcapProtocol {
      *
      * @return The layer of the protocol.
      */
-    public int getLayer() {
+    public Layer getLayer() {
         return fLayer;
     }
 
@@ -128,12 +167,7 @@ public enum PcapProtocol {
      *            The layer of the protocols.
      * @return The protocols on that layer.
      */
-    public static List<PcapProtocol> getProtocolsOnLayer(int layer) {
-
-        if (layer > PcapProtocolValues.LAYER_7 || layer < PcapProtocolValues.LAYER_0) {
-            throw new IllegalArgumentException("The layer is invalid."); //$NON-NLS-1$
-        }
-
+    public static Collection<PcapProtocol> getProtocolsOnLayer(Layer layer) {
         List<PcapProtocol> protocolsOnLayer = new ArrayList<>();
         for (PcapProtocol p : PcapProtocol.values()) {
             if (p.getLayer() == layer) {
@@ -142,13 +176,4 @@ public enum PcapProtocol {
         }
         return protocolsOnLayer;
     }
-
-    /**
-     * Method that returns all the protocol defined.
-     *
-     * @return A list containing all the protocols.
-     */
-    public static List<PcapProtocol> getAllProtocols() {
-        return new ArrayList<>(Arrays.asList(PcapProtocol.values()));
-    }
 }
This page took 0.026834 seconds and 5 git commands to generate.