Package mncservermon

Source Code of mncservermon.MNCServer

package mncservermon;

import java.net.*;
import java.io.*;
import org.codehaus.jettison.json.*;
import org.jasypt.util.text.BasicTextEncryptor;

/* This class defines a MNC server object by IP, port, login and password, and gives methods to access the implemented API commands
*
* Syntax: MNCServer (String IP, int port, String login, String password)
*
*/
class MNCServer {

    final String ip;
    final int port;
    JSONArray players;
    JSONObject status;
    private BasicTextEncryptor encryptor;

    MNCServer(final String newip,
            final int newport,
            final String newlogin,
            final String newpwd) {

        encryptor = new BasicTextEncryptor();
        encryptor.setPassword("XQ1kg3L3zjPEnZZTtu01dohfPEyiwW0TzrUVwiPcjuZKIgVxD8qRnvK64j5xJsq");

        Authenticator.setDefault(new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(newlogin,
                        encryptor.decrypt(newpwd).toCharArray());
            }
        });
        ip = newip;
        port = newport;
    }
   
    /*
     * Does the http request to the server, and returns the result as a JSONObject.
     */
    JSONObject mncQuery(String command) throws IOException, MalformedURLException, JSONException {
        URL server = new URL("http", this.ip, this.port, "/" + command);
        URLConnection Connection = server.openConnection();
        InputStream input = Connection.getInputStream();
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(input, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            input.close();
        }
        String txt = writer.toString();
        writer.close();
        JSONObject data = new JSONObject(txt);
        return data;
    }

    /*
     * Light weight version of mncQuery for when we don't expect an answer.
     */
    void mncAction(String command) throws IOException, MalformedURLException, JSONException {
        URL server = new URL("http", this.ip, this.port, "/" + command);
        URLConnection Connection = server.openConnection();
        Connection.getInputStream();
    }

    /*
     * Gets the server's players list.
     */
    JSONArray mncPlayerList() throws JSONException, IOException {
        JSONObject data = this.mncQuery("playerlist");
        players = new JSONArray(data.get("PlayerList").toString());
        return players;
    }

    /*
     * Gets a player's details, identified by his Steam id.
     */
    JSONObject mncPlayerDetails(String player) throws JSONException, IOException {
        JSONObject obj = this.mncQuery("playerdetails?id=" + player);
        JSONObject details = new JSONObject(obj.get("PlayerDetails").toString());
        return details;
    }

    /*
     * Kicks a player, identified by his Steam id.
     */
    void mncPlayerKick(String player) throws JSONException, IOException {
        this.mncAction("kick?id=" + player);
    }

    /*
     * Bans a player, identified by his Steam id.
     */
    void mncPlayerBan(String player) throws JSONException, IOException {
        this.mncAction("ban?id=" + player);
    }

    /*
     * Gets the server's status infos.
     */
    JSONObject mncServerStatus() throws JSONException, IOException {
        status = this.mncQuery("serverstatus");
        JSONObject result = this.status.getJSONObject("ServerStatus");
        return result;
    }

    /*
     * Gets the chat history (broken?). Takes an integer as argument which is the timestamp from which the chat history must be retrieved?
     */
    JSONArray mncChatHistory(int time) throws JSONException, IOException {
        JSONObject obj = this.mncQuery("chathistory?time=" + time);
        JSONArray chat = obj.getJSONArray("ChatHistory");
        return chat;
    }

    /*
     * Sends a message to the server's chat.
     */
    void mncChatSend(String line) throws JSONException, IOException {
        this.mncAction("sendchat?msg=" + URLEncoder.encode(line, "UTF-8"));
    }

    /*
     * Executes a console command on the server.
     */
    JSONObject mncConsole(String line) throws JSONException, IOException {
        String fline = URLEncoder.encode("/" + line, "UTF-8");
        JSONObject result = this.mncQuery("console?cmd=" + fline);
        return result;
    }
}
TOP

Related Classes of mncservermon.MNCServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.