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);
25 private static Context _instance = new Context();
29 public static Context getInstance(){
33 public ZMQ.Context getZmqContext(){
34 return this.zmqContext;
37 public String getLocalUri(){
38 uri = (uri != null) ? uri
39 : new StringBuilder("tcp://").append(getIpAddress()).append(":")
40 .append(getRpcPort()).toString();
45 public String getRpcPort(){
46 String rpcPort = (System.getProperty("rpc.port") != null)
47 ? System.getProperty("rpc.port")
53 private String getIpAddress(){
54 String ipAddress = (System.getProperty("local.ip") != null)
55 ? System.getProperty("local.ip")
62 * Finds IPv4 address of the local VM
63 * TODO: This method is non-deterministic. There may be more than one IPv4 address. Cant say which
64 * address will be returned. Read IP from a property file or enhance the code to make it deterministic.
65 * Should we use IP or hostname?
69 private String findIpAddress() {
70 String hostAddress = null;
73 e = NetworkInterface.getNetworkInterfaces();
74 } catch (SocketException e1) {
77 while (e.hasMoreElements()) {
79 NetworkInterface n = (NetworkInterface) e.nextElement();
81 Enumeration ee = n.getInetAddresses();
82 while (ee.hasMoreElements()) {
83 InetAddress i = (InetAddress) ee.nextElement();
84 if ((i instanceof Inet4Address) && (i.isSiteLocalAddress()))
85 hostAddress = i.getHostAddress();