Exemple #1
0
 def _startSession(self, datagram, addr, mode):
     # Set up a call context so that we can pass extra arbitrary
     # information to interested backends without adding extra call
     # arguments, or switching to using a request object, for example.
     context = {}
     if self.transport is not None:
         # Add the local and remote addresses to the call context.
         local = self.transport.getHost()
         context["local"] = local.host, local.port
         context["remote"] = addr
     try:
         if datagram.opcode == OP_WRQ:
             fs_interface = yield call(context, self.backend.get_writer,
                                       datagram.filename)
         elif datagram.opcode == OP_RRQ:
             fs_interface = yield call(context, self.backend.get_reader,
                                       datagram.filename)
     except Unsupported as e:
         self.transport.write(
             ERRORDatagram.from_code(
                 ERR_ILLEGAL_OP,
                 u"{}".format(e).encode("ascii", "replace")).to_wire(),
             addr)
     except AccessViolation:
         self.transport.write(
             ERRORDatagram.from_code(ERR_ACCESS_VIOLATION).to_wire(), addr)
     except FileExists:
         self.transport.write(
             ERRORDatagram.from_code(ERR_FILE_EXISTS).to_wire(), addr)
     except FileNotFound:
         self.transport.write(
             ERRORDatagram.from_code(ERR_FILE_NOT_FOUND).to_wire(), addr)
     except BackendError as e:
         self.transport.write(
             ERRORDatagram.from_code(
                 ERR_NOT_DEFINED,
                 u"{}".format(e).encode("ascii", "replace")).to_wire(),
             addr)
     else:
         if datagram.opcode == OP_WRQ:
             if mode == b'netascii':
                 fs_interface = NetasciiReceiverProxy(fs_interface)
             session = RemoteOriginWriteSession(addr,
                                                fs_interface,
                                                datagram.options,
                                                _clock=self._clock)
             reactor.listenUDP(0, session)
             returnValue(session)
         elif datagram.opcode == OP_RRQ:
             if mode == b'netascii':
                 fs_interface = NetasciiSenderProxy(fs_interface)
             session = RemoteOriginReadSession(addr,
                                               fs_interface,
                                               datagram.options,
                                               _clock=self._clock)
             reactor.listenUDP(0, session)
             returnValue(session)
 def test_conversion(self):
     p = NetasciiSenderProxy(self.source)
     chunk = yield p.read(2)
     self.assertEqual(len(chunk), 2)
     self.sink.write(chunk)
     last_chunk = False
     while chunk:
         chunk = yield p.read(2)
         # If a terminating chunk (len < blocknum) was already sent, there should
         # be no more data (means, we can only yield empty lines from now on)
         if last_chunk and chunk:
             print("LEN: %s" % len(chunk))
             self.fail("Last chunk (with len < blocksize) was already yielded, "
                       "but there is more data.")
         if len(chunk) < 2:
             last_chunk = True
         self.sink.write(chunk)
     self.sink.seek(0)
     self.assertEqual(self.sink.read(), to_netascii(self.test_data))
 def test_conversion(self):
     p = NetasciiSenderProxy(self.source)
     chunk = yield p.read(2)
     self.assertEqual(len(chunk), 2)
     self.sink.write(chunk)
     last_chunk = False
     while chunk:
         chunk = yield p.read(2)
         # If a terminating chunk (len < blocknum) was already sent, there should
         # be no more data (means, we can only yield empty lines from now on)
         if last_chunk and chunk:
             print("LEN: %s" % len(chunk))
             self.fail(
                 "Last chunk (with len < blocksize) was already yielded, "
                 "but there is more data.")
         if len(chunk) < 2:
             last_chunk = True
         self.sink.write(chunk)
     self.sink.seek(0)
     self.assertEqual(self.sink.read(), to_netascii(self.test_data))
Exemple #4
0
                ERRORDatagram.from_code(ERR_ACCESS_VIOLATION).to_wire(), addr)
        except FileExists:
            self.transport.write(
                ERRORDatagram.from_code(ERR_FILE_EXISTS).to_wire(), addr)
        except FileNotFound:
            self.transport.write(
                ERRORDatagram.from_code(ERR_FILE_NOT_FOUND).to_wire(), addr)
        except BackendError, e:
            self.transport.write(
                ERRORDatagram.from_code(ERR_NOT_DEFINED, str(e)).to_wire(),
                addr)
        else:
            if datagram.opcode == OP_WRQ:
                if mode == 'netascii':
                    fs_interface = NetasciiReceiverProxy(fs_interface)
                session = RemoteOriginWriteSession(addr,
                                                   fs_interface,
                                                   datagram.options,
                                                   _clock=self._clock)
                reactor.listenUDP(0, session)
                returnValue(session)
            elif datagram.opcode == OP_RRQ:
                if mode == 'netascii':
                    fs_interface = NetasciiSenderProxy(fs_interface)
                session = RemoteOriginReadSession(addr,
                                                  fs_interface,
                                                  datagram.options,
                                                  _clock=self._clock)
                reactor.listenUDP(0, session)
                returnValue(session)