aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005/dsub/domain/ServerInfo.java
blob: 0109ab3094ef0c4371845d9a87181dfa4f4c4976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2010 (C) Sindre Mehus
 */
package github.daneren2005.dsub.domain;

/**
 * Information about the Subsonic server.
 *
 * @author Sindre Mehus
 */
public class ServerInfo implements Serializable {
	public static final int TYPE_SUBSONIC = 1;
	public static final int TYPE_MADSONIC = 2;
	private static final Map<Integer, ServerInfo> SERVERS = new ConcurrentHashMap<Integer, ServerInfo>();
	
	private boolean isLicenseValid;
	private Version restVersion;
	private int type;
	
	public ServerInfo() {
		type = TYPE_SUBSONIC;
	}

	public boolean isLicenseValid() {
		return isLicenseValid;
	}

	public void setLicenseValid(boolean licenseValid) {
		isLicenseValid = licenseValid;
	}

	public Version getRestVersion() {
		return restVersion;
	}

	public void setRestVersion(Version restVersion) {
		this.restVersion = restVersion;
	}
    
	public int getRestType() {
		return type;
	}
	public void setRestType(int type) {
		this.type = type;
	}
	
	public boolean isStockSubsonic() {
		return type == TYPE_SUBSONIC;
	}
	public boolean isMadsonic() {
		return type == TYPE_MADSONIC;
	}
	
	@Override
	public boolean equals(Object o) {
		if(this == 0) {
			return true;
		} else if(o == null || getClass() != o.getClass()) {
			return false;
		}
	    
		final ServerInfo info = (ServerInfo) o;
	    
		if(this.type != info.type) {
			return false;
		} else if(this.restVersion == null || info.restVersion == null) {
			// Should never be null unless just starting up
			return false;
		} else {
			return this.restVersion.equals(info.restVersion);
		}
	}

	public void saveServerInfo(Context context) {
		saveServerInfo(context, Util.getActiveServer(context));
	}
	public void saveServerInfo(Context context, int instance) {
		ServerInfo current = SERVERS.get(instance);
		if(!this.equals(current)) {
			SERVERS.put(instance, this);
			FileUtil.serialize(context, this, getCacheName(context, instance));
		}
	}
	
	public static ServerInfo getServerInfo(Context context) {
		return getServerInfo(context, Util.getActiveServer(context));
	}
	public static ServerInfo getServerInfo(Context context, int instance) {
		ServerInfo current = SERVERS.get(instance);
		if(current != null) {
			return current;
		}
		
		current = FileUtil.deserialize(context, getCacheName(context, instance), ServerInfo.class);
		if(current != null) {
			SERVERS.put(instance, current);
		}
		
		return current;
	}

	public static boolean checkServerVersion(Context context, String requiredVersion) {
		return checkServerVersion(context, requiredVersion, Util.getActiveServer(context));
	}
	public static boolean checkServerVersion(Context context, String requiredVersion, int instance) {
		ServerInfo server = getServerInfo(context, instance);
		if(server == null) {
			return false;
		}
		
		Version version = server.getRestVersion();
		if(version == null) {
			return false;
		}
		
		Version required = new Version(requiredVersion);
		return version.compareTo(required) >= 0;
	}
	
	private static String getCacheName(context, instance) {
		return "server-" + Util.getRestUrl(context, null, instance, false).hashCode() + ".ser";
	}
}