2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connector.remoterpc;
10 import org.zeromq.ZMQ;
12 import java.net.Inet4Address;
13 import java.net.InetAddress;
14 import java.net.NetworkInterface;
15 import java.net.SocketException;
16 import java.util.Enumeration;
19 * Provides a ZeroMQ Context object
21 public class Context {
22 private ZMQ.Context zmqContext = ZMQ.context(1);
24 private final String DEFAULT_RPC_PORT = "5554";
26 private static Context _instance = new Context();
30 public static Context getInstance(){
34 public ZMQ.Context getZmqContext(){
35 return this.zmqContext;
38 public String getLocalUri(){
39 uri = (uri != null) ? uri
40 : new StringBuilder().append(getIpAddress()).append(":")
41 .append(getRpcPort()).toString();
46 public String getRpcPort(){
47 String rpcPort = (System.getProperty("rpc.port") != null)
48 ? System.getProperty("rpc.port")
54 private String getIpAddress(){
55 String ipAddress = (System.getProperty("local.ip") != null)
56 ? System.getProperty("local.ip")
63 * Finds IPv4 address of the local VM
64 * TODO: This method is non-deterministic. There may be more than one IPv4 address. Cant say which
65 * address will be returned. Read IP from a property file or enhance the code to make it deterministic.
66 * Should we use IP or hostname?
70 private String findIpAddress() {
71 String hostAddress = null;
74 e = NetworkInterface.getNetworkInterfaces();
75 } catch (SocketException e1) {
78 while (e.hasMoreElements()) {
80 NetworkInterface n = (NetworkInterface) e.nextElement();
82 Enumeration ee = n.getInetAddresses();
83 while (ee.hasMoreElements()) {
84 InetAddress i = (InetAddress) ee.nextElement();
85 if ((i instanceof Inet4Address) && (i.isSiteLocalAddress()))
86 hostAddress = i.getHostAddress();