WebPreferencesProvider.java
4.24 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.mumfrey.webprefs.framework;
import java.net.Proxy;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.minecraft.util.Session;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
import com.mumfrey.webprefs.interfaces.IWebPreferencesClient;
import com.mumfrey.webprefs.interfaces.IWebPreferencesProvider;
import com.mumfrey.webprefs.interfaces.IWebPreferencesService;
import com.mumfrey.webprefs.interfaces.IWebPreferencesServiceMonitor;
public class WebPreferencesProvider extends Thread implements IWebPreferencesProvider, IWebPreferencesServiceMonitor
{
private final IWebPreferencesService service;
private final String hostName;
private final Session session;
private final int failureThreshold;
private int failureCount = 0;
private volatile boolean active = true;
private final BlockingQueue<WebPreferencesServiceTask> tasks = new LinkedBlockingQueue<WebPreferencesServiceTask>(2048);
public WebPreferencesProvider(Proxy proxy, Session session, String hostName, int maxFailedRequestsCount)
{
this.service = new WebPreferencesService(proxy, session);
this.service.addMonitor(this);
this.hostName = hostName;
this.session = session;
this.failureThreshold = maxFailedRequestsCount;
this.setName("WebPreferencesProvider daemon thread [" + hostName + "]");
this.setDaemon(true);
this.start();
}
@Override
public boolean isActive()
{
return this.active;
}
public void onTick()
{
}
@Override
public void run()
{
try
{
while (this.active)
{
WebPreferencesServiceTask task = this.tasks.take();
try
{
LiteLoaderLogger.debug("WebPreferencesProvider [%s] is processing %s for %s", this.hostName,
task.getClass().getSimpleName(), task.getRequest().getUUID());
this.service.submit(task.getRequest());
}
catch (Throwable th)
{
if (th instanceof InterruptedException) throw (InterruptedException)th;
th.printStackTrace();
this.onRequestFailed(th, 1);
}
}
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
@Override
public void onKeyRequestFailed()
{
this.registerError(this.failureThreshold / 2);
}
@Override
public void onRequestFailed(Throwable th, int severity)
{
this.registerError(severity);
}
private void registerError(int severity)
{
this.failureCount += severity;
if (this.failureCount >= this.failureThreshold)
{
LiteLoaderLogger.warning("WebPreferencesProvider for " + this.hostName + " is terminating. Too many failed requests.");
this.active = false;
this.tasks.clear();
this.interrupt();
}
}
@Override
public boolean requestGet(IWebPreferencesClient client, String uuid, Set<String> keys, boolean getPrivate)
{
if (!this.isActive())
{
return false;
}
WebPreferencesServiceTask task = new WebPreferencesServiceTaskGet(this, client);
task.setRequest(new WebPreferencesRequestGet(task, uuid, keys, getPrivate));
return this.tasks.offer(task);
}
@Override
public boolean requestSet(IWebPreferencesClient client, String uuid, Map<String, String> values, boolean setPrivate)
{
if (!this.isActive())
{
return false;
}
WebPreferencesServiceTask task = new WebPreferencesServiceTaskSet(this, client);
task.setRequest(new WebPreferencesRequestSet(task, uuid, values, setPrivate));
return this.tasks.offer(task);
}
@Override
public String getHostName()
{
return this.hostName;
}
@Override
public Session getSession()
{
return this.session;
}
@Override
public IWebPreferencesService getService()
{
return this.service;
}
}