Retrieving server info with Java

· PS FAQ · Search Forum · Any ET related problems? Post them here
Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Retrieving server info with Java

Postby GoldenBullet » Sun Nov 26, 2017 14:42

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.

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Sun Nov 26, 2017 15:12

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 :D


You basically send a getstatus request on a udp socket and it sends you all the info it has

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Sun Nov 26, 2017 16:40

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:

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".

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Sun Nov 26, 2017 17:11

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.

Spam Pro
User avatar
Posts: 332
Joined: Mon Aug 13, 2007 0:00

Re: Retrieving server info with Java

Postby Epa » Sun Nov 26, 2017 20:43

start mining with rapids que htmml java, and see what happens if youll get your app ;exec

Spam Pro
User avatar
Posts: 332
Joined: Mon Aug 13, 2007 0:00

Re: Retrieving server info with Java

Postby Epa » Sun Nov 26, 2017 21:00

tppshss

Spam Pro
User avatar
Posts: 332
Joined: Mon Aug 13, 2007 0:00

Re: Retrieving server info with Java

Postby Epa » Sun Nov 26, 2017 21:32


Server High Admin
User avatar
Posts: 390
Joined: Wed Aug 26, 2015 18:05

Re: Retrieving server info with Java

Postby Dr Zoidberg » Tue Nov 28, 2017 0:57

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 =)

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

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Tue Nov 28, 2017 9:11

Dr Zoidberg wrote:If I were you, instead of writing error-prone low-level socket requests with all that hassel...



outrageous socket bashing detected

Server High Admin
User avatar
Posts: 390
Joined: Wed Aug 26, 2015 18:05

Re: Retrieving server info with Java

Postby Dr Zoidberg » Tue Nov 28, 2017 10:47

OliVier_ wrote:outrageous socket bashing detected


hehe true =)

//low-level-programming fan #6'999'999'999
//Dr Zoidberg

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Tue Nov 28, 2017 12:59

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.

Spam Pro
User avatar
Posts: 332
Joined: Mon Aug 13, 2007 0:00

Re: Retrieving server info with Java

Postby Epa » Tue Nov 28, 2017 16:56

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

Server High Admin
User avatar
Posts: 390
Joined: Wed Aug 26, 2015 18:05

Re: Retrieving server info with Java

Postby Dr Zoidberg » Tue Nov 28, 2017 21:47

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

Spam Pro
User avatar
Posts: 332
Joined: Mon Aug 13, 2007 0:00

Re: Retrieving server info with Java

Postby Epa » Tue Nov 28, 2017 22:33

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.

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Wed Nov 29, 2017 11:31

Image

from the light side

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Wed Nov 29, 2017 13:02

That looks pretty neat!
Did the getstatus work as predicted?

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Wed Nov 29, 2017 17:59

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.. :mrgreen:

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 972 times

Server High Admin
User avatar
Posts: 390
Joined: Wed Aug 26, 2015 18:05

Re: Retrieving server info with Java

Postby Dr Zoidberg » Sun Dec 03, 2017 23:49

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.
//Dr Zoidberg

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Mon Dec 04, 2017 0:17

Yep so the code is hosted here

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

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Mon Dec 04, 2017 1:01

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

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Mon Dec 04, 2017 2:10

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.

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Mon Dec 04, 2017 15:10

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?

Spam Noob
Posts: 11
Joined: Fri Dec 30, 2016 17:18

Re: Retrieving server info with Java

Postby OliVier_ » Mon Dec 04, 2017 16:27

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.


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

Server High Admin
User avatar
Posts: 1385
Joined: Sun Apr 30, 2006 0:00
Location: Poland

Re: Retrieving server info with Java

Postby RapidHail » Mon Dec 04, 2017 21:03

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

Server High Admin
User avatar
Posts: 2923
Joined: Mon May 01, 2006 0:00
Location: Finland

Re: Retrieving server info with Java

Postby GoldenBullet » Tue Dec 05, 2017 1:20

That would be convenient.
Are you planning of making such or is there already way to retrieve the server data in XML/JSON?

Next

Return to ET Help Desk

Who is online

Users browsing this forum: No registered users and 11 guests