KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > zirc > threads > DCCincomingThread


1 package zirc.threads ;
2
3 import java.io.* ;
4 import java.net.* ;
5 import java.util.* ;
6 import java.util.Timer JavaDoc ;
7 import java.awt.* ;
8 import javax.swing.* ;
9 import zirc.base.* ;
10 import zirc.gui.* ;
11
12 //zIrc, irc client.
13
// Copyright (C) 2004 CoolBytes(Stephane Claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com
14
//
15
// This program is free software; you can redistribute it and/or
16
// modify it under the terms of the GNU General Public License
17
// as published by the Free Software Foundation; either version 2
18
// of the License, or (at your option) any later version.
19
//
20
// This program is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
// GNU General Public License for more details.
24

25 /**
26  * <p>Title: DCCincomingThread</p>
27  * <p>Description: thread qui gere le telechargement par dcc</p>
28  * <p>Copyright: Copyright (c) 2004</p>
29  * <p>Company: CoolBytes(Stephane Claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com</p>
30  * @version 1.0
31  */

32
33 public class DCCincomingThread extends Thread JavaDoc
34 {
35   private Socket s ;
36   RandomAccessFile file ;
37   String JavaDoc sender ;
38   int amount ;
39   int offset;
40   int port;
41   JLabel labelInfo ;
42   int transferred ;
43   int speed = 0 ;
44   DataInputStream in ;
45   String JavaDoc filename ;
46   int filesize ;
47   MainFrame frm ;
48   IRCconnexion IRCchan ;
49   private JFileChooser jFileChooser ;
50
51   public DCCincomingThread(String JavaDoc _ip, int _port, String JavaDoc _filename, int _filesize, MainFrame _frm, IRCconnexion _IRCchan, String JavaDoc _sender,
52                            int _offset)
53   {
54     filename = _filename ;
55     filesize = _filesize ;
56     port=_port;
57     long t = Long.parseLong(_ip) ;
58     frm = _frm ;
59     IRCchan = _IRCchan ;
60     sender = _sender ;
61     offset=_offset;
62     transferred = _offset ;
63     String JavaDoc ip = toInetAddress(t).toString().replaceAll("/", "") ;
64     try
65     {
66       s = new Socket(ip, port) ;
67       in = new DataInputStream(s.getInputStream()) ;
68       this.start() ;
69     }
70     catch (UnknownHostException ex)
71     {ex.printStackTrace() ;
72     }
73     catch (IOException ex)
74     {ex.printStackTrace() ;
75     }
76   }
77
78   private InetAddress toInetAddress(long ipAddress)
79   {
80     InetAddress inetAddress = null ;
81     try
82     {
83       long upperBytes = 0 ;
84       long longByte ;
85       byte[] bytes = new byte[4] ;
86       for (int i = 3 ; i >= 0 ; i--)
87       {
88         longByte = (new Double JavaDoc(Math.floor((ipAddress - Math.abs(upperBytes)) >> (8 * i)))).longValue() ;
89         bytes[4 - i - 1] = (byte)longByte ;
90         upperBytes += longByte << (8 * i) ;
91       }
92       inetAddress = InetAddress.getByAddress(bytes) ;
93
94     }
95     catch (UnknownHostException e)
96     {
97       e.printStackTrace() ;
98     }
99     return inetAddress ;
100   }
101
102 /////////////////////////////////////////////////////
103
////////////////////////////////////////////////////
104
public void run()
105   {
106     try
107     {
108       file = null ;
109       DCCtransfer dccfrm = new DCCtransfer(frm, IRCchan, "DCC " + filename, this, sender, filename,transferred,port ) ;
110       byte b[] = new byte[1024] ;
111       Timer JavaDoc speedTimer = new Timer JavaDoc() ;
112       Speed speedTask = new Speed() ;
113       jFileChooser = new JFileChooser("/");
114       jFileChooser.setSelectedFile(new File(filename)) ;
115       if (jFileChooser.showSaveDialog(frm) == JFileChooser.APPROVE_OPTION)
116       {
117         String JavaDoc t = jFileChooser.getSelectedFile().getAbsolutePath() ;
118         //on ouvre un fichier en ecriture
119
file = new RandomAccessFile(t, "rw") ;
120       }
121       else
122       {
123         s.close() ;
124       }
125
126       dccfrm.getProgressBar().setMaximum(filesize) ;
127
128       frm.getMdiPanel().add(dccfrm) ;
129       dccfrm.setVisible(true) ;
130       labelInfo = dccfrm.getInfoLabel() ;
131
132       //timer pour la vitesse de transfer
133
speedTimer.schedule(speedTask, 1000, 100) ;
134
135       //ecriture des donnees
136
while (transferred < filesize)
137       {
138         amount = in.read(b) ;
139         file.write(b,offset,amount);
140             transferred += amount ;
141             dccfrm.getProgressBar().setValue(transferred) ;
142       }
143
144       speedTimer.cancel() ;
145       speedTimer.cancel() ;
146       if (transferred >= filesize)
147       {
148         labelInfo.setText(filesize + " / " + filesize + " " + speed + " ko/s") ;
149       }
150       file.close() ; //on ferme le fichier
151
//on ferme le flux et le socket
152
in.close() ;
153       s.close() ;
154     }
155     catch (UnknownHostException ex)
156     {
157       ex.printStackTrace() ;
158     }
159     catch (IOException ex)
160     {
161       ex.printStackTrace() ;
162     }
163   }
164
165   public void abortTransfer()
166   {
167     try
168     {
169       file.close() ; //on ferme le fichier
170
//on ferme le flux et le socket
171
in.close() ;
172       s.close() ;
173     }
174     catch (IOException ex)
175     {
176     }
177   }
178
179
180   //timer qui sert a l'affichage de la vitesse de dl
181
class Speed extends TimerTask
182   {
183     long startPoint = System.currentTimeMillis() ;
184     long passedTime = 0 ;
185     public void run()
186     {
187       passedTime = (System.currentTimeMillis() - startPoint) / 1000 ; //temmps ecoule en secondes
188
speed = ((int)(((transferred + 1) / 1000) / passedTime)) ;
189       labelInfo.setText(transferred + " / " + filesize + " " + speed + " ko/s") ;
190     }
191   }
192
193   public DCCincomingThread()
194   {
195     try
196     {
197       jbInit() ;
198     }
199     catch (Exception JavaDoc e)
200     {
201       e.printStackTrace() ;
202     }
203   }
204
205   private void jbInit() throws Exception JavaDoc
206   {
207     jFileChooser.setBorder(BorderFactory.createLineBorder(Color.gray)) ;
208   }
209 }
210
Popular Tags