-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
120 lines (110 loc) · 4.38 KB
/
Example2.java
File metadata and controls
120 lines (110 loc) · 4.38 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
/*
* Copyright (C) 2013 Sebastian "prodigy" Grunow <sebastian.gr at servertube.net>.
*
* YATSQUO is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* YATSQUO is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with YATSQUO; If not, see
* <http://www.gnu.org/licenses/>.
*/
package net.servertube.yatsquo.examples;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import net.servertube.yatsquo.Data.EventType;
import net.servertube.yatsquo.QueryException;
import net.servertube.yatsquo.QueryInterface;
import net.servertube.yatsquo.QueryListener;
import net.servertube.yatsquo.Server;
/**
* QueryInterface - Example2<br />
* <br />
* Establish a connection and hook up an event caller<br />
* The program quits when an event is received.
* @author Sebastian "prodigy" G.
*/
public class Example2 {
private static String user = "serveradmin";
private static String passwd = "PASSWORD";
private static String ip = "127.0.0.1";
private static int port = 10011;
private QueryInterface qi;
protected Boolean running;
/**
* @see Example2
* @param args
* @throws QueryException
*/
public static void main(String[] args) throws QueryException {
Example2 ex2 = new Example2(args);
ex2.showMe();
}
public Example2(String[] args) throws QueryException {
qi = new QueryInterface(args[0], Integer.valueOf(args[1]), args[2], args[3], new Listener(qi));
running = true;
}
public void showMe() throws QueryException {
// get the first server in the list
Server s = qi.getServers().get(0);
// register all available events
s.registerEvent(EventType.TEXT_SERVER, null);
s.registerEvent(EventType.TEXT_CHANNEL, null);
s.registerEvent(EventType.TEXT_PRIVATE, null);
s.registerEvent(EventType.EVENT_SERVER, null);
s.registerEvent(EventType.CLIENT_MOVED, null);
// enable the event listener
qi.getQueryListener().enableEventListener();
// get the current server name
String serverName = s.getName();
String newName;
while (running) {
/*
* While we are waiting for an event we change the server name in a loop
* that breaks when we receive an event.
* This demonstrates that the seperate thread of the QueryListener does
* even work under heavy load of other commands. A situation like this
* is very uncommon in a live environment. In a test on a local host I
* got about 14 commands per second in such a loop. (With each name change
* and the EVENT_SERVER registered a notifyserveredited event is passed
* which is NOT catched by the library, yet; This increases traffic
* and reduces possible commands per second)
*
* The library still passes commands and receives the correct replies while
* the system waits for events on another thread. All events are received.
*/
//newName = new BigInteger(130, new SecureRandom()).toString(32).substring(0, 8);
//s.setName(newName);
}
// reset the server name to what we got before
s.setName(serverName);
}
protected class Listener extends QueryListener {
public Listener(QueryInterface qi) {
super(qi);
}
/*
* Override the abstract function executeEvent with our own testing function<br />
* This function simply displays all data received with the event and disables<br />
* the name-changing-loop in our main class.<br />
*/
@Override
public void executeEvent(EventType type, HashMap<String, String> data) {
System.out.println("** EVENT RAISED **");
System.out.println(" - Type: " + type.toString());
System.out.println(" - Data {");
for (String key : data.keySet()) {
System.out.println(" " + key + " = " + data.get(key));
}
System.out.println(" }");
//running = false;
}
}
}