Android Open Source - Android-NetPowerctrl-Shared Device Connection






From Project

Back to project page Android-NetPowerctrl-Shared.

License

The source code is released under:

Copyright (c) 2014, David Gr?ff All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project Android-NetPowerctrl-Shared listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package oly.netpowerctrl.device_base.device;
/*ww w  . ja v a  2  s.co m*/
import android.support.annotation.NonNull;
import android.util.JsonReader;
import android.util.JsonWriter;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import oly.netpowerctrl.device_base.data.JSONHelper;
import oly.netpowerctrl.device_base.executables.ExecutableReachability;

/**
 * Created by david on 28.07.14.
 */
public abstract class DeviceConnection {
    public final Device device;
    public String mHostName;
    public String not_reachable_reason;
    protected boolean mIsAssignedByDevice = false;
    // Cache
    protected InetAddress[] cached_addresses;
    protected int receivedPackets = 0;

    public DeviceConnection(Device device) {
        this.device = device;
    }

    public void setReceiveAddress(@NonNull InetAddress inetAddress) {
        cached_addresses = new InetAddress[1];
        cached_addresses[0] = inetAddress;
    }

    public String getDestinationHost() {
        return mHostName;
    }

    public Device getDevice() {
        return device;
    }

    void incReceivedPackets() {
        ++receivedPackets;
    }

    public ExecutableReachability reachableState() {
        if (receivedPackets == 0)
            return ExecutableReachability.MaybeReachable;
        else if (receivedPackets > 0)
            return ExecutableReachability.Reachable;
        else
            return ExecutableReachability.NotReachable;
    }

    public String getNotReachableReason() {
        return not_reachable_reason;
    }

    void setStatusMessage(@NonNull String not_reachable_reason, boolean clearReachability) {
        this.not_reachable_reason = not_reachable_reason;
        if (clearReachability)
            receivedPackets = -1;
    }

    public void clearStatusMessage() {
        not_reachable_reason = null;
    }

    public String getString() {
        return getProtocol() + "/" + mHostName + ":" + String.valueOf(getDestinationPort());
    }

    public boolean needResolveName() {
        return cached_addresses == null;
    }

    public void lookupIPs() throws UnknownHostException {
        receivedPackets = -1;
        // do the lookup (exception may occur)
        cached_addresses = InetAddress.getAllByName(mHostName);
    }

    // This has to be executed in another thread not the gui thread if lookupDNSName is set.
    public InetAddress[] getHostnameIPs(boolean lookupDNSName) {
        if (cached_addresses == null || cached_addresses.length == 0) {
            if (lookupDNSName)
                try {
                    cached_addresses = InetAddress.getAllByName(mHostName);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                    cached_addresses = new InetAddress[0];
                }
            else
                cached_addresses = new InetAddress[0];
        }
        return cached_addresses;
    }

    // This has to be executed in another thread not the gui thread if lookupDNSName is set.
    public boolean hasAddress(InetAddress[] addresses, boolean lookupDNSName) {
        getHostnameIPs(lookupDNSName);
        if (cached_addresses != null) {
            for (InetAddress local_address : cached_addresses)
                for (InetAddress other_address : addresses)
                    if (local_address.equals(other_address))
                        return true;
        }
        return false;
    }

    public boolean isAssignedByDevice() {
        return mIsAssignedByDevice;
    }

    /**
     * Make this connection a connection assigned by the remote device.
     * The connection is automatically flagged as reachable.
     */
    public void makeAssignedByDevice() {
        this.mIsAssignedByDevice = true;
        ++receivedPackets;
    }

    public abstract void toJSON(JsonWriter writer) throws IOException;

    /**
     * Return the json representation of this connection
     *
     * @return JSON String
     */
    @Override
    public String toString() {
        try {
            JSONHelper h = new JSONHelper();
            toJSON(h.createWriter());
            return h.getString();
        } catch (IOException ignored) {
            return null;
        }
    }

    public abstract boolean fromJSON(@NonNull JsonReader reader, boolean beginObjectAlreadyCalled) throws IOException, ClassNotFoundException;

    public abstract int getDestinationPort();

    public abstract String getProtocol();

    public abstract boolean equalsByDestinationAddress(DeviceConnection otherConnection, boolean lookupDNSName);

    public abstract int computeHash();
}




Java Source Code List

oly.netpowerctrl.device_base.ApplicationTest.java
oly.netpowerctrl.device_base.data.JSONHelper.java
oly.netpowerctrl.device_base.data.StorableInterface.java
oly.netpowerctrl.device_base.device.DeviceConnectionAPI.java
oly.netpowerctrl.device_base.device.DeviceConnectionFabric.java
oly.netpowerctrl.device_base.device.DeviceConnectionHTTP.java
oly.netpowerctrl.device_base.device.DeviceConnectionUDP.java
oly.netpowerctrl.device_base.device.DeviceConnection.java
oly.netpowerctrl.device_base.device.DeviceFeatureFabric.java
oly.netpowerctrl.device_base.device.DeviceFeatureInterface.java
oly.netpowerctrl.device_base.device.DeviceFeatureTemperature.java
oly.netpowerctrl.device_base.device.DevicePort.java
oly.netpowerctrl.device_base.device.Device.java
oly.netpowerctrl.device_base.executables.ExecutableReachability.java
oly.netpowerctrl.device_base.executables.ExecutableType.java
oly.netpowerctrl.device_base.executables.Executable.java
oly.netpowerctrl.plugin.App2PluginCommunication.java
oly.netpowerctrl.plugin.App2PluginService.java
oly.netpowerctrl.plugin.DeviceConnectionStateInterface.java
oly.netpowerctrl.plugin.onDeviceStateChange.java
oly.netpowerctrl.plugin.onServiceStateChange.java