Skip to content

Commit 5339fea

Browse files
committed
React Actions over net
1 parent 5ea5a43 commit 5339fea

File tree

8 files changed

+353
-34
lines changed

8 files changed

+353
-34
lines changed

src/org/cyberpwn/react/network/NetworkScheduler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ public NetworkScheduler(GList<NetworkedServer> ns, long ms)
1313
this.ms = ms;
1414
}
1515

16+
@Override
1617
public void run()
1718
{
1819
while(!interrupted())
1920
{
2021
for(NetworkedServer i : ns)
2122
{
23+
i.requestActions();
2224
i.requestSample();
2325
}
2426

2527
try
2628
{
2729
Thread.sleep(ms);
28-
}
30+
}
2931

3032
catch(InterruptedException e)
3133
{

src/org/cyberpwn/react/network/NetworkedServer.java

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.cyberpwn.react.network;
22

3+
import javax.swing.JLabel;
34
import org.cyberpwn.react.ui.ServerTab;
5+
import org.cyberpwn.react.util.GList;
46
import org.cyberpwn.react.util.GMap;
57
import org.cyberpwn.react.util.JSONArray;
68
import org.cyberpwn.react.util.JSONObject;
@@ -15,32 +17,38 @@ public class NetworkedServer
1517
private GMap<String, Double> sample;
1618
private ServerTab tab;
1719
private Request rx;
20+
private RequestActions ra;
1821
private String version;
1922
private String versionBukkit;
23+
private GList<String> actions;
24+
private boolean gettingReactors;
2025

2126
public NetworkedServer(String name, String username, String password, String address, Integer port)
2227
{
2328
this.name = name;
2429
this.username = username;
2530
this.password = password;
2631
this.address = address;
27-
this.tab = null;
32+
tab = null;
33+
gettingReactors = true;
34+
actions = null;
2835
this.port = port;
29-
this.sample = new GMap<String, Double>();
30-
this.rx = null;
31-
this.version = null;
32-
this.versionBukkit = null;
36+
sample = new GMap<String, Double>();
37+
rx = null;
38+
ra = null;
39+
version = null;
40+
versionBukkit = null;
3341
}
3442

3543
public NetworkedServer(JSONObject js)
3644
{
37-
this.name = js.getString("name");
38-
this.username = js.getString("username");
39-
this.password = js.getString("password");
40-
this.address = js.getString("address");
41-
this.port = js.getInt("port");
42-
this.tab = null;
43-
this.sample = new GMap<String, Double>();
45+
name = js.getString("name");
46+
username = js.getString("username");
47+
password = js.getString("password");
48+
address = js.getString("address");
49+
port = js.getInt("port");
50+
tab = null;
51+
sample = new GMap<String, Double>();
4452
}
4553

4654
public void toJson(JSONArray parent)
@@ -56,12 +64,51 @@ public void toJson(JSONArray parent)
5664
parent.put(js);
5765
}
5866

67+
public void requestActions()
68+
{
69+
if(actions != null)
70+
{
71+
return;
72+
}
73+
74+
gettingReactors = true;
75+
76+
tab.pushStartedActions();
77+
78+
if(ra != null && ra.isAlive())
79+
{
80+
return;
81+
}
82+
83+
ra = new RequestActions(this, new RequestActionsCallback()
84+
{
85+
@Override
86+
public void run()
87+
{
88+
if(isOk())
89+
{
90+
actions = getActions();
91+
tab.push(actions);
92+
gettingReactors = false;
93+
}
94+
}
95+
});
96+
97+
ra.start();
98+
}
99+
59100
public void requestSample()
60101
{
102+
if(gettingReactors)
103+
{
104+
return;
105+
}
106+
61107
if(version == null || versionBukkit == null)
62108
{
63109
new RequestBasic(this, new RequestCallbackBasic()
64110
{
111+
@Override
65112
public void run()
66113
{
67114
if(isOk())
@@ -80,6 +127,7 @@ public void run()
80127

81128
rx = new Request(this, new RequestCallback()
82129
{
130+
@Override
83131
public void run()
84132
{
85133
if(isOk())
@@ -163,19 +211,42 @@ public void setTab(ServerTab tab)
163211
{
164212
this.tab = tab;
165213
}
166-
214+
167215
public Request getRx()
168216
{
169217
return rx;
170218
}
171-
219+
172220
public String getVersion()
173221
{
174222
return version;
175223
}
176-
224+
177225
public String getVersionBukkit()
178226
{
179227
return versionBukkit;
180228
}
229+
230+
public void act(String action, final JLabel label)
231+
{
232+
label.setText("Preparing...");
233+
RequestAction ra = new RequestAction(this, new RequestActionCallback()
234+
{
235+
@Override
236+
public void run()
237+
{
238+
if(isOk())
239+
{
240+
label.setText("Action Completed");
241+
}
242+
243+
else
244+
{
245+
label.setText("Failed... Check Console.");
246+
}
247+
}
248+
}, action);
249+
label.setText("Executing...");
250+
ra.start();
251+
}
181252
}

src/org/cyberpwn/react/network/PacketResponse.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class PacketResponse
1010

1111
public PacketResponse()
1212
{
13-
this.js = new JSONObject();
13+
js = new JSONObject();
1414
}
1515

1616
public PacketResponse(JSONObject js)
@@ -85,6 +85,12 @@ public Boolean getBoolean(String s)
8585
return js.getBoolean(s);
8686
}
8787

88+
@Override
89+
public String toString()
90+
{
91+
return js.toString();
92+
}
93+
8894
public GList<String> getStringList(String s)
8995
{
9096
GList<String> list = new GList<String>();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.cyberpwn.react.network;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.net.Socket;
6+
import org.cyberpwn.react.util.JSONObject;
7+
8+
public class RequestAction extends Thread
9+
{
10+
private RequestActionCallback callback;
11+
private NetworkedServer ns;
12+
private Socket s;
13+
private String action;
14+
15+
public RequestAction(NetworkedServer ns, RequestActionCallback callback, String action)
16+
{
17+
this.callback = callback;
18+
this.ns = ns;
19+
this.action = action;
20+
}
21+
22+
@Override
23+
public void run()
24+
{
25+
try
26+
{
27+
System.out.println("Requesting Action: " + action);
28+
s = new Socket(ns.getAddress(), ns.getPort());
29+
s.setSoTimeout(500);
30+
DataInputStream i = new DataInputStream(s.getInputStream());
31+
DataOutputStream o = new DataOutputStream(s.getOutputStream());
32+
PacketRequest pr = new PacketRequest(ns.getUsername(), ns.getPassword(), "ACTION " + action);
33+
System.out.println("OUT: " + pr.toString());
34+
o.writeUTF(pr.toString());
35+
o.flush();
36+
String response = i.readUTF();
37+
PacketResponse ps = new PacketResponse(new JSONObject(response));
38+
System.out.println("IN: " + ps.toString());
39+
40+
if(ps.getString("type").equals("OK"))
41+
{
42+
callback.run(true);
43+
}
44+
45+
else
46+
{
47+
callback.run(false);
48+
}
49+
}
50+
51+
catch(Exception e)
52+
{
53+
54+
}
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.cyberpwn.react.network;
2+
3+
public class RequestActionCallback implements Runnable
4+
{
5+
private boolean ok;
6+
7+
public void run(boolean ok)
8+
{
9+
this.ok = ok;
10+
run();
11+
}
12+
13+
@Override
14+
public void run()
15+
{
16+
17+
}
18+
19+
public boolean isOk()
20+
{
21+
return ok;
22+
}
23+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.cyberpwn.react.network;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.net.Socket;
6+
import org.cyberpwn.react.util.GList;
7+
import org.cyberpwn.react.util.JSONObject;
8+
9+
public class RequestActions extends Thread
10+
{
11+
private RequestActionsCallback callback;
12+
private NetworkedServer ns;
13+
private Socket s;
14+
15+
public RequestActions(NetworkedServer ns, RequestActionsCallback callback)
16+
{
17+
this.callback = callback;
18+
this.ns = ns;
19+
}
20+
21+
@Override
22+
public void run()
23+
{
24+
try
25+
{
26+
System.out.println("Polling action mapping...");
27+
s = new Socket(ns.getAddress(), ns.getPort());
28+
s.setSoTimeout(500);
29+
DataInputStream i = new DataInputStream(s.getInputStream());
30+
DataOutputStream o = new DataOutputStream(s.getOutputStream());
31+
PacketRequest pr = new PacketRequest(ns.getUsername(), ns.getPassword(), PacketRequestType.GET_ACTIONS.toString());
32+
o.writeUTF(pr.toString());
33+
o.flush();
34+
System.out.println("Reading action mapping...");
35+
String response = i.readUTF();
36+
PacketResponse ps = new PacketResponse(new JSONObject(response));
37+
GList<String> acts = new GList<String>();
38+
39+
try
40+
{
41+
Thread.sleep(800);
42+
}
43+
44+
catch(InterruptedException e)
45+
{
46+
e.printStackTrace();
47+
}
48+
49+
if(ps.getString("type").equals("OK"))
50+
{
51+
for(Object j : ps.getJSON().getJSONArray("actions"))
52+
{
53+
try
54+
{
55+
acts.add((String) j);
56+
}
57+
58+
catch(Exception e)
59+
{
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
System.out.println("Discovered " + acts.size() + " actions.");
65+
System.out.println(acts.toString(", "));
66+
callback.run(acts, true);
67+
}
68+
69+
else
70+
{
71+
callback.run(acts, false);
72+
}
73+
}
74+
75+
catch(Exception e)
76+
{
77+
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)