KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > irc > Bandwidth


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.event.irc;
19
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import net.sf.drftpd.Bytes;
25 import net.sf.drftpd.master.BaseFtpConnection;
26 import net.sf.drftpd.master.ConnectionManager;
27 import net.sf.drftpd.master.usermanager.NoSuchUserException;
28 import net.sf.drftpd.master.usermanager.User;
29 import net.sf.drftpd.slave.SlaveStatus;
30 import net.sf.drftpd.slave.Transfer;
31 import net.sf.drftpd.util.ReplacerUtils;
32 import net.sf.drftpd.util.Time;
33
34 import org.apache.log4j.Logger;
35 import org.drftpd.plugins.SiteBot;
36 import org.tanesha.replacer.ReplacerEnvironment;
37
38 import f00f.net.irc.martyr.GenericCommandAutoService;
39 import f00f.net.irc.martyr.InCommand;
40 import f00f.net.irc.martyr.commands.MessageCommand;
41
42 /**
43  * @author flowman
44  * @version $Id: Bandwidth.java,v 1.7 2004/04/23 12:18:30 mog Exp $
45  */

46
47 public class Bandwidth
48     extends GenericCommandAutoService
49     implements IRCPluginInterface {
50
51     private static final Logger logger = Logger.getLogger(Bandwidth.class);
52
53     private SiteBot _listener;
54
55     public Bandwidth(SiteBot listener) {
56         super(listener.getIRCConnection());
57         _listener = listener;
58     }
59
60     public String JavaDoc getCommands() {
61         return "!bw !speed";
62     }
63
64     private ConnectionManager getConnectionManager() {
65         return _listener.getConnectionManager();
66     }
67
68     protected void updateCommand(InCommand command) {
69         if (!(command instanceof MessageCommand)) {
70             return;
71         }
72         MessageCommand msgc = (MessageCommand) command;
73         if (msgc.isPrivateToUs(_listener.getIRCConnection().getClientState())) {
74             return;
75         }
76
77         String JavaDoc msg = msgc.getMessage();
78         if (msg.startsWith("!bw")) {
79             SlaveStatus status =
80                 getConnectionManager().getSlaveManager().getAllStatus();
81
82             ReplacerEnvironment env =
83                 new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
84
85             SiteBot.fillEnvSlaveStatus(env, status, _listener.getSlaveManager());
86
87             _listener.sayChannel(msgc.getDest(), ReplacerUtils.jprintf("bw", env, SiteBot.class));
88         } else if (msg.startsWith("!speed ")) {
89             String JavaDoc username;
90             try {
91                 username = msgc.getMessage().substring("!speed ".length());
92             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
93                 logger.warn("", e);
94                 return;
95             } catch (StringIndexOutOfBoundsException JavaDoc e) {
96                 logger.warn("", e);
97                 return;
98             }
99             ReplacerEnvironment env =
100                 new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
101             env.add("user", username);
102             String JavaDoc status =
103                 ReplacerUtils.jprintf("speed.pre", env, SiteBot.class);
104
105             String JavaDoc separator =
106                 ReplacerUtils.jprintf("speed.separator", env, SiteBot.class);
107
108             boolean first = true;
109
110             ArrayList JavaDoc conns =
111                 new ArrayList JavaDoc(getConnectionManager().getConnections());
112             for (Iterator JavaDoc iter = conns.iterator(); iter.hasNext();) {
113                 BaseFtpConnection conn = (BaseFtpConnection) iter.next();
114                 try {
115                     User connUser = conn.getUser();
116                     if (!first) {
117                         status = status + separator;
118                     }
119                     if (connUser.getUsername().equals(username)) {
120
121                         env.add(
122                             "idle",
123                             Time.formatTime(
124                                 System.currentTimeMillis()
125                                     - conn.getLastActive()));
126
127                         if (getConnectionManager()
128                             .getConfig()
129                             .checkHideInWho(
130                                 connUser,
131                                 conn.getCurrentDirectory()))
132                             continue;
133                         first = false;
134                         if (!conn.isExecuting()) {
135                             status
136                                 += ReplacerUtils.jprintf(
137                                     "speed.idle",
138                                     env,
139                                     SiteBot.class);
140
141                         } else if (
142                             conn.getDataConnectionHandler().isTransfering()) {
143                             try {
144                                 env.add(
145                                     "speed",
146                                     Bytes.formatBytes(
147                                         conn
148                                             .getDataConnectionHandler()
149                                             .getTransfer()
150                                             .getXferSpeed())
151                                         + "/s");
152                             } catch (RemoteException JavaDoc e2) {
153                                 logger.warn("", e2);
154                             }
155                             env.add(
156                                 "file",
157                                 conn
158                                     .getDataConnectionHandler()
159                                     .getTransferFile()
160                                     .getName());
161                             env.add(
162                                 "slave",
163                                 conn
164                                     .getDataConnectionHandler()
165                                     .getTranferSlave()
166                                     .getName());
167
168                             if (conn.getTransferDirection()
169                                 == Transfer.TRANSFER_RECEIVING_UPLOAD) {
170                                 status
171                                     += ReplacerUtils.jprintf(
172                                         "speed.up",
173                                         env,
174                                         SiteBot.class);
175
176                             } else if (
177                                 conn.getTransferDirection()
178                                     == Transfer.TRANSFER_SENDING_DOWNLOAD) {
179                                 status
180                                     += ReplacerUtils.jprintf(
181                                         "speed.down",
182                                         env,
183                                         SiteBot.class);
184                             }
185                         }
186                     }
187                 } catch (NoSuchUserException e) {
188                     //just continue.. we aren't interested in connections without logged-in users
189
}
190             } // for
191
status += ReplacerUtils.jprintf("speed.post", env, SiteBot.class);
192             if (first) {
193                 status =
194                     ReplacerUtils.jprintf("speed.error", env, SiteBot.class);
195             }
196             _listener.sayChannel(msgc.getDest(), status);
197         }
198     }
199 }
200
Popular Tags