You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.4 KiB

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.time.Clock;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
* Created by itsmy on 08.08.2016.
*/
public class Console extends Thread{
private BufferedReader in;
private String line;
private static Boolean hide = false;
public void consoleListner() throws IOException{
in = new BufferedReader(new InputStreamReader(System.in));
line = "";
start();
}
public static void out(String parent, String msg){
if(hide){return;}
String out = "[";
out += Clock.systemUTC().instant().toString();
out += "|";
out += parent;
out += "]: ";
out += msg;
System.out.println(out);
}
public static void socket(Integer socketId, String msg){
if(hide){return;}
String out = "[";
out += Clock.systemUTC().instant().toString();
out += "|Socket ";
out += socketId;
out += "]: ";
out += msg;
System.out.println(out);
}
public static void debug(String msg){
if(hide){return;}
String out = "[";
out += Clock.systemUTC().instant().toString();
out += "|DEBUG]: ";
out += msg;
System.out.println(out);
}
public void run() {
Console.out("Server", "Welcome to the CtServer[BUILD 030]");
while (line.equalsIgnoreCase("/stop") == false) {
try{
line = in.readLine();
Pattern main = Pattern.compile(" ");
String[] sx = main.split(line);
switch (sx[0]){
case "/list":
cList();
break;
case "/kick":
cKick(sx);
break;
case "/send":
cSend(sx);
break;
case "/info":
cInfo(sx);
break;
case "/help":
cHelp();
break;
case "/hide":
cHide();
break;
case "/restart":
cRestart();
break;
default: Console.out("Server","Введите /help для помощи");
}
}catch (IOException e){
Console.out("Console","Something goes wrong...");
}catch (NumberFormatException e){
Console.out("Console","Hey guy, don't be so crazy!!!");
}catch (ctException e){
}
}
}
private void cHide() {
if(!hide){hide = true;} else {hide = false;}
}
private void cList() throws ctException {
Integer sid;
Console.out("Server","/list");
for(int i = 0; SocketMaps.getAllClients().size() > i; i++){
sid = SocketMaps.getAllClients().get(i);
if(SocketMaps.getInfo(sid).PROTOCOL == "1A") {
Console.socket(sid,"VKID " + SocketMaps.getInfo(sid).VKID
+ " PROTOCOL " + SocketMaps.getInfo(sid).PROTOCOL
+ " EVENT " + SocketMaps.getEID(sid)
+ " PARTYID " + SocketMaps.getPID(sid)
+ " TIME " + SocketMaps.getInfo(sid).TIME
+ " TRACKDURATION " + SocketMaps.getInfo(sid).DURATION);
}else{
Console.socket(sid,"Protocol not checked!");
}
}
}
private void cKick(String[] sx) throws ctException{
Integer sid;
sid = Integer.valueOf(sx[1]);
SocketMaps.getClient(sid).closeSocket();
}
private void cSend(String[] sx) throws ctException {
Integer sid;
sid = Integer.valueOf(sx[1]);
String msg = "";
for (int i = 2; i < sx.length ; i++) {
msg += " ";
msg += sx[i];
}
SocketMaps.getClient(sid).writeSocket(msg);
}
private void cHelp() {
Console.out("Server","/kick [socketid] для отключения сокета от сервера");
Console.out("Server","/list для отображения всех клиентов");
Console.out("Server","/info pid/sid [id] для отображения полной информации о данном клиенте");
Console.out("Server","/send [socketid] [msg] отладочная команда");
Console.out("Server","CtServer Proto Defend Development 2016");
}
private void cInfo(String[] sx) throws ctException {
Integer sid;
if(sx.length < 3){
Console.out("Server","/info [pid/sid] <id>");
} else {
switch (sx[1]) {
case "sid":
sid = Integer.valueOf(sx[2]);
Console.socket(sid,"VKID " + SocketMaps.getInfo(sid).VKID
+ " PROTOCOL " + SocketMaps.getInfo(sid).PROTOCOL
+ " EVENT " + SocketMaps.getEID(sid)
+ " PARTYID " + SocketMaps.getPID(sid)
+ " TRACKDURATION " + SocketMaps.getInfo(sid).DURATION);
break;
case "pid":
Integer pid = Integer.valueOf(sx[2]);
ArrayList<Integer> a = SocketMaps.getAllParty(pid);
for (int i = 0; i < a.size(); i++) {
Console.socket(a.get(i),"VKID " + SocketMaps.getInfo(a.get(i)).VKID
+ " PROTOCOL " + SocketMaps.getInfo(a.get(i)).PROTOCOL
+ " EVENT " + SocketMaps.getEID(a.get(i))
+ " PARTYID " + SocketMaps.getPID(a.get(i))
+ " TRACKDURATION " + SocketMaps.getInfo(a.get(i)).DURATION);
}
break;
default:
Console.out("Server", "/info [pid/sid] <id>");
break;
}
}
}
private void cRestart(){
ArrayList<Integer> TempList= SocketMaps.getAllClients();
for (int i = 0; i < TempList.size(); i++) {
try{
SocketMaps.getClient(TempList.get(i)).writeSocket("{\"EVENT\":\"RESTART\"}");
}catch (ctException ct){}
}
System.exit(99);
}
}