-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMotdFetcher.java
More file actions
88 lines (74 loc) · 2.54 KB
/
Copy pathMotdFetcher.java
File metadata and controls
88 lines (74 loc) · 2.54 KB
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
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package com.google.appinventor.client;
import static com.google.appinventor.client.Ode.MESSAGES;
import com.google.appinventor.client.output.MotdUi;
import com.google.appinventor.client.output.OdeLog;
import com.google.appinventor.shared.rpc.Motd;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* Polls for new MOTDs and displays them.
*
* <p>We don't use a timer because it would keep the server busy with MOTD fetches even when the
* user isn't actively using App Inventor. Instead, we fetch the MOTD when another RPC is going to
* the server and the specified interval of time has elapsed since the last MOTD fetch. This way,
* we only fetch the MOTD if the user is actively using App Inventor.</p>
*
* @author kerr@google.com (Debby Wallach)
*/
class MotdFetcher implements RpcListener {
private final int intervalMillis; // how long to wait between fetches
private long lastFetchTime;
MotdFetcher(int intervalSecs) {
intervalMillis = intervalSecs * 1000;
fetchMotd();
}
/**
* Register a service proxy with this MOTD fetcher.
*
* @param service the service to monitor
*/
void register(ExtendedServiceProxy<?> service) {
service.addRpcListener(this);
}
/**
* Unregister a service proxy with the loading popup.
*
* @param service the service to monitor
*/
void unregister(ExtendedServiceProxy<?> service) {
service.removeRpcListener(this);
}
// RpcListener implementation
@Override
public void onStart(String method, Object... params) {
// When an RPC is going to the server, we check whether enough time has elapsed that we should
// fetch the MOTD.
long now = System.currentTimeMillis();
if (now - lastFetchTime >= intervalMillis) {
fetchMotd();
}
}
@Override
public void onFailure(String method, Throwable caught) {
}
@Override
public void onSuccess(String method, Object result) {
}
private void fetchMotd() {
lastFetchTime = System.currentTimeMillis();
AsyncCallback<Motd> callback = new AsyncCallback<Motd>() {
@Override
public void onFailure(Throwable caught) {
OdeLog.log(MESSAGES.getMotdFailed());
}
@Override
public void onSuccess(Motd motd) {
MotdUi.getMotd().setMotd(motd);
}
};
Ode.getInstance().getGetMotdService().getMotd(callback);
}
}