1
# -*- coding: utf-8 -*-
3
# pymsn - a python client library for Msn
5
# Copyright (C) 2007 Ali Sabil <asabil@gmail.com>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from pymsn.msnp.message import MessageAcknowledgement
22
from pymsn.msnp2p.transport.TLP import MessageChunk
23
from pymsn.msnp2p.transport.base import BaseP2PTransport
24
from pymsn.switchboard_manager import SwitchboardClient
30
__all__ = ['SwitchboardP2PTransport']
32
logger = logging.getLogger('msnp2p:transport')
35
class SwitchboardP2PTransport(BaseP2PTransport, SwitchboardClient):
36
def __init__(self, client, contacts, transport_manager):
37
SwitchboardClient.__init__(self, client, contacts)
38
BaseP2PTransport.__init__(self, transport_manager, "switchboard")
41
BaseP2PTransport.close(self)
45
def _can_handle_message(message, switchboard_client=None):
46
content_type = message.content_type[0]
47
return content_type == 'application/x-msnmsgrp2p'
51
for peer in self.total_participants:
60
def max_chunk_size(self):
61
return 1250 # length of the chunk including the header but not the footer
63
def _send_chunk(self, chunk):
64
headers = {'P2P-Dest': self.peer.account}
65
content_type = 'application/x-msnmsgrp2p'
66
body = str(chunk) + struct.pack('>L', chunk.application_id)
67
self._send_message(content_type, body, headers, MessageAcknowledgement.MSNC)
69
def _on_message_received(self, message):
70
chunk = MessageChunk.parse(message.body[:-4])
71
chunk.application_id = struct.unpack('>L', message.body[-4:])[0]
72
self._on_chunk_received(chunk)
74
def _on_message_sent(self, message):
75
chunk = MessageChunk.parse(message.body[:-4])
76
chunk.application_id = struct.unpack('>L', message.body[-4:])[0]
77
self._on_chunk_sent(chunk)
79
def _on_contact_joined(self, contact):
82
def _on_contact_left(self, contact):