KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dialogs > server > Server


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 JavaDoc
9 {
10    // x0=server descriptionSERVER:host:portrange:passwordGROUP:network
11
// x00=server descriptionSERVER:host:portrangeGROUP:network
12

13    // Server w/ password regex - (\S)\S*=(.*)SERVER:(.*):(.*):(.*)GROUP:(.*)
14
// Server w/o password regex - (\S)\S*=(.*)SERVER:(.*):(.*)GROUP:(.*)
15

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 JavaDoc description;
20    protected String JavaDoc host;
21    protected String JavaDoc portRange;
22    protected String JavaDoc network;
23    protected boolean isSSL;
24    protected String JavaDoc password;
25    protected String JavaDoc compare;
26
27    public void setValues(String JavaDoc d, String JavaDoc h, String JavaDoc r, String JavaDoc n, boolean s, String JavaDoc 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 JavaDoc d, String JavaDoc h, String JavaDoc r, String JavaDoc n, boolean s, String JavaDoc p)
42    {
43        setValues(d, h, r, n, s, p);
44    }
45
46    public String JavaDoc toString()
47    {
48        return toString(0);
49 // return "[Server: " + host + ":" + portRange + ", Desc: " + description + ", Network: " + network + ", Password: " + password + ", Secure? " + isSSL + "]";
50
}
51
52    public boolean isRandom()
53    {
54       return getNetwork() == null || getNetwork().length() <= 2;
55    }
56
57    public String JavaDoc getCompare() { return compare; }
58
59    public int compareTo(Object JavaDoc o)
60    {
61       Server arg = (Server)o;
62
63       return getCompare().compareTo(arg.getCompare());
64    }
65
66    public String JavaDoc getPassword()
67    {
68        return password;
69    }
70
71    public String JavaDoc getPorts()
72    {
73        return portRange;
74    }
75
76    public String JavaDoc getHost()
77    {
78        return host;
79    }
80
81    public String JavaDoc getConnectPort()
82    {
83        String JavaDoc 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 JavaDoc getDescription()
104    {
105        return description;
106    }
107
108    public String JavaDoc getNetwork()
109    {
110        return network;
111    }
112
113    public String JavaDoc getCommand()
114    {
115        StringBuffer JavaDoc command = new StringBuffer JavaDoc("/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 JavaDoc toString(int x)
137    {
138        StringBuffer JavaDoc value = new StringBuffer JavaDoc();
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 JavaDoc text)
170    {
171        StringParser check = new StringParser(text, isServerPassword);
172  
173        if (check.matches())
174        {
175           String JavaDoc[] 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           // 0: s
187
// 1: Random US DALnet server
188
// 2: irc.dal.net
189
// 3: 6660-6667
190
// 4: 01
191

192           String JavaDoc[] 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