1 | from abc import ABC
|
---|
2 | from typing import TypeVar, Generic, List, Optional
|
---|
3 |
|
---|
4 | from tudelft.utilities.listener.Listenable import Listenable
|
---|
5 | from uri.uri import URI
|
---|
6 |
|
---|
7 | from geniusweb.references.Reference import Reference
|
---|
8 |
|
---|
9 |
|
---|
10 | INTYPE = TypeVar('INTYPE')
|
---|
11 | OUTTYPE = TypeVar('OUTTYPE')
|
---|
12 |
|
---|
13 | class ConnectionEnd(Listenable[INTYPE], Generic[INTYPE,OUTTYPE]):
|
---|
14 | '''
|
---|
15 | One end of a general two-way connection. incoming data is reported through
|
---|
16 | the {@link Listenable} channel, sending events of the INTYPE.
|
---|
17 | <p>
|
---|
18 |
|
---|
19 | The connection mechanism assumed here is fundamentally asymmetric. One side
|
---|
20 | is "Connectable", the other side is a ConnectionEnd created (usually from a
|
---|
21 | {@link Reference}. This matches the typical client-server system (web
|
---|
22 | architecture).
|
---|
23 |
|
---|
24 | <p>
|
---|
25 | If an internal error occurs, eg a socket failure, timeout, or parser error, a
|
---|
26 | null event is sent into the Listenable. {@link #getError()} can be called to
|
---|
27 | find out about the error.
|
---|
28 | @param <INTYPE> the type of incoming messages (incoming for the user of this
|
---|
29 | connection end). Incoming messages are received through
|
---|
30 | Listener#not. Incoming messages are usually asynchronous.
|
---|
31 | @param <OUTTYPE> the type of outgoing messages (outgoing for the user of this
|
---|
32 | connection end). Outgoing messages can be sent directly with
|
---|
33 | #send.
|
---|
34 | '''
|
---|
35 |
|
---|
36 | def send(self,data:OUTTYPE ):
|
---|
37 | '''
|
---|
38 | Send data out (and flush the output so that there are no excessive delays
|
---|
39 | in sending the data). This call is assumed to return immediately (never
|
---|
40 | block, eg on synchronized, Thread.sleep, IO, etc). When this is called
|
---|
41 | multiple times in sequence, the data should arrive at the receiver end in
|
---|
42 | the same order.
|
---|
43 |
|
---|
44 | @param data the data to be sent.
|
---|
45 | @throws ConnectionError if the data failed to be sent.
|
---|
46 | '''
|
---|
47 |
|
---|
48 | def getReference(self) -> Reference:
|
---|
49 | '''
|
---|
50 | @return Reference that was used to create this connection
|
---|
51 | '''
|
---|
52 |
|
---|
53 | def getRemoteURI(self)->URI:
|
---|
54 | '''
|
---|
55 | @return the URI of the remote endpoint that makes up the connection. This
|
---|
56 | is a URI that uniquely identifies the remote object.
|
---|
57 | '''
|
---|
58 |
|
---|
59 | def close(self):
|
---|
60 | '''
|
---|
61 | Close the connection. Should return immediately (not block). Before
|
---|
62 | really closing, this should attempt to send out possibly cached messages
|
---|
63 | before closing the connection.
|
---|
64 | '''
|
---|
65 | def getError(self) -> Optional[Exception]:
|
---|
66 | '''
|
---|
67 | @return the latest internal error, or null if no error occured. If the
|
---|
68 | channel is closed, this is set to {@link SocketException} "Socket
|
---|
69 | closed", even if the close was a "normal" termination eg when
|
---|
70 | {@link #close()} was called.
|
---|
71 | '''
|
---|
72 |
|
---|