Changeset 6609 for lang/java

Show
Ignore:
Timestamp:
02/12/08 23:32:13 (5 years ago)
Author:
nori090
Message:

lang/java/jimmy: connectionの間違い修正

Location:
lang/java/jimmy/trunk/jimmy/src
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/java/jimmy/trunk/jimmy/src/main/java/jimmy/Connection.java

    r6570 r6609  
    2121import java.io.IOException; 
    2222import java.net.InetAddress; 
     23import java.net.InetSocketAddress; 
    2324import java.net.Socket; 
    2425import java.nio.ByteBuffer; 
     
    2728import java.util.concurrent.ConcurrentLinkedQueue; 
    2829 
    29 import org.apache.commons.lang.ArrayUtils; 
    30  
    3130import jimmy.command.Command; 
    3231import jimmy.command.Command00; 
    3332import jimmy.command.Command97; 
     33 
     34import org.apache.commons.codec.binary.Hex; 
     35import org.apache.commons.lang.ArrayUtils; 
    3436 
    3537/** 
     
    8183    ConcurrentLinkedQueue<Command> queue = new ConcurrentLinkedQueue<Command>(); 
    8284 
    83     void send( byte[] packet ) 
     85    public void send( byte[] packet ) 
    8486        throws WinnyProtocolException { 
    8587        long cur; 
     
    160162    } 
    161163 
    162     Command00 authorize() 
     164    public Command00 authorize() 
    163165        throws WinnyProtocolException { 
    164166        Command00 header = new Command00(); 
     
    176178            send_rc4key = new ARC4Cipher( Command.toStep2Key( key ) ); 
    177179 
     180            authorize_block = new byte[6]; 
    178181            rsize = receive( authorize_block, false ); 
    179182            if ( rsize != authorize_block.length ) { 
     
    203206    } 
    204207 
    205     Command receiveOneCommand() throws WinnyProtocolException { 
     208    public Command receiveOneCommand() 
     209        throws WinnyProtocolException { 
    206210        if ( !queue.isEmpty() ) { 
    207211            return queue.poll(); 
     
    209213        byte[] head = new byte[5]; 
    210214        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" ); 
    213217        } 
    214218        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" ); 
    217221        } 
    218222        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 
    226232    // TODO バッファリング受信は良く分からないので見送り 
    227233    void replenish() { 
    228234        // キューに入れる作業 
    229235    } 
    230      
     236 
    231237    long rate() { 
    232238        long cur = System.currentTimeMillis(); 
    233239        long rate; 
    234          
     240 
    235241        long recv = cur - last_recv_time; 
    236242        long send = cur - last_send_time; 
    237          
    238         if (recv == 0 && send == 0) { 
     243 
     244        if ( recv == 0 && send == 0 ) { 
    239245            return 0; 
    240         } else if (recv == 0) { 
     246        } 
     247        else if ( recv == 0 ) { 
    241248            rate = send_size_sec / send; 
    242         } else if (send == 0) { 
     249        } 
     250        else if ( send == 0 ) { 
    243251            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 ); 
    246255        } 
    247256        // maybe int size; 
    248257        return rate; 
    249258    } 
    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 
    251273    void close() { 
    252274        try { 
  • lang/java/jimmy/trunk/jimmy/src/test/java/jimmy/SocketDumpTest.java

    r6570 r6609  
    4444        InputStream in = null; 
    4545        try { 
    46             Node target = new Node( "@d9d162a23cc199f08f40b3b45704846392a6d73d" ); 
    47             s = new Socket( target.address, target.port ); 
     46            s = new Socket( "192.168.77.128", 6191 ); 
    4847            s.setSoTimeout( 5000 ); 
    4948            in = s.getInputStream();