i2p.samclasses
index
d:\code\i2p\i2p\samclasses.py

Lower-level SAM API, interfaces with SAM Bridge.
 
For internal use only.
 
Use the higher level i2p.sam module for your own programs.
 
For details on SAM, see "Simple Anonymous Messaging (SAM) v1.0,"
as published by jrandom.
 
Class Overview:
 
  SAMTerminal:     Message sender/reader, talks to SAM Bridge
                   through a single socket.
  StringBuffer:    Queue for character data.
  BaseSession:     SAM session classes are derived from this.
  StreamSession:   Manipulate a SAM stream session through a
                   threadsafe, high-level interface.
  DatagramSession: SAM datagram session, threadsafe, high level.
  RawSession:      SAM raw session, threadsafe, high level.
 
Note that a 'None' timeout is an infinite timeout: it
blocks forever if necessary.
 
Todo:
  * Error handling is a huge mess.  Neaten it up.
    Subclass a ErrorMixin class, then use set_error(e),
    check_error(), get_error().
  * Streams are a huge mess.  Neaten them up.
  * This whole interface is a tad confusing.  Neaten it up.

 
Modules
       
Queue
i2p
random
shlex
socket
string
sys
thread
threading
time
traceback

 
Classes
       
BaseSession
DatagramSession
RawSession
StreamSession
Deque
StringBuffer
SAMTerminal
Stream

 
class BaseSession
    Base session, from which StreamSessionDatagramSession,
and RawSession are derived.
 
  Methods defined here:
__init__(self, addr='')
close(self)
Close the session.

 
class DatagramSession(BaseSession)
    Datagram session.  All methods are blocking and threadsafe.
 
  Methods defined here:
__init__(self, name, addr='', **kwargs)
__len__(self)
Number of packets in read buffer.
recv(self, timeout=None, peek=False)
Get a single packet.  Blocks for up to timeout seconds if
n > 0 and no packet is available (timeout=None means wait
forever).  If still no packet is available, raises BlockError
or Timeout.  Returns the pair (data, address).  If peek is
True, the data is not removed.
send(self, s, dest)
Send packet with contents s to given destination.

Methods inherited from BaseSession:
close(self)
Close the session.

 
class Deque
    A double-ended queue.
 
  Methods defined here:
__init__(self)
__len__(self)
Number of items in the deque.
pop_first(self)
Pop an item off the beginning of the deque, and return it.
pop_last(self)
Pop an item off the end of the deque, and return it.
push_first(self, obj)
Prepend obj to the beginning of the deque.
push_last(self, obj)
Append obj to the end of the deque.

 
class RawSession(BaseSession)
    Raw session.  All methods are blocking and threadsafe.
 
  Methods defined here:
__init__(self, name, addr='', **kwargs)
__len__(self)
Number of packets in read buffer.
recv(self, timeout=None, peek=False)
Identical to DatagramSocket.recv.  The from address is an
empty string.
send(self, s, dest)
Send packet with contents s to given destination.

Methods inherited from BaseSession:
close(self)
Close the session.

 
class SAMTerminal
    Message-by-message communication with SAM through a single
socket.  _on_* messages are dispatched to msgobj.
 
  Methods defined here:
__init__(self, addr, msgobj)
check(self)
Raise an error if terminal was closed, otherwise do
nothing.
check_message(self, kwargs)
Raises an error if kwargs['RESULT'] != 'OK'.
close(self)
Close the SAM terminal.
on_message(self, msg, kwargs)
Process a SAM message that was received.  Dispatch to
_on_MESSAGE_NAME(**kwargs).
queue_get(self, q)
Identical to q.get() unless a call to check() fails,
in which case the waiting is cut short with an error.
send_message(self, msg)
Send a message to the SAM bridge.  A newline will be
automatically added if none is present.

 
class Stream
    Receives and sends data for an individual stream.
 
  Methods defined here:
__del__(self)
__init__(self, parent, remotedest, id, didconnect=True)
__len__(self)
Current length of read buffer.
close(self)
Close the stream.  Threadsafe.
on_close(self, e)
on_receive(self, s)
recv(self, n, timeout=None, peek=False, waitall=False)
Reads up to n bytes in a manner identical to socket.recv.
Blocks for up to timeout seconds if n > 0 and no data is
available (timeout=None means wait forever).  If still no data
is available, raises BlockError or Timeout.  For a closed
stream, recv will read the data stored in the buffer until
EOF, at which point the read data will be truncated.  If peek
is True, the data is not removed.  If waitall is True, reads
exactly n bytes, or raises BlockError or Timeout as
appropriate.  Returns data.
send(self, s)
Sends the string s, blocking if necessary.

 
class StreamSession(BaseSession)
    Stream session.  All methods are blocking and threadsafe.
 
  Methods defined here:
__init__(self, name, addr='', **kwargs)
__len__(self)
Unconnected session; has no read data available.
accept(self, timeout=None)
Wait for incoming connection, and return a Stream object
for it.
connect(self, dest, timeout=None)
Create a stream connected to remote destination 'dest'.  The
id is random.  If the timeout is exceeded, do NOT raise an
error; rather, return a Stream object with .didconnect set
to False.
listen(self, backlog)
Set maximum number of queued connections.

Methods inherited from BaseSession:
close(self)
Close the session.

 
class StringBuffer(Deque)
    A FIFO for characters.  Strings can be efficiently
appended to the end, and read from the beginning.
 
Example:
  B = StringBuffer('Hello W')
  B.append('orld!')
  print B.read(5)        # 'Hello'
  print B.read()         # 'World!'
 
  Methods defined here:
__init__(self, s='')
__len__(self)
__repr__(self)
__str__(self)
append(self, s)
Append string data to the end of the buffer.
peek(self, n=None)
Like read(), but do not remove the data that is returned.
prepend(self, s)
Prepend string data to the beginning of the buffer.
read(self, n=None)
Read n bytes of data (or less if less data available) from the
beginning of the buffer.  The data is removed.  If n is
omitted, read the entire buffer.

Methods inherited from Deque:
pop_first(self)
Pop an item off the beginning of the deque, and return it.
pop_last(self)
Pop an item off the end of the deque, and return it.
push_first(self, obj)
Prepend obj to the beginning of the deque.
push_last(self, obj)
Append obj to the end of the deque.

 
Data
        sam_log = False