Android Open Source - Android-NetPowerctrl-Shared Device Connection H T T P






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;
//from w  w  w . j  a  v a  2  s.c om
import android.support.annotation.NonNull;
import android.util.JsonReader;
import android.util.JsonWriter;

import java.io.IOException;

/**
 * A device connection for http
 */
public class DeviceConnectionHTTP extends DeviceConnection {
    public static final String ID = "HTTP";
    // Device
    public int PortHttp = -1;

    public DeviceConnectionHTTP(Device device) {
        super(device);
    }

    /**
     * Create a new http connection with hostname and port that does not use default ports.
     *
     * @param device   The device
     * @param hostName The hostname or IP
     * @param httpPort The http port
     */
    public DeviceConnectionHTTP(Device device, String hostName, int httpPort) {
        super(device);
        this.mHostName = hostName;
        this.PortHttp = httpPort;
    }

    public void toJSON(JsonWriter writer) throws IOException {
        writer.beginObject();
        writer.name("connection_type").value(ID);
        writer.name("HttpPort").value(PortHttp);
        writer.name("HostName").value(mHostName);
        writer.name("AllowHostnameUpdates").value(mIsAssignedByDevice);
        writer.endObject();
    }

    @Override
    public boolean fromJSON(@NonNull JsonReader reader, boolean beginObjectAlreadyCalled) throws IOException, ClassNotFoundException {
        if (!beginObjectAlreadyCalled)
            reader.beginObject();

        int members = 0;
        while (reader.hasNext()) {
            String name = reader.nextName();
            assert name != null;
            switch (name) {
                case "HostName":
                    mHostName = reader.nextString();
                    ++members;
                    break;
                case "AllowHostnameUpdates":
                    mIsAssignedByDevice = reader.nextBoolean();
                    ++members;
                    break;
                case "HttpPort":
                    PortHttp = reader.nextInt();
                    ++members;
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }

        reader.endObject();

        return members >= 3;
    }

    @Override
    public int getDestinationPort() {
        return PortHttp;
    }

    @Override
    public String getProtocol() {
        return ID;
    }

    @Override
    public boolean equalsByDestinationAddress(DeviceConnection otherConnection, boolean lookupDNSName) {
        return cached_addresses == null ? mHostName.equals(otherConnection.mHostName) : hasAddress(otherConnection.getHostnameIPs(lookupDNSName), lookupDNSName);
    }

    @Override
    public int computeHash() {
        StringBuilder builder = new StringBuilder();
        builder.append(ID);
        builder.append(PortHttp);
        builder.append(mHostName);
        return builder.toString().hashCode();
    }
}




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