Advertisement
comniemeer

Untitled

Dec 4th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. package de.comniemeer.pingapi;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.DataInput;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutput;
  8. import java.io.DataOutputStream;
  9. import java.io.IOException;
  10. import java.net.Socket;
  11.  
  12. import com.eclipsesource.json.JsonObject;
  13.  
  14. public class PingResult {
  15.    
  16.     private String motd;
  17.     private int onlinePlayers;
  18.     private int maxPlayers;
  19.    
  20.     PingResult(String host, int port, int timeout) {
  21.         try {
  22.             this.ping(host, port);
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.    
  28.     private void ping(String host, int port) throws IOException {
  29.         try (Socket socket = new Socket(host, port)) {
  30.             try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {
  31.                 try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
  32.                     try (ByteArrayOutputStream frame = new ByteArrayOutputStream()) {
  33.                         try (DataOutputStream frameOut = new DataOutputStream(frame)) {
  34.                             // Handshake
  35.                             this.writeVarInt(0x00, frameOut);
  36.                             this.writeVarInt(4, frameOut);
  37.                             this.writeString(host, frameOut);
  38.                             frameOut.writeShort(port);
  39.                             this.writeVarInt(1, frameOut);
  40.                             // Write handshake
  41.                             this.writeVarInt(frame.size(), out);
  42.                             frame.writeTo(out);
  43.                             frame.reset();
  44.                            
  45.                             // Ping
  46.                             this.writeVarInt(0x00, frameOut);
  47.                             // Write ping
  48.                             this.writeVarInt(frame.size(), out);
  49.                             frame.writeTo(out);
  50.                             frame.reset();
  51.                            
  52.                             int len = this.readVarInt(in);
  53.                             byte[] packet = new byte[len];
  54.                             in.readFully(packet);
  55.                            
  56.                             try (ByteArrayInputStream inPacket = new ByteArrayInputStream(packet)) {
  57.                                 try (DataInputStream inFrame = new DataInputStream(inPacket)) {
  58.                                     int id = this.readVarInt(inFrame);
  59.                                    
  60.                                     if (id != 0x00) {
  61.                                         throw new IllegalStateException("Wrong ping response");
  62.                                     }
  63.                                    
  64.                                     JsonObject jsonObject = JsonObject.readFrom(this.readString(inFrame));
  65.                                     JsonObject jsonPlayers = jsonObject.get("players").asObject();
  66.                                    
  67.                                     this.onlinePlayers = jsonPlayers.get("online").asInt();
  68.                                     this.maxPlayers = jsonPlayers.get("max").asInt();
  69.                                     this.motd = jsonObject.get("description").asString();
  70.                                 }
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         } catch (Exception e) {
  77.             ;
  78.         }
  79.     }
  80.    
  81.     private void writeString(String s, DataOutput out) throws IOException {
  82.         byte[] b = s.getBytes("UTF-8");
  83.         this.writeVarInt(b.length, out);
  84.         out.write(b);
  85.     }
  86.    
  87.     private String readString(DataInput in) throws IOException {
  88.         int len = this.readVarInt(in);
  89.         byte[] b = new byte[len];
  90.         in.readFully(b);
  91.        
  92.         return new String(b, "UTF-8");
  93.     }
  94.    
  95.     private int readVarInt(DataInput input) throws IOException {
  96.         int out = 0;
  97.         int bytes = 0;
  98.         byte in;
  99.        
  100.         while (true) {
  101.             in = input.readByte();
  102.             out |= (in & 0x7F) << (bytes++ * 7);
  103.            
  104.             if (bytes > 32) {
  105.                 throw new RuntimeException("VarInt too big");
  106.             }
  107.            
  108.             if ((in & 0x80) != 0x80) {
  109.                 break;
  110.             }
  111.         }
  112.        
  113.         return out;
  114.     }
  115.    
  116.     private void writeVarInt(int value, DataOutput output) throws IOException {
  117.         int part;
  118.        
  119.         while (true) {
  120.             part = value & 0x7F;
  121.             value >>>= 7;
  122.            
  123.             if (value != 0) {
  124.                 part |= 0x80;
  125.             }
  126.            
  127.             output.writeByte(part);
  128.            
  129.             if (value == 0) {
  130.                 break;
  131.             }
  132.         }
  133.     }
  134.    
  135.     public int getOnlinePlayers() {
  136.         return this.onlinePlayers;
  137.     }
  138.    
  139.     public int getMaxPlayers() {
  140.         return this.maxPlayers;
  141.     }
  142.    
  143.     public String getMotd() {
  144.         return this.motd;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement