KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > listeners > GlftpdLog


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.listeners;
19
20 // import java.io.FileInputStream;
21
import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Locale JavaDoc;
35 // import java.util.Properties;
36

37 import net.sf.drftpd.FatalException;
38 import net.sf.drftpd.NoAvailableSlaveException;
39 import net.sf.drftpd.Nukee;
40 import net.sf.drftpd.SFVFile;
41 import net.sf.drftpd.SlaveUnavailableException;
42 import net.sf.drftpd.SFVFile.SFVStatus;
43 import net.sf.drftpd.event.DirectoryFtpEvent;
44 import net.sf.drftpd.event.Event;
45 import net.sf.drftpd.event.FtpListener;
46 import net.sf.drftpd.event.InviteEvent;
47 import net.sf.drftpd.event.MessageEvent;
48 import net.sf.drftpd.event.NukeEvent;
49 import net.sf.drftpd.event.SlaveEvent;
50 import net.sf.drftpd.event.TransferEvent;
51 import net.sf.drftpd.master.ConnectionManager;
52 import net.sf.drftpd.master.GroupPosition;
53 import net.sf.drftpd.master.UploaderPosition;
54 import net.sf.drftpd.master.command.plugins.Nuke;
55 // import net.sf.drftpd.master.config.ExcludePath;
56
// import net.sf.drftpd.master.config.FtpConfig;
57
import net.sf.drftpd.master.usermanager.NoSuchUserException;
58 import net.sf.drftpd.master.usermanager.User;
59 import net.sf.drftpd.master.usermanager.UserFileException;
60 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface;
61 import net.sf.drftpd.slave.SlaveStatus;
62
63 import org.apache.log4j.Level;
64 import org.apache.log4j.Logger;
65 import org.tanesha.replacer.FormatterException;
66
67 /**
68  * @author flowman
69  * @version $Id: GlftpdLog.java,v 1.7 2004/05/12 00:45:05 mog Exp $
70  */

