~motu-torrent/azureus/upstream-stable

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/core/peermanager/messaging/azureus/session/AZSessionSyn.java

  • Committer: John Dong
  • Date: 2007-10-22 04:54:13 UTC
  • Revision ID: john.dong@gmail.com-20071022045413-3ovb11u82rrcokxx
Commit 3.0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on Apr 30, 2004
3
 
 * Created by Alon Rohter
4
 
 * Copyright (C) 2004, 2005, 2006 Aelitis, 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
 
 * AELITIS, SAS au capital de 46,603.30 euros
19
 
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
20
 
 *
21
 
 */
22
 
 
23
 
package com.aelitis.azureus.core.peermanager.messaging.azureus.session;
24
 
 
25
 
import java.util.*;
26
 
 
27
 
import org.gudy.azureus2.core3.util.*;
28
 
 
29
 
import com.aelitis.azureus.core.peermanager.messaging.*;
30
 
import com.aelitis.azureus.core.peermanager.messaging.azureus.AZMessage;
31
 
 
32
 
 
33
 
 
34
 
/**
35
 
 * Sent to initiate a torrent session.
36
 
 */
37
 
public class AZSessionSyn implements AZMessage {
38
 
  private DirectByteBuffer buffer = null;
39
 
  private String description = null;
40
 
  
41
 
  private final byte[] infohash;
42
 
  private final int session_id;
43
 
  private final Map session_info;
44
 
 
45
 
  public AZSessionSyn( byte[] infohash, int local_session_id, Map session_info ) {
46
 
    this.session_id = local_session_id;
47
 
    this.infohash = infohash;
48
 
    this.session_info = session_info;
49
 
  }
50
 
  
51
 
  
52
 
  public byte[] getInfoHash() {  return infohash;  }
53
 
  public int getSessionID(){  return session_id;  }
54
 
  public Map getSessionInfo() {  return session_info;  }
55
 
  
56
 
  
57
 
    
58
 
  public String getID() {  return AZMessage.ID_AZ_SESSION_SYN;  }
59
 
  public byte[] getIDBytes() {  return AZMessage.ID_AZ_SESSION_SYN_BYTES;  }
60
 
  
61
 
  public String getFeatureID() {  throw new RuntimeException( "not implemented" );  }   //TODO  
62
 
  public int getFeatureSubID() {  throw new RuntimeException( "not implemented" );  }   //TODO
63
 
  
64
 
  public int getType() {  return Message.TYPE_PROTOCOL_PAYLOAD;  }
65
 
    
66
 
  
67
 
  public String getDescription() {
68
 
    if( description == null ) {
69
 
      description = getID()+ " session id " +session_id+ " for infohash " +ByteFormatter.nicePrint( infohash, true );
70
 
    }
71
 
    return description;
72
 
  }
73
 
  
74
 
  
75
 
  public DirectByteBuffer[] getData() {
76
 
    if( buffer == null ) {
77
 
      Map payload_map = new HashMap();
78
 
      
79
 
      payload_map.put( "session_id", new Long(session_id) );
80
 
      payload_map.put( "infohash", infohash );
81
 
      payload_map.put( "info", session_info );
82
 
 
83
 
      buffer = MessagingUtil.convertPayloadToBencodedByteStream( payload_map, DirectByteBuffer.AL_MSG );
84
 
    }
85
 
    
86
 
    return new DirectByteBuffer[]{ buffer };
87
 
  }
88
 
  
89
 
  
90
 
  public Message deserialize( DirectByteBuffer data ) throws MessageException {    
91
 
    Map root = MessagingUtil.convertBencodedByteStreamToPayload( data, 20, getID() );
92
 
 
93
 
    Long id = (Long)root.get( "session_id" );
94
 
    if( id == null ) throw new MessageException( "id == null" );
95
 
    int sid = id.intValue();
96
 
    
97
 
    byte[] hash = (byte[])root.get( "infohash" );
98
 
    if( hash == null )  throw new MessageException( "hash == null" );
99
 
    if( hash.length != 20 )  throw new MessageException( "hash.length != 20: " +hash.length );
100
 
    
101
 
    Map info = (Map)root.get( "info" );
102
 
    
103
 
    return new AZSessionSyn( hash, sid, info );
104
 
  }
105
 
  
106
 
  
107
 
