added Snapclient & Snapserver json objects

This commit is contained in:
badaix 2016-03-08 08:38:51 +01:00
parent 5951c123c9
commit 78c9a1c2ad
8 changed files with 139 additions and 42 deletions

View file

@ -8,7 +8,7 @@ import org.json.JSONObject;
*/
public class Client implements JsonSerialisable {
private Host host;
private Snapcast snapclient;
private Snapclient snapclient;
private ClientConfig config;
private Time_t lastSeen;
private boolean connected;
@ -31,9 +31,9 @@ public class Client implements JsonSerialisable {
}
if (json.has("snapclient"))
snapclient = new Snapcast(json.getJSONObject("snapclient"));
snapclient = new Snapclient(json.getJSONObject("snapclient"));
else {
snapclient = new Snapcast();
snapclient = new Snapclient();
snapclient.version = json.getString("version");
}

View file

@ -8,7 +8,7 @@ import org.json.JSONObject;
*/
public class Server implements JsonSerialisable {
private Host host;
private Snapcast snapserver;
private Snapserver snapserver;
public Server(JSONObject json) {
fromJson(json);
@ -25,9 +25,9 @@ public class Server implements JsonSerialisable {
}
if (json.has("snapserver"))
snapserver = new Snapcast(json.getJSONObject("snapserver"));
snapserver = new Snapserver(json.getJSONObject("snapserver"));
else {
snapserver = new Snapcast();
snapserver = new Snapserver();
snapserver.version = json.getString("version");
}
} catch (JSONException e) {

View file

@ -9,8 +9,7 @@ import org.json.JSONObject;
public class Snapcast implements JsonSerialisable {
String name = "";
String version = "";
int streamProtocolVersion = 1;
int controlProtocolVersion = 1;
int protocolVersion = 1;
public Snapcast() {
}
@ -24,8 +23,7 @@ public class Snapcast implements JsonSerialisable {
try {
name = json.getString("name");
version = json.getString("version");
streamProtocolVersion = json.getInt("streamProtocolVersion");
controlProtocolVersion = json.getInt("controlProtocolVersion");
protocolVersion = json.getInt("protocolVersion");
} catch (JSONException e) {
e.printStackTrace();
}
@ -37,8 +35,7 @@ public class Snapcast implements JsonSerialisable {
try {
json.put("name", name);
json.put("version", version);
json.put("streamProtocolVersion", streamProtocolVersion);
json.put("controlProtocolVersion", controlProtocolVersion);
json.put("protocolVersion", protocolVersion);
} catch (JSONException e) {
e.printStackTrace();
}
@ -53,12 +50,8 @@ public class Snapcast implements JsonSerialisable {
return version;
}
public int getStreamProtocolVersion() {
return streamProtocolVersion;
}
public int getControlProtocolVersion() {
return controlProtocolVersion;
public int getProtocolVersion() {
return protocolVersion;
}
@Override
@ -75,16 +68,14 @@ public class Snapcast implements JsonSerialisable {
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (version != null ? !version.equals(that.version) : that.version != null) return false;
if (streamProtocolVersion != that.streamProtocolVersion) return false;
return (controlProtocolVersion == that.controlProtocolVersion);
return (protocolVersion == that.protocolVersion);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (version != null ? version.hashCode() : 0);
result = 31 * result + streamProtocolVersion;
result = 31 * result + controlProtocolVersion;
result = 31 * result + protocolVersion;
return result;
}
}

View file

@ -0,0 +1,18 @@
package de.badaix.snapcast.control.json;
import org.json.JSONObject;
/**
* Created by johannes on 06.01.16.
*/
public class Snapclient extends Snapcast {
public Snapclient() {
super();
}
public Snapclient(JSONObject json) {
super(json);
}
}

View file

@ -0,0 +1,63 @@
package de.badaix.snapcast.control.json;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by johannes on 06.01.16.
*/
public class Snapserver extends Snapcast {
int controlProtocolVersion = 1;
public Snapserver() {
super();
}
public Snapserver(JSONObject json) {
super(json);
}
@Override
public void fromJson(JSONObject json) {
try {
super.fromJson(json);
controlProtocolVersion = json.getInt("controlProtocolVersion");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public JSONObject toJson() {
JSONObject json = super.toJson();
try {
json.put("controlProtocolVersion", controlProtocolVersion);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
public int getControlProtocolVersion() {
return controlProtocolVersion;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Snapserver that = (Snapserver) o;
if (controlProtocolVersion != that.controlProtocolVersion) return false;
return super.equals(o);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + controlProtocolVersion;
return result;
}
}

View file

@ -42,7 +42,6 @@ public:
add("ClientName", "Snapclient");
add("OS", ::getOS());
add("Arch", ::getArch());
add("SnapControlProtocolVersion", "1");
add("SnapStreamProtocolVersion", "1");
}
@ -80,12 +79,7 @@ public:
return get("Arch");
}
int getControlProtocolVersion()
{
return get("SnapControlProtocolVersion", 1);
}
int getStreamProtocolVersion()
int getProtocolVersion()
{
return get("SnapStreamProtocolVersion", 1);
}

View file

@ -137,31 +137,63 @@ struct ClientConfig
struct Snapcast
{
Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), streamProtocolVersion(1), controlProtocolVersion(1)
Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), protocolVersion(1)
{
}
void fromJson(const json& j)
virtual ~Snapcast()
{
}
virtual void fromJson(const json& j)
{
name = trim_copy(jGet<std::string>(j, "name", ""));
version = trim_copy(jGet<std::string>(j, "version", ""));
streamProtocolVersion = jGet<int>(j, "streamProtocolVersion", 1);
controlProtocolVersion = jGet<int>(j, "controlProtocolVersion", 1);
protocolVersion = jGet<int>(j, "protocolVersion", 1);
}
json toJson()
virtual json toJson()
{
json j;
j["name"] = trim_copy(name);
j["version"] = trim_copy(version);
j["streamProtocolVersion"] = streamProtocolVersion;
j["controlProtocolVersion"] = controlProtocolVersion;
j["protocolVersion"] = protocolVersion;
return j;
}
std::string name;
std::string version;
int streamProtocolVersion;
int protocolVersion;
};
struct Snapclient : public Snapcast
{
Snapclient(const std::string& _name = "", const std::string& _version = "") : Snapcast(_name, _version)
{
}
};
struct Snapserver : public Snapcast
{
Snapserver(const std::string& _name = "", const std::string& _version = "") : Snapcast(_name, _version), controlProtocolVersion(1)
{
}
virtual void fromJson(const json& j)
{
Snapcast::fromJson(j);
controlProtocolVersion = jGet<int>(j, "controlProtocolVersion", 1);
}
virtual json toJson()
{
json j = Snapcast::toJson();
j["controlProtocolVersion"] = controlProtocolVersion;
return j;
}
int controlProtocolVersion;
};
@ -218,7 +250,7 @@ struct ClientInfo
}
Host host;
Snapcast snapclient;
Snapclient snapclient;
ClientConfig config;
timeval lastSeen;
bool connected;

View file

@ -146,7 +146,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
Host host;
//TODO: Set MAC and IP
Snapcast snapserver("Snapserver", VERSION);
Snapserver snapserver("Snapserver", VERSION);
response = {
{"server", {
{"host", host.toJson()},//getHostName()},
@ -262,7 +262,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
connection->macAddress = helloMsg.getMacAddress();
logO << "Hello from " << connection->macAddress << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
<< ", control version: " << helloMsg.getControlProtocolVersion() << ", Stream version: " << helloMsg.getStreamProtocolVersion() << "\n";
<< ", Protocol version: " << helloMsg.getProtocolVersion() << "\n";
logD << "request kServerSettings: " << connection->macAddress << "\n";
// std::lock_guard<std::mutex> mlock(mutex_);
@ -290,8 +290,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
client->host.arch = helloMsg.getArch();
client->snapclient.version = helloMsg.getVersion();
client->snapclient.name = helloMsg.getClientName();
client->snapclient.streamProtocolVersion = helloMsg.getStreamProtocolVersion();
client->snapclient.controlProtocolVersion = helloMsg.getControlProtocolVersion();
client->snapclient.protocolVersion = helloMsg.getProtocolVersion();
client->connected = true;
gettimeofday(&client->lastSeen, NULL);