]> git.basschouten.com Git - openhab-addons.git/blob
1b272ac317656691bc1ccadbe307e19f03489e80
[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             final Set<PcapNetworkInterfaceWrapper> allNetworkInterfaces = Collections.unmodifiableSet(Pcaps
40                     .findAllDevs().stream().map(PcapNetworkInterfaceWrapper.TRANSFORMER).collect(Collectors.toSet()));
41             return allNetworkInterfaces;
42         } catch (PcapNativeException e) {
43             throw new RuntimeException(e);
44         }
45     }
46
47     /**
48      * Returns the Pcap network interface with the given name relying on {@link Pcaps#getDevByName(String)}. If no
49      * interface is found, null is returned.
50      *
51      * @param name The name of the Pcap network interface
52      * @return The network interface with the given name. Returns null, if no interface is found
53      */
54     public static PcapNetworkInterfaceWrapper getNetworkInterfaceByName(String name) {
55         try {
56             return PcapNetworkInterfaceWrapper.TRANSFORMER.apply(Pcaps.getDevByName(name));
57         } catch (PcapNativeException e) {
58             throw new RuntimeException(e);
59         }
60     }
61 }