4 * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
31 * @author Eddie Berrisford-Lynch <dev@fun2be.me>
33 public class Network {
34 public static final int PORTS_SERVER_SOCIAL = 50000;
35 public static final int PORTS_SERVER_CHAT = 50001;
36 public static final int PORTS_SERVER_LOG = 50002;
37 public static final int PORTS_MULTICAST_DISCOVERY = 50003;
38 public static final int PORTS_CLIENT_CONTROL_FIRST = 50010;
39 public static final int PORTS_CLIENT_CHAT_FIRST = 51000;
40 public static final int PORTS_EPHEMERAL = 0;
43 * Special IPv4 'wildcard' address asking java.net.ServerSocket#bind to bind to all interfaces.
45 public static final InetAddress IPv4_WILDCARD;
47 * Multicast IPv4 address the applications use to discover each other.
49 public static final InetAddress MULTICAST_IP;
51 * Unicast IPv4 address for testing network connectivity.
53 public static final InetAddress PING_IP;
56 * Initialisation of class-static final constants.
58 * Required in order to initialise static final constant values.
59 * Only runs once when the class is created, not when object instances are constructed.
63 /* temporary used to avoid the compiler error:
65 * variable IPv4_WILDCARD might already have been assigned
67 * that will be caused if the value was directly assigned to the
68 * 'static final' variable in both the try and catch clauses.
71 temp = InetAddress.getByAddress(new byte[] {0,0,0,0});
72 } catch (UnknownHostException ex) {
73 temp = null; //FIXME: variable IPv4_WILDCARD might already have been assigned
78 temp = InetAddress.getByAddress(new byte[] {(byte)239, (byte)192, (byte)3, (byte)4});
79 } catch (UnknownHostException ex) {
80 temp = null; // FIXME: variable MULTICAST_IP might already have been assigned
84 temp = InetAddress.getByAddress(new byte[] {(byte)176, (byte)58, (byte)127, (byte)210});
85 } catch (UnknownHostException ex) {
86 temp = null; // FIXME: variable PING_IP might already have been assigned
91 public static final InetAddress getDefaultRoutedInetAddress() {
92 InetAddress result = null;
94 WSYD_SocketAddress sa = new WSYD_SocketAddress(PING_IP);
95 // TODO: getDefaultRoutedInetAddress() - implement route detection logic
96 } catch(Exception e) {
97 // TODO: getDefaultRoutedInetAddress() handle all Exceptions