1 /*
2  * Created on 2 Oct 2006
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 package com.aelitis.azureus.core.networkmanager.impl.http;
21 
22 import com.aelitis.azureus.core.networkmanager.RawMessage;
23 import com.aelitis.azureus.core.peermanager.messaging.Message;
24 import com.aelitis.azureus.core.peermanager.messaging.MessageStreamEncoder;
25 import com.aelitis.azureus.core.peermanager.messaging.bittorrent.BTMessage;
26 
27 public class
28 HTTPMessageEncoder
29 	implements MessageStreamEncoder
30 {
31 	private HTTPNetworkConnection	http_connection;
32 
33 	public void
setConnection( HTTPNetworkConnection _http_connection )34 	setConnection(
35 		HTTPNetworkConnection	_http_connection )
36 	{
37 		http_connection	= _http_connection;
38 	}
39 
40 	public RawMessage[]
encodeMessage( Message message )41 	encodeMessage(
42 		Message message )
43 	{
44 		String	id = message.getID();
45 
46 		// System.out.println( "encodeMessage: " + message.getID());
47 
48 		RawMessage	raw_message = null;
49 
50 		if ( id.equals( BTMessage.ID_BT_HANDSHAKE )){
51 
52 			raw_message = http_connection.encodeHandShake( message );
53 
54 		}else if ( id.equals( BTMessage.ID_BT_CHOKE )){
55 
56 			raw_message = http_connection.encodeChoke();
57 
58 		}else if ( id.equals( BTMessage.ID_BT_UNCHOKE )){
59 
60 			raw_message = http_connection.encodeUnchoke();
61 
62 		}else if ( id.equals( BTMessage.ID_BT_BITFIELD)){
63 
64 			raw_message = http_connection.encodeBitField();
65 
66 		}else if ( id.equals( BTMessage.ID_BT_PIECE )){
67 
68 			return( http_connection.encodePiece( message ));
69 
70 		}else if ( id.equals( HTTPMessage.MSG_ID )){
71 
72 			raw_message = ((HTTPMessage)message).encode( message );
73 		}
74 
75 		if ( raw_message == null ){
76 
77 			raw_message = http_connection.getEmptyRawMessage( message );
78 		}
79 
80 		return( new RawMessage[]{ raw_message });
81 	}
82 }
83