APIv3/src/main/java/net/frozenorb/apiv3/models/Server.java

99 lines
3.3 KiB
Java
Raw Normal View History

2016-04-08 13:12:31 +02:00
package net.frozenorb.apiv3.models;
2016-02-06 02:58:49 +01:00
import com.google.common.collect.ImmutableSet;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
2016-02-06 02:58:49 +01:00
import lombok.Getter;
2016-04-30 20:03:34 +02:00
import lombok.Setter;
2016-03-21 23:28:17 +01:00
import net.frozenorb.apiv3.APIv3;
2016-05-01 06:34:02 +02:00
import net.frozenorb.apiv3.serialization.ExcludeFromReplies;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.utils.SyncUtils;
import org.bson.Document;
2016-02-06 02:58:49 +01:00
2016-04-27 02:46:34 +02:00
import java.util.*;
2016-05-29 08:43:37 +02:00
import java.util.concurrent.TimeUnit;
2016-02-06 02:58:49 +01:00
2016-03-21 23:28:17 +01:00
public final class Server {
2016-02-08 00:03:42 +01:00
private static final MongoCollection<Server> serversCollection = APIv3.getDatabase().getCollection("servers", Server.class);
2016-05-29 08:43:37 +02:00
private static Map<String, Server> serverCache = null;
private static List<Server> serverCacheAlt = null;
private static long serverCacheUpdated = 0;
2016-04-08 13:12:31 +02:00
@Getter @Id private String id;
2016-02-08 00:03:42 +01:00
@Getter private String displayName;
2016-05-01 06:34:02 +02:00
@Getter @ExcludeFromReplies String apiKey;
@Getter private String serverGroup;
2016-05-10 18:30:21 +02:00
@Getter private String serverIp;
2016-05-11 00:37:07 +02:00
@Getter @Setter private Date lastUpdatedAt;
2016-04-30 20:03:34 +02:00
@Getter @Setter private double lastTps;
2016-05-01 06:34:02 +02:00
@Getter @Setter @ExcludeFromReplies private Set<UUID> players;
2016-02-08 00:03:42 +01:00
public static List<Server> findAll() {
2016-05-29 08:43:37 +02:00
updateCacheIfNeeded();
return serverCacheAlt;
2016-02-08 00:03:42 +01:00
}
public static Server findById(String id) {
2016-05-29 08:43:37 +02:00
updateCacheIfNeeded();
return serverCache.get(id);
2016-04-27 02:46:34 +02:00
}
2016-03-21 23:28:17 +01:00
public Server() {} // For Morphia
2016-05-20 22:19:13 +02:00
public Server(String id, String displayName, String apiKey, ServerGroup serverGroup, String serverIp) {
2016-03-21 23:28:17 +01:00
this.id = id;
this.displayName = displayName;
2016-05-01 02:08:58 +02:00
this.apiKey = apiKey;
2016-05-11 00:37:07 +02:00
this.serverGroup = serverGroup.getId();
2016-05-10 18:30:21 +02:00
this.serverIp = serverIp;
2016-05-11 00:37:07 +02:00
this.lastUpdatedAt = new Date();
2016-03-21 23:28:17 +01:00
this.lastTps = 0;
this.players = new HashSet<>();
2016-02-08 00:03:42 +01:00
}
2016-05-29 08:43:37 +02:00
private static void updateCacheIfNeeded() {
if (serverCache == null || (System.currentTimeMillis() - serverCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
Map<String, Server> working = new HashMap<>();
List<Server> workingAlt = new ArrayList<>();
for (Server server : SyncUtils.blockMulti(serversCollection.find())) {
2016-05-29 08:43:37 +02:00
working.put(server.getId(), server);
workingAlt.add(server);
}
serverCache = working;
serverCacheAlt = workingAlt;
serverCacheUpdated = System.currentTimeMillis();
}
}
public void receivedHeartbeat(double tps, Iterable<UUID> players) {
this.lastUpdatedAt = new Date();
this.lastTps = tps;
this.players = ImmutableSet.copyOf(players);
}
public void insert() {
BlockingCallback<Void> callback = new BlockingCallback<>();
serversCollection.insertOne(this, callback);
callback.get();
}
public void save() {
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
serversCollection.replaceOne(new Document("_id", id), this, callback);
callback.get();
}
public void delete() {
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
serversCollection.deleteOne(new Document("_id", id), callback);
callback.get();
}
2016-02-06 02:58:49 +01:00
}