Back to project page Android-NetPowerctrl-Shared.
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.
package oly.netpowerctrl.device_base.device; //from www . j a va 2 s .c o m import android.support.annotation.NonNull; import android.util.JsonReader; import android.util.JsonWriter; import java.io.IOException; /** * A device connection for udp */ public class DeviceConnectionUDP extends DeviceConnection { public static final String ID = "UDP"; // Device public int PortUDPSend = -1; public int PortUDPReceive = -1; public DeviceConnectionUDP(@NonNull Device device) { super(device); } public DeviceConnectionUDP(@NonNull Device device, String hostName, int PortUDPReceive, int PortUDPSend) { super(device); this.mHostName = hostName; this.PortUDPReceive = PortUDPReceive; this.PortUDPSend = PortUDPSend; } public void toJSON(JsonWriter writer) throws IOException { writer.beginObject(); writer.name("connection_type").value(ID); writer.name("PortUDPSend").value(PortUDPSend); writer.name("PortUDPReceive").value(PortUDPReceive); 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 "PortUDPSend": PortUDPSend = reader.nextInt(); ++members; break; case "PortUDPReceive": PortUDPReceive = reader.nextInt(); ++members; break; default: reader.skipValue(); break; } } reader.endObject(); //DEPRECATED if (members == 4) { mIsAssignedByDevice = mHostName.startsWith("192.") || mHostName.startsWith("10."); } return members >= 5; } public int getListenPort() { return PortUDPReceive; } @Override public int getDestinationPort() { return PortUDPSend; } @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(PortUDPReceive); builder.append(PortUDPSend); builder.append(mHostName); return builder.toString().hashCode(); } }