1 package rero.dialogs.server; 2 3 import java.util.*; 4 import java.util.regex.*; 5 6 import rero.util.*; 7 8 public class Server implements Comparable 9 { 10 13 16 protected static Pattern isServerPassword = Pattern.compile("(\\S)\\S*=(.*)SERVER:(.*):(.*):(.*)GROUP:(.*)"); 17 protected static Pattern isServerNormal = Pattern.compile("(\\S)\\S*=(.*)SERVER:(.*):(.*)GROUP:(.*)"); 18 19 protected String description; 20 protected String host; 21 protected String portRange; 22 protected String network; 23 protected boolean isSSL; 24 protected String password; 25 protected String compare; 26 27 public void setValues(String d, String h, String r, String n, boolean s, String p) 28 { 29 description = d; 30 host = h; 31 portRange = r; 32 network = n; 33 isSSL = s; 34 password = p; 35 36 compare = n.toUpperCase() + host.toUpperCase(); 37 } 38 39 public Server() { } 40 41 public Server(String d, String h, String r, String n, boolean s, String p) 42 { 43 setValues(d, h, r, n, s, p); 44 } 45 46 public String toString() 47 { 48 return toString(0); 49 } 51 52 public boolean isRandom() 53 { 54 return getNetwork() == null || getNetwork().length() <= 2; 55 } 56 57 public String getCompare() { return compare; } 58 59 public int compareTo(Object o) 60 { 61 Server arg = (Server)o; 62 63 return getCompare().compareTo(arg.getCompare()); 64 } 65 66 public String getPassword() 67 { 68 return password; 69 } 70 71 public String getPorts() 72 { 73 return portRange; 74 } 75 76 public String getHost() 77 { 78 return host; 79 } 80 81 public String getConnectPort() 82 { 83 String myPort = portRange; 84 85 if (myPort.indexOf("-") > -1) 86 { 87 myPort = myPort.substring(0, myPort.indexOf("-")); 88 } 89 90 if (myPort.indexOf(",") > -1) 91 { 92 myPort = myPort.substring(0, myPort.indexOf(",")); 93 } 94 95 return myPort.trim(); 96 } 97 98 public boolean isSecure() 99 { 100 return isSSL; 101 } 102 103 public String getDescription() 104 { 105 return description; 106 } 107 108 public String getNetwork() 109 { 110 return network; 111 } 112 113 public String getCommand() 114 { 115 StringBuffer command = new StringBuffer ("/server "); 116 117 if (isSecure()) 118 { 119 command.append("-ssl "); 120 } 121 122 if (getPassword() != null && getPassword().length() > 0) 123 { 124 command.append("-pass "); 125 command.append(getPassword()); 126 command.append(" "); 127 } 128 129 command.append(getHost()); 130 command.append(" "); 131 command.append(getConnectPort()); 132 133 return command.toString(); 134 } 135 136 public String toString(int x) 137 { 138 StringBuffer value = new StringBuffer (); 139 140 if (isSSL) 141 { 142 value.append('s'); 143 } 144 else 145 { 146 value.append('n'); 147 } 148 149 value.append(x); 150 value.append('='); 151 value.append(description); 152 value.append("SERVER:"); 153 value.append(host); 154 value.append(":"); 155 value.append(portRange); 156 157 if (password != null && password.length() > 0) 158 { 159 value.append(":"); 160 value.append(password); 161 } 162 163 value.append("GROUP:"); 164 value.append(network); 165 166 return value.toString(); 167 } 168 169 public static Server decode(String text) 170 { 171 StringParser check = new StringParser(text, isServerPassword); 172 173 if (check.matches()) 174 { 175 String [] values = check.getParsedStrings(); 176 177 boolean secure = values[0].charAt(0) == 's'; 178 179 return new Server(values[1], values[2], values[3], values[5], secure, values[4]); 180 } 181 182 check = new StringParser(text, isServerNormal); 183 184 if (check.matches()) 185 { 186 192 String [] values = check.getParsedStrings(); 193 194 boolean secure = values[0].charAt(0) == 's'; 195 196 return new Server(values[1], values[2], values[3], values[4], secure, null); 197 } 198 199 return null; 200 } 201 } 202 | Popular Tags |