Skip to content

Commit

Permalink
Store device info on the server, get ready to handle FCM messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
codeka committed Sep 22, 2018
1 parent 088eed4 commit 205f416
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 28 deletions.
5 changes: 0 additions & 5 deletions client/src/main/AndroidManifest.xml
Expand Up @@ -39,11 +39,6 @@
</intent-filter>
</activity>

<service android:name=".DeviceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".MessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
Expand Down

This file was deleted.

Expand Up @@ -19,4 +19,10 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
log.info("From: " + remoteMessage.getFrom());
log.info("Notification Message Body: " + remoteMessage.getNotification().getBody());
}

@Override
public void onNewToken(String token) {
log.info("Firebase new token: %s", token);
// TODO: update the server.
}
}
Expand Up @@ -3,6 +3,10 @@
import static com.google.common.base.Preconditions.checkNotNull;

import android.os.Build;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;

import au.com.codeka.warworlds.client.App;
import au.com.codeka.warworlds.client.concurrency.Threads;
import au.com.codeka.warworlds.client.game.world.ChatManager;
Expand All @@ -14,6 +18,7 @@
import au.com.codeka.warworlds.common.net.PacketDecoder;
import au.com.codeka.warworlds.common.net.PacketEncoder;
import au.com.codeka.warworlds.common.proto.DeviceInfo;
import au.com.codeka.warworlds.common.proto.FcmDeviceInfo;
import au.com.codeka.warworlds.common.proto.HelloPacket;
import au.com.codeka.warworlds.common.proto.LoginRequest;
import au.com.codeka.warworlds.common.proto.LoginResponse;
Expand Down Expand Up @@ -234,12 +239,23 @@ private void updateState(
}

private static DeviceInfo populateDeviceInfo() {
String fcmToken = "";
try {
fcmToken = FirebaseInstanceId.getInstance().getToken("wwmmo", "FCM");
} catch (IOException e) {
log.error("Error getting FCM token.", e);
// We won't be able to message this device, I guess.
}
//FirebaseInstanceId.getInstance().getInstanceId()
return new DeviceInfo.Builder()
.device_build(Build.ID)
.device_id(GameSettings.i.getString(GameSettings.Key.INSTANCE_ID))
.device_manufacturer(Build.MANUFACTURER)
.device_model(Build.MODEL)
.device_version(Build.VERSION.RELEASE)
.fcm_device_info(new FcmDeviceInfo.Builder()
.token(fcmToken)
.build())
.build();
}
}
Expand Up @@ -130,4 +130,13 @@ message DeviceInfo {
// The value of android.os.Build.VERSION.RELEASE for this device (note that this can
// change if you upgrade the OS without re-registering your device!)
optional string device_version = 5;

// The Firebase Cloud Messaging info about the device.
optional FcmDeviceInfo fcm_device_info = 6;
}

// Contains details about the Firebase Cloud Messaging device.
message FcmDeviceInfo {
// A token needed to message this device.
optional string token = 1;
}
22 changes: 22 additions & 0 deletions server/src/main/data/admin/tmpl/empires/details.html
Expand Up @@ -8,6 +8,28 @@
{% block "content" %}
<h1>{{ empire.display_name }}</h1>

<h2>Devices</h2>
<table>
<tr>
<th>ID</th>
<th>Model</th>
<th>Manufacturer</th>
<th>Build</th>
<th>Version</th>
<th>FCM token</th>
</tr>
{% for device in devices %}
<tr>
<td>{{ device.device_id }}</td>
<td>{{ device.device_model }}</td>
<td>{{ device.device_manufacturer }}</td>
<td>{{ device.device_build }}</td>
<td>{{ device.device_version }}</td>
<td>{{ device.fcm_device_info.token }}</td>
</tr>
{% end %}
</table>

<h2>Stars</h2>
<table>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/data/admin/tmpl/empires/index.html
@@ -1,5 +1,5 @@
{% extends "skeleton.html" %}
{% block "title" %}Accounts{% end %}
{% block "title" %}Empires{% end %}
{% block "head" %}
<script type="text/javascript" src="/admin/js/tmpl.js"></script>
<script type="text/javascript" src="/admin/js/time.js"></script>
Expand Down
@@ -1,12 +1,14 @@
package au.com.codeka.warworlds.server.admin.handlers;

import au.com.codeka.warworlds.common.proto.DeviceInfo;
import au.com.codeka.warworlds.common.proto.Empire;
import au.com.codeka.warworlds.common.proto.Star;
import au.com.codeka.warworlds.server.handlers.RequestException;
import au.com.codeka.warworlds.server.store.DataStore;
import au.com.codeka.warworlds.server.world.StarManager;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;

/**
* Handler for /admin/empires/xxx which shows details about the empire with id xxx.
Expand All @@ -25,9 +27,12 @@ public void get() throws RequestException {
stars.add(StarManager.i.getStar(starId).get());
}

List<DeviceInfo> devices = DataStore.i.empires().getDevicesForEmpire(empire.id);

render("empires/details.html", ImmutableMap.<String, Object>builder()
.put("empire", empire)
.put("stars", stars)
.put("devices", devices)
.build());
}
}
Expand Up @@ -44,6 +44,7 @@ public void post() throws RequestException {
return;
}

DataStore.i.empires().saveDevice(empire.get(), req.device_info);
DataStore.i.stats().addLoginEvent(req, account);

LoginResponse.Builder resp = new LoginResponse.Builder()
Expand Down
@@ -1,6 +1,7 @@
package au.com.codeka.warworlds.server.store;

import au.com.codeka.warworlds.common.Log;
import au.com.codeka.warworlds.common.proto.DeviceInfo;
import au.com.codeka.warworlds.common.proto.Empire;
import au.com.codeka.warworlds.server.store.base.BaseStore;
import au.com.codeka.warworlds.server.store.base.QueryResult;
Expand Down Expand Up @@ -61,6 +62,40 @@ public void put(long id, Empire empire) {
}
}

/**
* Saves the given device to the empire store, under the given empire. When we want to msg the
* empire, these devices are what we'll message.
*/
public void saveDevice(Empire empire, DeviceInfo deviceInfo) {
try {
newWriter()
.stmt("INSERT OR REPLACE INTO devices (empire_id, device_id, device) VALUES (?, ?, ?)")
.param(0, empire.id)
.param(1, deviceInfo.device_id)
.param(2, deviceInfo.encode())
.execute();
} catch(StoreException e) {
log.error("Unexpected.", e);
}
}

public List<DeviceInfo> getDevicesForEmpire(long empireId) {
ArrayList<DeviceInfo> devices = new ArrayList<>();
try (
QueryResult res = newReader()
.stmt("SELECT device FROM devices WHERE empire_id = ?")
.param(0, empireId)
.query()
) {
while (res.next()) {
devices.add(DeviceInfo.ADAPTER.decode(res.getBytes(0)));
}
} catch (Exception e) {
log.error("Unexpected.", e);
}
return devices;
}

@Override
protected int onOpen(int diskVersion) throws StoreException {
if (diskVersion == 0) {
Expand All @@ -72,6 +107,19 @@ protected int onOpen(int diskVersion) throws StoreException {
.execute();
diskVersion++;
}
if (diskVersion == 1) {
newWriter()
.stmt(
"CREATE TABLE devices (" +
" empire_id INTEGER," +
" device_id STRING," +
" device BLOB)")
.execute();
newWriter()
.stmt("CREATE UNIQUE INDEX IX_devices_empire_device ON devices (empire_id, device_id)")
.execute();
diskVersion++;
}

return diskVersion;
}
Expand Down

0 comments on commit 205f416

Please sign in to comment.