Package common :: Module jingle_transport
[hide private]
[frames] | no frames]

Source Code for Module common.jingle_transport

  1  ## 
  2  ## Copyright (C) 2006 Gajim Team 
  3  ## 
  4  ## This program is free software; you can redistribute it and/or modify 
  5  ## it under the terms of the GNU General Public License as published 
  6  ## by the Free Software Foundation; version 2 only. 
  7  ## 
  8  ## This program is distributed in the hope that it will be useful, 
  9  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  ## GNU General Public License for more details. 
 12  ## 
 13   
 14  """ 
 15  Handles Jingle Transports (currently only ICE-UDP) 
 16  """ 
 17   
 18  import xmpp 
 19   
 20  transports = {} 
 21   
22 -def get_jingle_transport(node):
23 namespace = node.getNamespace() 24 if namespace in transports: 25 return transports[namespace]()
26 27
28 -class TransportType(object):
29 """ 30 Possible types of a JingleTransport 31 """ 32 datagram = 1 33 streaming = 2
34 35
36 -class JingleTransport(object):
37 """ 38 An abstraction of a transport in Jingle sessions 39 """ 40
41 - def __init__(self, type_):
42 self.type = type_ 43 self.candidates = [] 44 self.remote_candidates = []
45
46 - def _iter_candidates(self):
47 for candidate in self.candidates: 48 yield self.make_candidate(candidate)
49
50 - def make_candidate(self, candidate):
51 """ 52 Build a candidate stanza for the given candidate 53 """ 54 pass
55
56 - def make_transport(self, candidates=None):
57 """ 58 Build a transport stanza with the given candidates (or self.candidates if 59 candidates is None) 60 """ 61 if not candidates: 62 candidates = self._iter_candidates() 63 else: 64 candidates = (self.make_candidate(candidate) for candidate in candidates) 65 transport = xmpp.Node('transport', payload=candidates) 66 return transport
67
68 - def parse_transport_stanza(self, transport):
69 """ 70 Return the list of transport candidates from a transport stanza 71 """ 72 return []
73 74 75 import farsight 76
77 -class JingleTransportICEUDP(JingleTransport):
78 - def __init__(self):
80
81 - def make_candidate(self, candidate):
82 types = {farsight.CANDIDATE_TYPE_HOST: 'host', 83 farsight.CANDIDATE_TYPE_SRFLX: 'srflx', 84 farsight.CANDIDATE_TYPE_PRFLX: 'prflx', 85 farsight.CANDIDATE_TYPE_RELAY: 'relay', 86 farsight.CANDIDATE_TYPE_MULTICAST: 'multicast'} 87 attrs = { 88 'component': candidate.component_id, 89 'foundation': '1', # hack 90 'generation': '0', 91 'ip': candidate.ip, 92 'network': '0', 93 'port': candidate.port, 94 'priority': int(candidate.priority), # hack 95 } 96 if candidate.type in types: 97 attrs['type'] = types[candidate.type] 98 if candidate.proto == farsight.NETWORK_PROTOCOL_UDP: 99 attrs['protocol'] = 'udp' 100 else: 101 # we actually don't handle properly different tcp options in jingle 102 attrs['protocol'] = 'tcp' 103 return xmpp.Node('candidate', attrs=attrs)
104
105 - def make_transport(self, candidates=None):
106 transport = JingleTransport.make_transport(self, candidates) 107 transport.setNamespace(xmpp.NS_JINGLE_ICE_UDP) 108 if self.candidates and self.candidates[0].username and \ 109 self.candidates[0].password: 110 transport.setAttr('ufrag', self.candidates[0].username) 111 transport.setAttr('pwd', self.candidates[0].password) 112 return transport
113
114 - def parse_transport_stanza(self, transport):
115 candidates = [] 116 for candidate in transport.iterTags('candidate'): 117 cand = farsight.Candidate() 118 cand.component_id = int(candidate['component']) 119 cand.ip = str(candidate['ip']) 120 cand.port = int(candidate['port']) 121 cand.foundation = str(candidate['foundation']) 122 #cand.type = farsight.CANDIDATE_TYPE_LOCAL 123 cand.priority = int(candidate['priority']) 124 125 if candidate['protocol'] == 'udp': 126 cand.proto = farsight.NETWORK_PROTOCOL_UDP 127 else: 128 # we actually don't handle properly different tcp options in jingle 129 cand.proto = farsight.NETWORK_PROTOCOL_TCP 130 131 cand.username = str(transport['ufrag']) 132 cand.password = str(transport['pwd']) 133 134 #FIXME: huh? 135 types = {'host': farsight.CANDIDATE_TYPE_HOST, 136 'srflx': farsight.CANDIDATE_TYPE_SRFLX, 137 'prflx': farsight.CANDIDATE_TYPE_PRFLX, 138 'relay': farsight.CANDIDATE_TYPE_RELAY, 139 'multicast': farsight.CANDIDATE_TYPE_MULTICAST} 140 if 'type' in candidate and candidate['type'] in types: 141 cand.type = types[candidate['type']] 142 else: 143 print 'Unknown type %s', candidate['type'] 144 candidates.append(cand) 145 self.remote_candidates.extend(candidates) 146 return candidates
147 148 transports[xmpp.NS_JINGLE_ICE_UDP] = JingleTransportICEUDP 149