NetlinkTrackerpublic class NetlinkTracker extends BaseNetworkObserver Keeps track of link configuration received from Netlink.
Instances of this class are expected to be owned by subsystems such as Wi-Fi
or Ethernet that manage one or more network interfaces. Each interface to be
tracked needs its own {@code NetlinkTracker}.
An instance of this class is constructed by passing in an interface name and
a callback. The owner is then responsible for registering the tracker with
NetworkManagementService. When the class receives update notifications from
the NetworkManagementService notification threads, it applies the update to
its local LinkProperties, and if something has changed, notifies its owner of
the update via the callback.
The owner can then call {@code getLinkProperties()} in order to find out
what changed. If in the meantime the LinkProperties stored here have changed,
this class will return the current LinkProperties. Because each change
triggers an update callback after the change is made, the owner may get more
callbacks than strictly necessary (some of which may be no-ops), but will not
be out of sync once all callbacks have been processed.
Threading model:
- The owner of this class is expected to create it, register it, and call
getLinkProperties or clearLinkProperties on its thread.
- Most of the methods in the class are inherited from BaseNetworkObserver
and are called by NetworkManagementService notification threads.
- All accesses to mLinkProperties must be synchronized(this). All the other
member variables are immutable once the object is constructed.
This class currently tracks IPv4 and IPv6 addresses. In the future it will
track routes and DNS servers. |
Fields Summary |
---|
private final String | TAG | private final String | mInterfaceName | private final Callback | mCallback | private final android.net.LinkProperties | mLinkProperties | private DnsServerRepository | mDnsServerRepository | private static final boolean | DBG |
Constructors Summary |
---|
public NetlinkTracker(String iface, Callback callback)
TAG = "NetlinkTracker/" + iface;
mInterfaceName = iface;
mCallback = callback;
mLinkProperties = new LinkProperties();
mLinkProperties.setInterfaceName(mInterfaceName);
mDnsServerRepository = new DnsServerRepository();
|
Methods Summary |
---|
public void | addressRemoved(java.lang.String iface, android.net.LinkAddress address)
if (mInterfaceName.equals(iface)) {
maybeLog("addressRemoved", iface, address);
boolean changed;
synchronized (this) {
changed = mLinkProperties.removeLinkAddress(address);
}
if (changed) {
mCallback.update();
}
}
| public void | addressUpdated(java.lang.String iface, android.net.LinkAddress address)
if (mInterfaceName.equals(iface)) {
maybeLog("addressUpdated", iface, address);
boolean changed;
synchronized (this) {
changed = mLinkProperties.addLinkAddress(address);
}
if (changed) {
mCallback.update();
}
}
| public synchronized void | clearLinkProperties()
// Clear the repository before clearing mLinkProperties. That way, if a clear() happens
// while interfaceDnsServerInfo() is being called, we'll end up with no DNS servers in
// mLinkProperties, as desired.
mDnsServerRepository = new DnsServerRepository();
mLinkProperties.clear();
mLinkProperties.setInterfaceName(mInterfaceName);
| public synchronized android.net.LinkProperties | getLinkProperties()Returns a copy of this object's LinkProperties.
return new LinkProperties(mLinkProperties);
| public void | interfaceDnsServerInfo(java.lang.String iface, long lifetime, java.lang.String[] addresses)
if (mInterfaceName.equals(iface)) {
maybeLog("interfaceDnsServerInfo", Arrays.toString(addresses));
boolean changed = mDnsServerRepository.addServers(lifetime, addresses);
if (changed) {
synchronized (this) {
mDnsServerRepository.setDnsServersOn(mLinkProperties);
}
mCallback.update();
}
}
| private void | maybeLog(java.lang.String operation, java.lang.String iface, android.net.LinkAddress address)
if (DBG) {
Log.d(TAG, operation + ": " + address + " on " + iface +
" flags " + address.getFlags() + " scope " + address.getScope());
}
| private void | maybeLog(java.lang.String operation, java.lang.Object o)
if (DBG) {
Log.d(TAG, operation + ": " + o.toString());
}
| public void | routeRemoved(android.net.RouteInfo route)
if (mInterfaceName.equals(route.getInterface())) {
maybeLog("routeRemoved", route);
boolean changed;
synchronized (this) {
changed = mLinkProperties.removeRoute(route);
}
if (changed) {
mCallback.update();
}
}
| public void | routeUpdated(android.net.RouteInfo route)
if (mInterfaceName.equals(route.getInterface())) {
maybeLog("routeUpdated", route);
boolean changed;
synchronized (this) {
changed = mLinkProperties.addRoute(route);
}
if (changed) {
mCallback.update();
}
}
|
|