2 * Copyright 2011 Huize Dai
4 * This file is part of MFW.
6 * MFW is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * MFW is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with MFW. If not, see <http://www.gnu.org/licenses/>.
21 import java
.io
.BufferedReader
;
22 import java
.io
.IOException
;
23 import java
.io
.InputStreamReader
;
24 import java
.net
.InetSocketAddress
;
25 import java
.nio
.channels
.SelectionKey
;
26 import java
.util
.HashSet
;
27 import java
.util
.Iterator
;
30 import javax
.xml
.parsers
.DocumentBuilderFactory
;
31 import mfw
.buff
.CommandReceiver
;
32 import mfw
.buff
.Returnable
;
33 import mfw
.ctrl
.ReceiverFactory
;
34 import mfw
.ctrl
.Trigger
;
35 import mfw
.ctrl
.Cmd
.Parameter
;
38 import mfw
.pool
.Mission
;
39 import mfw
.timer
.ClockThread
;
40 import mfw
.timer
.TimerEvent
;
41 import mfw
.timer
.TimerEventGroup
;
43 public class Telnet_ChatRoom
{
44 private static boolean flag
= true;
45 private static Server srv
= new Server();
46 private static CommandReceiver chatReceiver
;
47 private static CommandReceiver adminReceiver
;
50 chatReceiver
= ReceiverFactory
51 .newReceiverInstance(DocumentBuilderFactory
56 .getSystemResourceAsStream("chat/chat_cmd.xml"))
57 .getDocumentElement());
58 adminReceiver
= ReceiverFactory
59 .newReceiverInstance(DocumentBuilderFactory
64 .getSystemResourceAsStream("chat/admin_cmd.xml"))
65 .getDocumentElement());
66 } catch (Exception e
) {
71 private static Returnable admin
= new Returnable() {
73 public boolean isReturnable() {
77 public void returnResult(byte[] result
, int offset
, int len
) {
78 System
.out
.print(new String(result
, offset
, len
));
82 public static Set
<Client
> list
= new HashSet
<Client
>();
84 private static TimerEventGroup timer
= new TimerEventGroup();
86 private static byte[] content
= new byte[0];
88 public static void main(String
[] args
) throws IOException
{
90 new ClockThread(timer
, 30000).start();
91 timer
.add(new CleanTimer());
92 timer
.add(new PubTimer());
93 srv
.setWelcomeStr("Welcome!\r\n".getBytes());
94 srv
.setAddress(new InetSocketAddress(8081));
95 srv
.setCmdLength(256);
96 srv
.setCoreReceiver(chatReceiver
);
98 BufferedReader in
= new BufferedReader(new InputStreamReader(System
.in
));
100 byte[] cmd
= in
.readLine().getBytes();
101 adminReceiver
.receive(admin
, cmd
, 0, cmd
.length
);
108 public static class ListSockets
extends Trigger
{
110 protected void execute(Returnable subject
, Parameter param
) {
111 Iterator
<SelectionKey
> iter
= srv
.getKeys().iterator();
112 while (iter
.hasNext()) {
113 byte[] socket
= (iter
.next().channel().toString() + "\r\n")
115 subject
.returnResult(socket
, 0, socket
.length
);
120 public static class ListClients
extends Trigger
{
122 protected void execute(Returnable subject
, Parameter param
) {
123 Iterator
<Client
> iter
= list
.iterator();
124 while (iter
.hasNext()) {
125 byte[] client
= (iter
.next().attachment().toString() + "\r\n")
127 subject
.returnResult(client
, 0, client
.length
);
132 public static class StopClient
extends Trigger
{
134 protected void execute(Returnable subject
, Parameter param
) {
135 byte[] warning
= "Warning!!!\r\n".getBytes();
136 String nick
= new String(param
.getBytes());
137 Iterator
<Client
> iter
= list
.iterator();
138 while (iter
.hasNext()) {
139 Client cl
= iter
.next();
140 if (cl
.attachment().toString().equals(nick
)) {
141 cl
.returnResult(warning
, 0, warning
.length
);
149 public static class Exit
extends Trigger
{
151 protected void execute(Returnable subject
, Parameter param
) {
152 byte[] bye
= "GoodBye!\r\n".getBytes();
153 subject
.returnResult(bye
, 0, bye
.length
);
158 public static class Pub
extends Trigger
{
160 protected void execute(Returnable subject
, Parameter param
) {
161 content
= param
.getBytes();
165 static class PubTimer
extends TimerEvent
{
167 protected boolean isContinue() {
168 Iterator
<Client
> iter
= list
.iterator();
169 while (iter
.hasNext()) {
170 iter
.next().returnResult(content
, 0, content
.length
);
176 static class CleanTimer
extends TimerEvent
{
178 protected boolean isContinue() {
179 Iterator
<Client
> iter
= list
.iterator();
180 while (iter
.hasNext()) {
181 if (!iter
.next().isReturnable())