  public void destroy() {
108
 
    if( buffer != null )  buffer.returnToPool();
109
 
  }
110
 
  
111
 
}
 
1
/*
 
2
 * Created on Apr 30, 2004
 
3
 * Created by Alon Rohter
 
4
 * Copyright (C) 2004, 2005, 2006 Aelitis, 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
 * AELITIS, SAS au capital de 46,603.30 euros
 
19
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 
20
 *
 
21
 */
 
22
 
 
23
package com.aelitis.azureus.core.peermanager.messaging.azureus.session;
 
24
 
 
25
import java.util.*;
 
26
 
 
27
import org.gudy.azureus2.core3.util.*;
 
28
 
 
29
import com.aelitis.azureus.core.peermanager.messaging.*;
 
30
import com.aelitis.azureus.core.peermanager.messaging.azureus.AZMessage;
 
31
 
 
32
 
 
33
 
 
34
/**
 
35
 * Sent to initiate a torrent session.
 
36
 */
 
37
public class AZSessionSyn implements AZMessage {
 
38
  private final byte version;
 
39
  private DirectByteBuffer buffer = null;
 
40
  private String description = null;
 
41
  
 
42
  private final byte[] infohash;
 
43
  private final int session_id;
 
44
  private final Map session_info;
 
45
 
 
46
  public AZSessionSyn( byte[] infohash, int local_session_id, Map session_info, byte version ) {
 
47
    this.session_id = local_session_id;
 
48
    this.infohash = infohash;
 
49
    this.session_info = session_info;
 
50
    this.version = version;
 
51
  }
 
52
  
 
53
  
 
54
  public byte[] getInfoHash() {  return infohash;  }
 
55
  public int getSessionID(){  return session_id;  }
 
56
  public Map getSessionInfo() {  return session_info;  }
 
57
  
 
58
  
 
59
    
 
60
  public String getID() {  return AZMessage.ID_AZ_SESSION_SYN;  }
 
61
  public byte[] getIDBytes() {  return AZMessage.ID_AZ_SESSION_SYN_BYTES;  }
 
62
  
 
63
  public String getFeatureID() {  throw new RuntimeException( "not implemented" );  }   //TODO  
 
64
  public int getFeatureSubID() {  throw new RuntimeException( "not implemented" );  }   //TODO
 
65
  
 
66
  public int getType() {  return Message.TYPE_PROTOCOL_PAYLOAD;  }
 
67
    
 
68
  public byte getVersion() { return version; };
 
69
 
 
70
  public String getDescription() {
 
71
    if( description == null ) {
 
72
      description = getID()+ " session id " +session_id+ " for infohash " +ByteFormatter.nicePrint( infohash, true );
 
73
    }
 
74
    return description;
 
75
  }
 
76
  
 
77
  
 
78
  public DirectByteBuffer[] getData() {
 
79
    if( buffer == null ) {
 
80
      Map payload_map = new HashMap();
 
81
      
 
82
      payload_map.put( "session_id", new Long(session_id) );
 
83
      payload_map.put( "infohash", infohash );
 
84
      payload_map.put( "info", session_info );
 
85
 
 
86
      buffer = MessagingUtil.convertPayloadToBencodedByteStream( payload_map, DirectByteBuffer.AL_MSG );
 
87
    }
 
88
    
 
89
    return new DirectByteBuffer[]{ buffer };
 
90
  }
 
91
  
 
92
  
 
93
  public Message deserialize( DirectByteBuffer data, byte version ) throws MessageException {    
 
94
    Map root = MessagingUtil.convertBencodedByteStreamToPayload( data, 20, getID() );
 
95
 
 
96
    Long id = (Long)root.get( "session_id" );
 
97
    if( id == null ) throw new MessageException( "id == null" );
 
98
    int sid = id.intValue();
 
99
    
 
100
    byte[] hash = (byte[])root.get( "infohash" );
 
101
    if( hash == null )  throw new MessageException( "hash == null" );
 
102
    if( hash.length != 20 )  throw new MessageException( "hash.length != 20: " +hash.length );
 
103
    
 
104
    Map info = (Map)root.get( "info" );
 
105
    
 
106
    return new AZSessionSyn( hash, sid, info, version );
 
107
  }
 
108
  
 
109
  
 
110
  public void destroy() {
 
111
    if( buffer != null )  buffer.returnToPool();
 
112
  }
 
113
  
 
114
}