# Path: groundstation/peer_socket.py
# class PeerSocket(StreamSocket):
#     """Wrapper for a peer who just connected, or one we've connected to
# 
#     Since the communication protocol should be implicitly bidirectional, the
#     factory methods should be the only instanciation methods"""
# 
#     def __init__(self, conn, peer):
#         self._sock = conn
#         super(PeerSocket, self).__init__()
#         self.peer = peer
# 
#     @classmethod
#     def from_accept(klass, args):
#         return klass(*args)
# 
#     @classmethod
#     def from_connect(klass, args):
#         return klass(*args)
# 
#     def __repr__(self):
#         return "<%s: from %s>" % (self.__class__, self.peer)
# 
#     # Wrap StreamSocket's send and recv in exception handling
#     def send(self, *args, **kwargs):
#         try:
#             return super(PeerSocket, self).send(*args, **kwargs)
#         except socket.error as e:
#             self.close_and_finalise()
# 
#     def recv(self, *args, **kwargs):
#         try:
#             return super(PeerSocket, self).recv(*args, **kwargs)
#         except socket.error as e:
#             self.close_and_finalise()


import os
import socket
import select
import unittest
import tempfile
import shutil
from groundstation.peer_socket import PeerSocket
from integration_fixture import StationIntegrationFixture, \
                                TestListener, \
                                TestClient
        client = TestClient(addr)


class StationCommunication(StationIntegrationFixture):
    def test_send_objects(self):
        read_sockets = []
        write_sockets = []
        def tick():
            return select.select(read_sockets, write_sockets, [], 1)

        addr = os.path.join(self.dir, "listener")
        listener = TestListener(addr)
        read_sockets.append(listener)

        client = TestClient(addr)
        write_sockets.append(client)

        (sread, swrite, _) = tick()
        # Handle our listener
        self.assertEqual(len(sread), 1)
        peer = listener.accept(PeerSocket)
        read_sockets.append(peer)
        write_sockets.append(peer)

        self.assertEqual(len(swrite), 1)

        client.begin_handshake(self.stations[0])
        client.send()

        (sread, swrite, _) = tick()

# Path: groundstation/peer_socket.py
# class PeerSocket(StreamSocket):
#     """Wrapper for a peer who just connected, or one we've connected to
# 
#     Since the communication protocol should be implicitly bidirectional, the
#     factory methods should be the only instanciation methods"""
# 
#     def __init__(self, conn, peer):
#         self._sock = conn
#         super(PeerSocket, self).__init__()
#         self.peer = peer
# 
#     @classmethod
#     def from_accept(klass, args):
#         return klass(*args)
# 
#     @classmethod
#     def from_connect(klass, args):
#         return klass(*args)
# 
#     def __repr__(self):
#         return "<%s: from %s>" % (self.__class__, self.peer)
# 
#     # Wrap StreamSocket's send and recv in exception handling
#     def send(self, *args, **kwargs):
#         try:
#             return super(PeerSocket, self).send(*args, **kwargs)
#         except socket.error as e:
#             self.close_and_finalise()
# 
#     def recv(self, *args, **kwargs):
#         try:
#             return super(PeerSocket, self).recv(*args, **kwargs)
#         except socket.error as e:
#             self.close_and_finalise()


# Path: test/test_station_integration.py
import os
import socket
import select
import unittest
import tempfile
import shutil
from groundstation.peer_socket import PeerSocket
from integration_fixture import StationIntegrationFixture, \
                                TestListener, \
                                TestClient

        client = TestClient(addr)


class StationCommunication(StationIntegrationFixture):
    def test_send_objects(self):
        read_sockets = []
        write_sockets = []
        def tick():
            return select.select(read_sockets, write_sockets, [], 1)

        addr = os.path.join(self.dir, "listener")
        listener = TestListener(addr)
        read_sockets.append(listener)

        client = TestClient(addr)
        write_sockets.append(client)

        (sread, swrite, _) = tick()
        # Handle our listener
        self.assertEqual(len(sread), 1)
        peer = listener.accept(PeerSocket)
        read_sockets.append(peer)
        write_sockets.append(peer)

        self.assertEqual(len(swrite), 1)

        client.begin_handshake(self.stations[0])
        client.send()

        (sread, swrite, _) = tick()

        self.assertEqual(sread, [peer])