Retrieving server info with Java
27 posts
• Page 1 of 2 • 1, 2
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Retrieving server info with Java
So this isn't really related to ET itself, but it kinda is and I need help with it so I posted it here.
Anyway, I've had this small idea for an android app I wanted to try make. It would check the server regularly and if there were enough players online, it would give an alarm to the phone.
However this made me think how could I retrieve the server info itself like in the server monitor. Is there a way to get, like, an XML-file out of it which I could parse in Java? I mostly need the playerlist and their pings.
Anyway, I've had this small idea for an android app I wanted to try make. It would check the server regularly and if there were enough players online, it would give an alarm to the phone.
However this made me think how could I retrieve the server info itself like in the server monitor. Is there a way to get, like, an XML-file out of it which I could parse in Java? I mostly need the playerlist and their pings.
Re: Retrieving server info with Java
Hello,
i don't know if there is a java implementation out there, but this is a good looking php implementation,
you just have to translate it into java: https://github.com/adawolfa/etwsl/blob/master/src/ServerQuery.php
Its a matter of a few sockets
You basically send a getstatus request on a udp socket and it sends you all the info it has
i don't know if there is a java implementation out there, but this is a good looking php implementation,
you just have to translate it into java: https://github.com/adawolfa/etwsl/blob/master/src/ServerQuery.php
Its a matter of a few sockets
You basically send a getstatus request on a udp socket and it sends you all the info it has
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
Thanks for the info Olivier. So the request seems to be (in PHP)
@fwrite($socket, str_repeat(chr(255), 4) . "getstatus\n");
What bugs me is that str_repeat. It seems to repeat ÿ four times and I don't understand why.
To open all the variables, the request would look like
udp://xx.xx.xx.xx:yy ÿÿÿÿ.getstatus
maybe it's some php-spesific feature?
In java, the socket code would probably be something like:
Then put the "getstatus" into the "in" and read the result from the "out".
@fwrite($socket, str_repeat(chr(255), 4) . "getstatus\n");
What bugs me is that str_repeat. It seems to repeat ÿ four times and I don't understand why.
To open all the variables, the request would look like
udp://xx.xx.xx.xx:yy ÿÿÿÿ.getstatus
maybe it's some php-spesific feature?
In java, the socket code would probably be something like:
- Code: Select all
try (
Socket connection = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(connection.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(connection.getInputStream()));
)
Then put the "getstatus" into the "in" and read the result from the "out".
Re: Retrieving server info with Java
In java you will have to use the classes DatagramSocket / DatagramPacket to send and receive UDP packets, not the Socket class.
the '\xff\xff\xff\xff' produced by str_repeat(chr(255), 4) is just a header for the packets so you will have to include it in all the queries and it will be
included in the responses as well.
the '\xff\xff\xff\xff' produced by str_repeat(chr(255), 4) is just a header for the packets so you will have to include it in all the queries and it will be
included in the responses as well.
Re: Retrieving server info with Java
start mining with rapids que htmml java, and see what happens if youll get your app ;exec
- Dr Zoidberg
- Posts: 390
- Joined: Wed Aug 26, 2015 18:05
Re: Retrieving server info with Java
Love the idea! Would definitly install that app on my phone. Whenever > 6 humans, notify me!
If I were you, instead of writing error-prone low-level socket requests with all that hassel... i'd just use JSoup + CSS selectors to parse necessary user information on PS from http://et.rapidhail.pl/. Disregard the "users" with ping 0 as these are bots. Voila =) Then poll the site as often as you want, like every minute.
Just wrote the code for you =) 20 lines of code. Enjoy =)
If I were you, instead of writing error-prone low-level socket requests with all that hassel... i'd just use JSoup + CSS selectors to parse necessary user information on PS from http://et.rapidhail.pl/. Disregard the "users" with ping 0 as these are bots. Voila =) Then poll the site as often as you want, like every minute.
Just wrote the code for you =) 20 lines of code. Enjoy =)
- Enemy Territory Code: Select all
package whatever;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class whatever {
public static void main(String[] args) {
Document doc;
try {
int numberOfHumans = 0;
int numberOfBots = 0;
doc = Jsoup.connect("http://et.rapidhail.pl/").get();
Elements trs = doc.select("table.server_tab tr[onmouseover=this.style.backgroundColor='#556677']");
if (trs != null) {
for (Element tr : trs) {
Elements tds = tr.select("td");
if (tds != null && tds.size() > 2) {
String username = tds.get(0).text();
String userxp = tds.get(1).text();
String userping = tds.get(2).text();
if (userping.equals("0")) {
numberOfBots++;
} else {
numberOfHumans++;
}
}
}
}
System.out.println("number of bots: " + numberOfBots + ", number of humans: " + numberOfHumans);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Enemy Territory Code: Select all
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.1</version>
</dependency>
//Dr Zoidberg
Re: Retrieving server info with Java
Dr Zoidberg wrote:If I were you, instead of writing error-prone low-level socket requests with all that hassel...
outrageous socket bashing detected
- Dr Zoidberg
- Posts: 390
- Joined: Wed Aug 26, 2015 18:05
Re: Retrieving server info with Java
OliVier_ wrote:outrageous socket bashing detected
hehe true =)
//low-level-programming fan #6'999'999'999
//Dr Zoidberg
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
I was thinking about that Zoid. I was going to exercise my programming skills by using the sockets.
But I suppose I could cut some corners since this is just a small hobby project.
Had there been a little more documentation I would have went with sockets.
Sorry Olivier, I have turned to the dark side :d
I'll see what I can come up with, but don't expect anything soon.
But I suppose I could cut some corners since this is just a small hobby project.
Had there been a little more documentation I would have went with sockets.
Sorry Olivier, I have turned to the dark side :d
I'll see what I can come up with, but don't expect anything soon.
Re: Retrieving server info with Java
GoldenBullet wrote:I was thinking about that Zoid. I was going to exercise my programming skills by using the sockets.
But I suppose I could cut some corners since this is just a small hobby project.
Had there been a little more documentation I would have went with sockets.
Sorry Olivier, I have turned to the dark side :d
I'll see what I can come up with, but don't expect anything soon.
Should we start prime squadron server what could mine money for all members and regulars? If you make it, i can take it upon my name to get caught, prolly wont ever sit in jail, they promised me i don't even have to go to looneybin anymore.
Altho you need to red flag me or leave no evidence, its a pirates life for me eh
- Dr Zoidberg
- Posts: 390
- Joined: Wed Aug 26, 2015 18:05
Re: Retrieving server info with Java
Epa wrote:Should we start prime squadron server what could mine money for all members and regulars? If you make it, i can take it upon my name to get caught, prolly wont ever sit in jail, they promised me i don't even have to go to looneybin anymore.
Altho you need to red flag me or leave no evidence, its a pirates life for me eh
But Epa, mining cryptocurrencies isnt illegal in most countries. =) You can even mine with your home computer if you'd like. No need for piracy... but I still recommend using an eye-patch while doing it, because hey... eye-patches are fun! =)
//Dr Zoidberg
Re: Retrieving server info with Java
Dr Zoidberg wrote:Epa wrote:Should we start prime squadron server what could mine money for all members and regulars? If you make it, i can take it upon my name to get caught, prolly wont ever sit in jail, they promised me i don't even have to go to looneybin anymore.
Altho you need to red flag me or leave no evidence, its a pirates life for me eh
But Epa, mining cryptocurrencies isnt illegal in most countries. =) You can even mine with your home computer if you'd like. No need for piracy... but I still recommend using an eye-patch while doing it, because hey... eye-patches are fun! =)
haha, i don't believe in money, just wondering if anyone hungry. I know everything anyone i've talked to or made eye contact with me knows.
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
That looks pretty neat!
Did the getstatus work as predicted?
Did the getstatus work as predicted?
Re: Retrieving server info with Java
GoldenBullet wrote:Did the getstatus work as predicted?
Of course
But java sucks in sending raw packets
Edit: uploaded an APK, could you test it ?
sorry i stole you the idea but you know when you started..
Edit2: i suggest you set the refresh time to a lower value when you are not connected with wifi (like 8000)
- Attachments
-
- app.zip
- (1.68 MiB) Downloaded 971 times
- Dr Zoidberg
- Posts: 390
- Joined: Wed Aug 26, 2015 18:05
Re: Retrieving server info with Java
Just installed it, and it works beautifully =)
Well done guys!
Will this be open-source? ...you guys might have installed a keelogger, and will find out my awesome password: ILoveMyLittlePony! =) No, for real, not sure how easy it is to migrate to other games, but the potential is definitly there.
Well done guys!
Will this be open-source? ...you guys might have installed a keelogger, and will find out my awesome password: ILoveMyLittlePony! =) No, for real, not sure how easy it is to migrate to other games, but the potential is definitly there.
//Dr Zoidberg
Re: Retrieving server info with Java
Yep so the code is hosted here
Also i attach a release version of the apk (may be a little bit more optimized)
Also i attach a release version of the apk (may be a little bit more optimized)
- Attachments
-
- PSNotify.zip
- (1.45 MiB) Downloaded 933 times
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
Do you think the repeatable query task could be performed with ScheduledExecutorService instead of Handler/HandlerThread you've used?
I'll still try to make my own version, since I want to exercise Java :d
I'll still try to make my own version, since I want to exercise Java :d
Re: Retrieving server info with Java
GoldenBullet wrote:Do you think the repeatable query task could be performed with ScheduledExecutorService instead of Handler/HandlerThread you've used?
I'll still try to make my own version, since I want to exercise Java :d
I did not know that this class actually existed :O
Well, in both cases you will have to run a Service class and perform the scheduling in that class.
I did some research but i don't find a comparative between Handler/ScheduledExecutorService, only that ScheduledExecutorService is more the "java way" than the "android way" , those methods seems quite close but i don't see any problem in using that class.
In any way i think it is better than to use AlarmManager since we are working with potentially small delays
The issue with setting a long period in this app is that the sockets may timeout when we are located in an area with poor GSM conn.
resulting in not being notified at all, so we have to perform requests often enough.
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
Actually now that I think of it, we might be making the program a little differently. I couldn't install your app (not sure if it would run on my phone, it has an antique android version), but there doesn't seem to be any UI other than the notification every now and then.
I was planning to make an interface where you could snooze the notifications, change settings and refresh the player count manually in addition to the background service, so no need to tamper with any config files or source code
I'll probably collide with problems when I want to contact the background service with the foreground UI after the foreground part has been closed previously. Do you know if there's a command that would inquire if a spesific service is already in progress, so I wouldn't need to kill the old service and make a new one?
I was planning to make an interface where you could snooze the notifications, change settings and refresh the player count manually in addition to the background service, so no need to tamper with any config files or source code
I'll probably collide with problems when I want to contact the background service with the foreground UI after the foreground part has been closed previously. Do you know if there's a command that would inquire if a spesific service is already in progress, so I wouldn't need to kill the old service and make a new one?
Re: Retrieving server info with Java
Yep i have used a convenience function that make the app compatible only with android >= 6 :/ There is a way to get the equivalent for all the version but it is also more work xD (laziness++)
Actually there is an UI defined in src/main/res/layout/activity_main.xml that kind of do what you describe.
No need to worry about that startService() does the check for you
Actually there is an UI defined in src/main/res/layout/activity_main.xml that kind of do what you describe.
Do you know if there's a command that would inquire if a spesific service is already in progress, so I wouldn't need to kill the old service and make a new one?
No need to worry about that startService() does the check for you
Re: Retrieving server info with Java
What if there would be a json or xml output instead of downloading whole site?
-=RapidHail=-: Do or do not... There is no try.
-|PS|- Server Monitor
eRepublik Citizen
Network Toolkit
-|PS|- Server Monitor
eRepublik Citizen
Network Toolkit
- GoldenBullet
- Posts: 2923
- Joined: Mon May 01, 2006 0:00
- Location: Finland
Re: Retrieving server info with Java
That would be convenient.
Are you planning of making such or is there already way to retrieve the server data in XML/JSON?
Are you planning of making such or is there already way to retrieve the server data in XML/JSON?
27 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 2 guests