- Timestamp:
- 02/12/08 23:32:13 (5 years ago)
- Location:
- lang/java/jimmy/trunk/jimmy/src
- Files:
-
- 1 added
- 2 modified
-
main/java/jimmy/Connection.java (modified) (7 diffs)
-
test/java/jimmy/ConnectionTest.java (added)
-
test/java/jimmy/SocketDumpTest.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/java/jimmy/trunk/jimmy/src/main/java/jimmy/Connection.java
r6570 r6609 21 21 import java.io.IOException; 22 22 import java.net.InetAddress; 23 import java.net.InetSocketAddress; 23 24 import java.net.Socket; 24 25 import java.nio.ByteBuffer; … … 27 28 import java.util.concurrent.ConcurrentLinkedQueue; 28 29 29 import org.apache.commons.lang.ArrayUtils;30 31 30 import jimmy.command.Command; 32 31 import jimmy.command.Command00; 33 32 import jimmy.command.Command97; 33 34 import org.apache.commons.codec.binary.Hex; 35 import org.apache.commons.lang.ArrayUtils; 34 36 35 37 /** … … 81 83 ConcurrentLinkedQueue<Command> queue = new ConcurrentLinkedQueue<Command>(); 82 84 83 void send( byte[] packet )85 public void send( byte[] packet ) 84 86 throws WinnyProtocolException { 85 87 long cur; … … 160 162 } 161 163 162 Command00 authorize()164 public Command00 authorize() 163 165 throws WinnyProtocolException { 164 166 Command00 header = new Command00(); … … 176 178 send_rc4key = new ARC4Cipher( Command.toStep2Key( key ) ); 177 179 180 authorize_block = new byte[6]; 178 181 rsize = receive( authorize_block, false ); 179 182 if ( rsize != authorize_block.length ) { … … 203 206 } 204 207 205 Command receiveOneCommand() throws WinnyProtocolException { 208 public Command receiveOneCommand() 209 throws WinnyProtocolException { 206 210 if ( !queue.isEmpty() ) { 207 211 return queue.poll(); … … 209 213 byte[] head = new byte[5]; 210 214 int rsize = receive( head, true ); 211 if ( rsize != head.length) {212 throw new WinnyProtocolException( "read packet error");215 if ( rsize != head.length ) { 216 throw new WinnyProtocolException( "read packet error" ); 213 217 } 214 218 int block_length = littleEndianBytes2int( ArrayUtils.subarray( head, 0, 4 ) ); 215 if ( block_length > BUFFER_SIZE) {216 throw new WinnyProtocolException( "read packet error");219 if ( block_length > BUFFER_SIZE ) { 220 throw new WinnyProtocolException( "read packet error" ); 217 221 } 218 222 byte[] data = new byte[block_length - 1]; 219 rsize = receive( data, true ); 220 if (rsize == 0) { 221 throw new WinnyProtocolException("read packet error"); 222 } 223 return Command.getCommandInstance( data ); 224 } 225 223 if ( block_length - 1 != 0 ) { 224 rsize = receive( data, true ); 225 if ( rsize == 0 ) { 226 throw new WinnyProtocolException( "read packet error" ); 227 } 228 } 229 return Command.getCommandInstance( ArrayUtils.addAll( head, data ) ); 230 } 231 226 232 // TODO バッファリング受信は良く分からないので見送り 227 233 void replenish() { 228 234 // キューに入れる作業 229 235 } 230 236 231 237 long rate() { 232 238 long cur = System.currentTimeMillis(); 233 239 long rate; 234 240 235 241 long recv = cur - last_recv_time; 236 242 long send = cur - last_send_time; 237 238 if ( recv == 0 && send == 0) {243 244 if ( recv == 0 && send == 0 ) { 239 245 return 0; 240 } else if (recv == 0) { 246 } 247 else if ( recv == 0 ) { 241 248 rate = send_size_sec / send; 242 } else if (send == 0) { 249 } 250 else if ( send == 0 ) { 243 251 rate = recv_size_sec / recv; 244 } else { 245 rate = (send_size_sec/ send) + (recv_size_sec/recv); 252 } 253 else { 254 rate = ( send_size_sec / send ) + ( recv_size_sec / recv ); 246 255 } 247 256 // maybe int size; 248 257 return rate; 249 258 } 250 259 260 Command00 connect( InetAddress address, int port ) 261 throws WinnyProtocolException { 262 263 this.address = address; 264 try { 265 socket = SocketChannel.open( new InetSocketAddress( address, port ) ); 266 return authorize(); 267 } 268 catch ( IOException e ) { 269 throw new WinnyProtocolException( e ); 270 } 271 } 272 251 273 void close() { 252 274 try { -
lang/java/jimmy/trunk/jimmy/src/test/java/jimmy/SocketDumpTest.java
r6570 r6609 44 44 InputStream in = null; 45 45 try { 46 Node target = new Node( "@d9d162a23cc199f08f40b3b45704846392a6d73d" ); 47 s = new Socket( target.address, target.port ); 46 s = new Socket( "192.168.77.128", 6191 ); 48 47 s.setSoTimeout( 5000 ); 49 48 in = s.getInputStream();
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)