encodePiece(
Message message )
{
last_http_activity_time = SystemTime.getCurrentTime();
BTPiece piece = (BTPiece)message;
List<pendingRequest> ready_requests = new ArrayList<pendingRequest>();
boolean found = false;
synchronized( outstanding_requests ){
if ( destroyed ){
return( new RawMessage[]{ getEmptyRawMessage( message )});
}
for (int i=0;i<outstanding_requests.size();i++){
pendingRequest req = outstanding_requests.get(i);
if ( req.getPieceNumber() == piece.getPieceNumber() &&
req.getStart() == piece.getPieceOffset() &&
req.getLength() == piece.getPieceData().remaining( DirectByteBuffer.SS_NET )){
if ( req.getBTPiece() == null ){
req.setBTPiece( piece );
found = true;
if ( i == 0 ){
Iterator<pendingRequest> it = outstanding_requests.iterator();
while( it.hasNext()){
pendingRequest r = it.next();
BTPiece btp = r.getBTPiece();
if ( btp == null ){
break;
}
it.remove();
ready_requests.add( r );
}
}
break;
}
}
}
}
if ( !found ){
Debug.out( "request not matched" );
return( new RawMessage[]{ getEmptyRawMessage( message )});
}
if ( ready_requests.size() == 0 ){
return( new RawMessage[]{ getEmptyRawMessage( message )});
}
try{
submitBTRequests();
}catch( IOException e ){
}
pendingRequest req = (pendingRequest)ready_requests.get(0);
DirectByteBuffer[] buffers;
httpRequest http_request = req.getHTTPRequest();
RawMessage[] raw_messages = new RawMessage[ ready_requests.size()];
for (int i=0;i<raw_messages.length;i++){
buffers = new DirectByteBuffer[ 2 ];
if ( !http_request.hasSentFirstReply()){
http_request.setSentFirstReply();
String header = encodeHeader( http_request );
buffers[0] = new DirectByteBuffer( ByteBuffer.wrap( header.getBytes()));
}else{
// we have to do this as core code assumes buffer entry 0 is protocol
buffers[0] = new DirectByteBuffer( ByteBuffer.allocate(0));
}
req = (pendingRequest)ready_requests.get(i);
BTPiece this_piece = req.getBTPiece();
int piece_number = this_piece.getPieceNumber();
if ( !piece_map.get( piece_number )){
// kinda crappy as it triggers on first block of piece, however better
// than nothing
piece_map.set( piece_number );
decoder.addMessage( new BTHave( piece_number, (byte)1 ));
}
buffers[1] = this_piece.getPieceData();
req.logQueued();
if ( request_listeners != null ){