71 public class GlftpdLog implements FtpListener {
72     PrintWriter JavaDoc out;
73     
74     private ConnectionManager _cm;
75     
76     private static Logger logger = Logger.getLogger(GlftpdLog.class);
77     static {
78         logger.setLevel(Level.ALL);
79     }
80
81     public GlftpdLog() throws UnknownHostException JavaDoc, IOException JavaDoc {
82         reload();
83         logger.info("Glftpd plugin loaded successfully");
84         out = new PrintWriter JavaDoc(new FileWriter JavaDoc("logs/glftpd.log"));
85     }
86     
87     public void init(ConnectionManager mgr) {
88         _cm = mgr;
89     }
90     
91     public void actionPerformed(Event event) {
92         try {
93             if (event instanceof DirectoryFtpEvent) {
94                 actionPerformedDirectory((DirectoryFtpEvent) event);
95             } else if (event instanceof NukeEvent) {
96                 actionPerformedNuke((NukeEvent) event);
97             } else if (event instanceof SlaveEvent) {
98                 actionPerformedSlave((SlaveEvent) event);
99             } else if (event instanceof InviteEvent) {
100                 actionPerformedInvite((InviteEvent) event);
101             } else if (event.getCommand().equals("SHUTDOWN")) {
102                 MessageEvent mevent = (MessageEvent) event;
103                 print(
104                     "SHUTDOWN: \""
105                         + mevent.getMessage()
106                         + "\"");
107             }
108         } catch (FormatterException ex) {
109         }
110     }
111
112     private void actionPerformedDirectory(DirectoryFtpEvent direvent)
113         throws FormatterException {
114
115         if ("MKD".equals(direvent.getCommand())) {
116             sayDirectorySection(direvent, "NEWDIR", direvent.getDirectory());
117         } else if ("REQUEST".equals(direvent.getCommand())) {
118             sayDirectorySection(direvent, "REQUEST", direvent.getDirectory());
119         } else if ("REQFILLED".equals(direvent.getCommand())) {
120             sayDirectorySection(direvent, "REQFILLED", direvent.getDirectory());
121         } else if ("RMD".equals(direvent.getCommand())) {
122             sayDirectorySection(direvent, "DELDIR", direvent.getDirectory());
123         } else if ("WIPE".equals(direvent.getCommand())) {
124             if (direvent.getDirectory().isDirectory()) {
125                 sayDirectorySection(direvent, "WIPE", direvent.getDirectory());
126             }
127 /* } else if ("PRE".equals(direvent.getCommand())) {
128
129             Ret obj = getPropertyFileSuffix("PRE", direvent.getDirectory());
130             String format = obj.format;
131             LinkedRemoteFile dir = obj.section;
132
133             ReplacerEnvironment env = new ReplacerEnvironment(globalEnv);
134             fillEnvSection(env, direvent, dir);
135
136             say(SimplePrintf.jprintf(format, env));
137 */

138         } else if (direvent.getCommand().equals("STOR")) {
139             actionPerformedDirectorySTOR((TransferEvent)direvent);
140         } else {
141             // Unhandled DirectoryEvent:
142
}
143     }
144         
145     private void sayDirectorySection(DirectoryFtpEvent direvent, String JavaDoc string, LinkedRemoteFileInterface dir)
146         throws FormatterException {
147
148         // TYPE = NEWDIR DELDIR WIPE
149
// TYPE: "/path/to/release" "username" "group" "tagline"
150
print(
151             ""
152                 + string
153                 + ": \""
154                 + dir.getPath()
155                 + "\" \""
156                 + direvent.getUser().getUsername()
157                 + "\" \""
158                 + direvent.getUser().getGroupName()
159                 + "\" \""
160                 + direvent.getUser().getTagline()
161                 + "\"");
162     }
163         
164     private void actionPerformedDirectorySTOR(TransferEvent direvent)
165         throws FormatterException {
166
167         if(!direvent.isComplete()) return;
168         LinkedRemoteFileInterface dir;
169         try {
170             dir = direvent.getDirectory().getParentFile();
171         } catch (FileNotFoundException JavaDoc e) {
172             throw new FatalException(e);
173         }
174         SFVFile sfvfile;
175         try {
176             sfvfile = dir.lookupSFVFile();
177             // throws IOException, ObjectNotFoundException, NoAvailableSlaveException
178
} catch (FileNotFoundException JavaDoc ex) {
179             // No sfv file in in dir
180
return;
181         } catch (NoAvailableSlaveException e) {
182             // No available slave with .sfv
183
return;
184         } catch (IOException JavaDoc e) {
185             // IO error reading .sfv
186
return;
187         }
188
189         long starttime = Long.MAX_VALUE;
190         for (Iterator JavaDoc iter = sfvfile.getFiles().iterator(); iter.hasNext();) {
191             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
192             if (file.lastModified() < starttime)
193                 starttime = file.lastModified();
194         }
195
196         if (!sfvfile.hasFile(direvent.getDirectory().getName()))
197             return;
198
199         int halfway = (int) Math.floor((double) sfvfile.size() / 2);
200         ///// start ///// start ////
201

202         //check if new racer
203
String JavaDoc username = direvent.getUser().getUsername();
204         SFVStatus sfvstatus = sfvfile.getStatus();
205         if (sfvfile.size() - sfvstatus.getMissing() != 1) {
206             for (Iterator JavaDoc iter = sfvfile.getFiles().iterator();
207                 iter.hasNext();
208                 ) {
209                 LinkedRemoteFileInterface sfvFileEntry = (LinkedRemoteFileInterface) iter.next();
210                 if (sfvFileEntry == direvent.getDirectory())
211                     continue;
212                 if (sfvFileEntry.getUsername().equals(username))
213                     break;
214                 if (!iter.hasNext()) {
215
216                     // RACE: "/path/to/release" "new_racer_username" "new_racer_group"
217
// "whois_he_racing" "new_racers_speed" "files_left" "time_raced"
218
print(
219                         "RACE: \""
220                             + dir.getPath()
221                             + "\" \""
222                             + direvent.getUser().getUsername()
223                             + "\" \""
224                             + direvent.getUser().getGroupName()
225                             + "\" \""
226                             + sfvfile.getXferspeed()
227                             + "\" \""
228                             + Integer.toString(sfvstatus.getMissing())
229                             + "\" \""
230                             + Long.toString((direvent.getTime() - starttime) / 1000)
231                             + "\"");
232                 }
233             }
234         }
235
236         //COMPLETE
237
if (sfvstatus.isFinished()) {
238             Collection JavaDoc racers = UserSort(sfvfile.getFiles(), "bytes", "high");
239             Collection JavaDoc groups = topFileGroup(sfvfile.getFiles());
240             Collection JavaDoc fast = UserSort(sfvfile.getFiles(), "xfearspeed", "high");
241             Collection JavaDoc slow = UserSort(sfvfile.getFiles(), "xfearspeed", "low");
242             
243             UploaderPosition fastestuser = (UploaderPosition) fast.iterator().next();
244             UploaderPosition slowestuser = (UploaderPosition) slow.iterator().next();
245                 
246             User fastuser;
247             User slowuser;
248             try {
249                 fastuser = _cm.getUserManager().getUserByName(fastestuser.getUsername());
250                 slowuser = _cm.getUserManager().getUserByName(slowestuser.getUsername());
251             } catch (NoSuchUserException e2) {
252                 return;
253             } catch (UserFileException e2) {
254                 logger.log(Level.FATAL, "Error reading userfile", e2);
255                 return;
256             }
257             
258             //COMPLETERACE: "/path/to/release" "release_size" "release_files" "release_avrage_speed"
259
// "release_total_upload_time" "number_of_racers" "number_of_racing_groups"
260
// "fastest_uploader_username" "fastest_uploader_group" "fastest_uploaders_speed"
261
// "slowest_uploader_username" "slowest_uploader_group" "slowest_uploaders_speed"
262
print(
263                 "COMPLETE: \""
264                     + dir.getPath()
265                     + "\" \""
266                     + sfvfile.getTotalBytes()
267                     + "\" \""
268                     + Integer.toString(sfvfile.size())
269                     + "\" \""
270                     + sfvfile.getXferspeed()
271                     + "\" \""
272                     + Long.toString((direvent.getTime() - starttime) / 1000)
273                     + "\" \""
274                     + Integer.toString(racers.size())
275                     + "\" \""
276                     + Integer.toString(groups.size())
277                     + "\" \""
278                     + fastuser.getUsername()
279                     + "\" \""
280                     + fastuser.getGroupName()
281                     + "\" \""
282                     + fastestuser.getXferspeed()
283                     + "\" \""
284                     + slowuser.getUsername()
285                     + "\" \""
286                     + slowuser.getGroupName()
287                     + "\" \""
288                     + slowestuser.getXferspeed()
289                     + "\"");
290             
291             print(
292                 "STATS: \""
293                     + dir.getPath()
294                     + "\" \"UserTop:\"");
295             
296             int position = 1;
297             for (Iterator JavaDoc iter = racers.iterator(); iter.hasNext();) {
298                 UploaderPosition stat = (UploaderPosition) iter.next();
299
300                 User raceuser;
301                 try {
302                     raceuser = _cm.getUserManager().getUserByName(stat.getUsername());
303                 } catch (NoSuchUserException e2) {
304                     continue;
305                 } catch (UserFileException e2) {
306                     logger.log(Level.FATAL, "Error reading userfile", e2);
307                     continue;
308                 }
309                 
310                 // STATSUSER: "/path/to/release" "race_place" "username" "group" "mb_uploaded"
311
// "files_uploaded" "percent_uploaded" "avrage_speed"
312
print(
313                     "STATSUSER: \""
314                         + dir.getPath()
315                         + "\" \""
316                         + new Integer JavaDoc(position++)
317                         + "\" \""
318                         + raceuser.getUsername()
319                         + "\" \""
320                         + raceuser.getGroupName()
321                         + "\" \""
322                         + stat.getBytes()
323                         + "\" \""
324                         + Integer.toString(stat.getFiles())
325                         + "\" \""
326                         + Integer.toString(stat.getFiles() * 100 / sfvfile.size())
327                         + "\" \""
328                         + stat.getXferspeed()
329                         + "\"");
330             }
331             
332             print(
333                 "STATS: \""
334                     + dir.getPath()
335                     + "\" \"GroupTop:\"");
336                     
337             position = 1;
338             for (Iterator JavaDoc iter = groups.iterator(); iter.hasNext();) {
339                 GroupPosition stat = (GroupPosition) iter.next();
340
341                 // STATSGROUP: "/path/to/release" "race_place" "group" "mb_uploaded"
342
// "files_uploaded" "percent_uploaded" "avrage_speed"
343
print(
344                     "STATSGROUP: \""
345                         + dir.getPath()
346                         + "\" \""
347                         + new Integer JavaDoc(position++)
348                         + "\" \""
349                         + stat.getGroupname()
350                         + "\" \""
351                         + stat.getBytes()
352                         + "\" \""
353                         + Integer.toString(stat.getFiles())
354                         + "\" \""
355                         + Integer.toString(stat.getFiles() * 100 / sfvfile.size())
356                         + "\" \""
357                         + stat.getXferspeed()
358                         + "\"");
359             }
360         //HALFWAY
361
} else if (sfvfile.size() >= 4 && sfvstatus.getMissing() == halfway) {
362             Collection JavaDoc uploaders = UserSort(sfvfile.getFiles(), "bytes", "high");
363             UploaderPosition stat =
364                 (UploaderPosition) uploaders.iterator().next();
365
366             User leaduser;
367             try {
368                 leaduser =
369                     _cm.getUserManager().getUserByName(stat.getUsername());
370             } catch (NoSuchUserException e3) {
371                 return;
372             } catch (UserFileException e3) {
373                 logger.log(Level.FATAL, "Error reading userfile", e3);
374                 return;
375             }
376             
377             // HALFWAY: "/path/to/release" "leading_username" "group" "mb_uploaded"
378
// "files_uploaded" "percent_uploaded" "avrage_speed" "files_left"
379
print(
380                 "HALFWAY: \""
381                     + dir.getPath()
382                     + "\" \""
383                     + leaduser.getUsername()
384                     + "\" \""
385                     + leaduser.getGroupName()
386                     + "\" \""
387                     + stat.getBytes()
388                     + "\" \""
389                     + Integer.toString(stat.getFiles())
390                     + "\" \""
391                     + Integer.toString(stat.getFiles() * 100 / sfvfile.size())
392                     + "\" \""
393                     + stat.getXferspeed()
394                     + "\" \""
395                     + Integer.toString(sfvstatus.getMissing())
396                     + "\"");
397         }
398     }
399     
400     private void actionPerformedSlave(SlaveEvent event)
401         throws FormatterException {
402         SlaveEvent sevent = (SlaveEvent) event;
403
404         if (event.getCommand().equals("ADDSLAVE")) {
405             SlaveStatus status;
406             try {
407                 status = sevent.getRSlave().getStatus();
408             } catch (SlaveUnavailableException e) {
409                 return;
410             }
411
412             print(
413                     "SLAVEONLINE: \""
414                         + sevent.getRSlave().getName()
415                         + "\" \""
416                         + sevent.getMessage()
417                         + "\" \""
418                         + status.getDiskSpaceCapacity()
419                         + "\" \""
420                         + status.getDiskSpaceAvailable()
421                         + "\"");
422
423         } else if (event.getCommand().equals("DELSLAVE")) {
424             print(
425                     "SLAVEOFFLINE: \""
426                         + sevent.getRSlave().getName()
427                         + "\"");
428         }
429     }
430     
431     private void actionPerformedInvite(InviteEvent event) {
432         String JavaDoc user = event.getUser();
433             print(
434                     "INVITE: \""
435                         + user
436                         + "\"");
437     }
438
439     private void actionPerformedNuke(NukeEvent event)
440         throws FormatterException {
441         String JavaDoc cmd = event.getCommand();
442     
443         if (cmd.equals("NUKE")) {
444             
445             print(
446                 "NUKE: \""
447                     + event.getPath()
448                     + "\" \""
449                     + event.getUser().getUsername()
450                     + "\" \""
451                     + event.getUser().getGroupName()
452                     + "\" \""
453                     + String.valueOf(event.getMultiplier())
454                     + " "
455                     + event.getSize()
456                     + "\" \""
457                     + event.getReason()
458                     + "\"");
459                     
460             int position = 1;
461             long nobodyAmount = 0;
462             for (Iterator JavaDoc iter = event.getNukees2().iterator();
463                 iter.hasNext();) {
464                 Nukee stat = (Nukee) iter.next();
465
466                 User raceuser;
467                 try {
468                     raceuser =
469                         _cm.getUserManager().getUserByName(stat.getUsername());
470                 } catch (NoSuchUserException e2) {
471                     nobodyAmount += stat.getAmount();
472                     continue;
473                 } catch (UserFileException e2) {
474                     logger.log(Level.FATAL, "Error reading userfile", e2);
475                     continue;
476                 }
477                 
478                 long nukedamount =
479                     Nuke.calculateNukedAmount(
480                         stat.getAmount(),
481                         raceuser.getRatio(),
482                         event.getMultiplier());
483                 
484                 print(
485                     "NUKEE: \""
486                         + raceuser.getUsername()
487                         + "\" \""
488                         + raceuser.getGroupName()
489                         + "\" \""
490                         + position++
491                         + "\" \""
492                         + stat.getAmount()
493                         + " "
494                         + nukedamount
495                         + "\"");
496     
497             }
498             if (nobodyAmount != 0) {
499                 
500                 print(
501                     "NUKEE: \""
502                         + "nobody"
503                         + "\" \""
504                         + "nogroup"
505                         + "\" \""
506                         + "?"
507                         + "\" \""
508                         + nobodyAmount
509                         + " "
510                         + nobodyAmount
511                         + "\"");
512     
513             }
514         } else if (cmd.equals("UNNUKE")) {
515     
516             print(
517                 "UNNUKE: \""
518                     + event.getPath()
519                     + "\" \""
520                     + event.getUser().getUsername()
521                     + "\" \""
522                     + event.getUser().getGroupName()
523                     + "\" \""
524                     + String.valueOf(event.getMultiplier())
525                     + " "
526                     + event.getSize()
527                     + "\" \""
528                     + event.getReason()
529                     + "\"");
530         }
531     }
532     
533     public static Collection JavaDoc topFileGroup(Collection JavaDoc files) {
534         ArrayList JavaDoc ret = new ArrayList JavaDoc();
535         for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
536             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
537             String JavaDoc groupname = file.getGroupname();
538     
539             GroupPosition stat = null;
540             for (Iterator JavaDoc iter2 = ret.iterator(); iter2.hasNext();) {
541                 GroupPosition stat2 = (GroupPosition) iter2.next();
542                 if (stat2.getGroupname().equals(groupname)) {
543                     stat = stat2;
544                     break;
545                 }
546             }
547             if (stat == null) {
548                 stat =
549                     new GroupPosition(
550                         groupname,
551                         file.length(),
552                         1,
553                         file.getXfertime());
554                 ret.add(stat);
555             } else {
556                 stat.updateBytes(file.length());
557                 stat.updateFiles(1);
558                 stat.updateXfertime(file.getXfertime());
559             }
560         }
561         Collections.sort(ret);
562         return ret;
563     }
564         
565     public static Collection JavaDoc UserSort(Collection JavaDoc files, String JavaDoc type, String JavaDoc sort) {
566         ArrayList JavaDoc ret = new ArrayList JavaDoc();
567         for (Iterator JavaDoc iter = files.iterator(); iter.hasNext();) {
568             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
569             String JavaDoc username = file.getUsername();
570     
571             UploaderPosition stat = null;
572             for (Iterator JavaDoc iter2 = ret.iterator(); iter2.hasNext();) {
573                 UploaderPosition stat2 = (UploaderPosition) iter2.next();
574                 if (stat2.getUsername().equals(username)) {
575                     stat = stat2;
576                     break;
577                 }
578             }
579             if (stat == null) {
580                 stat =
581                     new UploaderPosition(
582                         username,
583                         file.length(),
584                         1,
585                         file.getXfertime());
586                 ret.add(stat);
587             } else {
588                 stat.updateBytes(file.length());
589                 stat.updateFiles(1);
590                 stat.updateXfertime(file.getXfertime());
591             }
592         }
593         Collections.sort(ret, new UserComparator(type, sort));
594         return ret;
595     }
596     
597     DateFormat JavaDoc DATE_FMT = new SimpleDateFormat JavaDoc("EEE MMM d HH:mm:ss yyyy ", Locale.ENGLISH);
598     
599     public void print(String JavaDoc line) {
600         print(new Date JavaDoc(), line);
601     }
602     
603     public void print(Date JavaDoc date, String JavaDoc line) {
604         out.println(DATE_FMT.format(date) + line);
605         out.flush();
606     }
607
608     private void reload() {
609 /** Properties props = new Properties();
610         try {
611             props.load(new FileInputStream("conf/glftpd.conf"));
612         } catch (IOException e) {
613             throw new RuntimeException(e);
614         }
615 */
}
616
617     public void unload() {
618     }
619 }
620
621 class UserComparator implements Comparator JavaDoc {
622     private String JavaDoc _type;
623     private String JavaDoc _sort;
624     
625     public UserComparator(String JavaDoc type, String JavaDoc sort) {
626         _type = type;
627         _sort = sort;
628     }
629     
630     static long getType(String JavaDoc type, UploaderPosition user) {
631         if (type.equals("bytes")) {
632             return user.getBytes();
633         } else if (type.equals("xfearspeed")) {
634             return user.getXferspeed();
635         } else if (type.equals("xfeartime")) {
636             return user.getXfertime();
637         }
638         return 0;
639     }
640
641     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
642         UploaderPosition u1 = (UploaderPosition) o1;
643         UploaderPosition u2 = (UploaderPosition) o2;
644
645         long thisVal = getType(_type, u1);
646         long anotherVal = getType(_type, u2);
647         if (_sort.equals("low")) {
648             return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
649         } else {
650             return (thisVal > anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
651         }
652     }
653 }
Popular Tags