]> git.basschouten.com Git - openhab-addons.git/blob
d15fde52c555732eb5d4c86b9a30f402ad570564
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.amazondashbutton.internal.pcap;
14
15 import java.util.Collections;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18
19 import org.pcap4j.core.PcapNativeException;
20 import org.pcap4j.core.Pcaps;
21
22 /**
23  *
24  * A simple utitlity class which encapsulates {@link Pcaps} and which catches checked exceptions and transforms them
25  * into {@link RuntimeException}s.
26  *
27  * @author Oliver Libutzki - Initial contribution
28  *
29  */
30 public class PcapUtil {
31
32     /**
33      * Returns all Pcap network interfaces relying on {@link Pcaps#findAllDevs()}.
34      *
35      * @return A {@link Set} of all {@link PcapNetworkInterfaceWrapper}s
36      */
37     public static Set<PcapNetworkInterfaceWrapper> getAllNetworkInterfaces() {
38         try {
39             return Collections.unmodifiableSet(Pcaps.findAllDevs().stream().map(PcapNetworkInterfaceWrapper.TRANSFORMER)
40                     .collect(Collectors.toSet()));
41         } catch (PcapNativeException e) {
42             throw new RuntimeException(e);
43         }
44     }
45
46     /**
47      * Returns the Pcap network interface with the given name relying on {@link Pcaps#getDevByName(String)}. If no
48      * interface is found, null is returned.
49      *
50      * @param name The name of the Pcap network interface
51      * @return The network interface with the given name. Returns null, if no interface is found
52      */
53     public static PcapNetworkInterfaceWrapper getNetworkInterfaceByName(String name) {
54         try {
55             return PcapNetworkInterfaceWrapper.TRANSFORMER.apply(Pcaps.getDevByName(name));
56         } catch (PcapNativeException e) {
57             throw new RuntimeException(e);
58         }
59     }
60 }