diff options
Diffstat (limited to 'src/main/java/org/gnunet/transport/PeerState.java')
-rw-r--r-- | src/main/java/org/gnunet/transport/PeerState.java | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/main/java/org/gnunet/transport/PeerState.java b/src/main/java/org/gnunet/transport/PeerState.java new file mode 100644 index 0000000..538c189 --- /dev/null +++ b/src/main/java/org/gnunet/transport/PeerState.java | |||
@@ -0,0 +1,148 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.transport; | ||
22 | |||
23 | |||
24 | /** | ||
25 | * Possible state of a neighbour. Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED. | ||
26 | * | ||
27 | * Then, there are two main paths. If we receive a CONNECT message, we give | ||
28 | * the inbound address to ATS. After the check we ask ATS for a suggestion | ||
29 | * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we | ||
30 | * send our CONNECT_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK. | ||
31 | * If we receive a SESSION_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED | ||
32 | * (and notify everyone about the new connection). If the operation times out, | ||
33 | * we go to #GNUNET_TRANSPORT_PS_DISCONNECT. | ||
34 | * | ||
35 | * The other case is where we transmit a CONNECT message first. We | ||
36 | * start with #GNUNET_TRANSPORT_PS_INIT_ATS. If we get an address, we send | ||
37 | * the CONNECT message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT. | ||
38 | * Once we receive a CONNECT_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED | ||
39 | * (and notify everyone about the new connection and send | ||
40 | * back a SESSION_ACK). If the operation times out, we go to | ||
41 | * #GNUNET_TRANSPORT_PS_DISCONNECT. | ||
42 | * | ||
43 | * If the session is in trouble (i.e. transport-level disconnect or | ||
44 | * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a new | ||
45 | * address (we don't notify anyone about the disconnect yet). Once we | ||
46 | * have a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a | ||
47 | * CONNECT message. If we receive a | ||
48 | * CONNECT_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had | ||
49 | * trouble; we also send a SESSION_ACK at this time just in case. If | ||
50 | * the operation times out, we go to #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone | ||
51 | * about the lost connection). | ||
52 | * | ||
53 | * If ATS decides to switch addresses while we have a normal | ||
54 | * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT | ||
55 | * and send a SESSION_CONNECT. If we get a SESSION_ACK back, we switch the | ||
56 | * primary connection to the suggested alternative from ATS, go back | ||
57 | * to #GNUNET_TRANSPORT_PS_CONNECTED and send a SESSION_ACK to the other peer just to be | ||
58 | * sure. If the operation times out | ||
59 | * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given alternative | ||
60 | * address is "invalid"). | ||
61 | * | ||
62 | * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and then goes | ||
63 | * to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED). If we receive an explicit disconnect | ||
64 | * request, we can go from any state to #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after | ||
65 | * generating disconnect notifications. | ||
66 | * | ||
67 | * Note that it is quite possible that while we are in any of these | ||
68 | * states, we could receive a 'CONNECT' request from the other peer. | ||
69 | * We then enter a 'weird' state where we pursue our own primary state | ||
70 | * machine (as described above), but with the 'send_connect_ack' flag | ||
71 | * set to 1. If our state machine allows us to send a 'CONNECT_ACK' | ||
72 | * (because we have an acceptable address), we send the 'CONNECT_ACK' | ||
73 | * and set the 'send_connect_ack' to 2. If we then receive a | ||
74 | * 'SESSION_ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack' | ||
75 | * to 0). | ||
76 | * | ||
77 | */ | ||
78 | class PeerState | ||
79 | { | ||
80 | /** | ||
81 | * Fresh peer or completely disconnected | ||
82 | */ | ||
83 | public static final int NOT_CONNECTED = 0; | ||
84 | |||
85 | /** | ||
86 | * Asked to initiate connection, trying to get address from ATS | ||
87 | */ | ||
88 | public static final int INIT_ATS = 1; | ||
89 | |||
90 | /** | ||
91 | * Sent CONNECT message to other peer, waiting for CONNECT_ACK | ||
92 | */ | ||
93 | public static final int CONNECT_SENT = 2; | ||
94 | |||
95 | /** | ||
96 | * Received a CONNECT, asking ATS about address suggestions. | ||
97 | */ | ||
98 | public static final int CONNECT_RECV_ATS = 3; | ||
99 | |||
100 | /** | ||
101 | * CONNECT request from other peer was CONNECT_ACK'ed, waiting for | ||
102 | * SESSION_ACK. | ||
103 | */ | ||
104 | public static final int CONNECT_RECV_ACK = 4; | ||
105 | |||
106 | /** | ||
107 | * Got our CONNECT_ACK/SESSION_ACK, connection is up. | ||
108 | */ | ||
109 | public static final int CONNECTED = 5; | ||
110 | |||
111 | /** | ||
112 | * Connection got into trouble, rest of the system still believes | ||
113 | * it to be up, but we're getting a new address from ATS. | ||
114 | */ | ||
115 | public static final int RECONNECT_ATS = 6; | ||
116 | |||
117 | /** | ||
118 | * Sent CONNECT over new address (either by ATS telling us to switch | ||
119 | * addresses or from RECONNECT_ATS); if this fails, we need to tell | ||
120 | * the rest of the system about a disconnect. | ||
121 | */ | ||
122 | public static final int RECONNECT_SENT = 7; | ||
123 | |||
124 | /** | ||
125 | * We have some primary connection, but ATS suggested we switch | ||
126 | * to some alternative; we now sent a CONNECT message for the | ||
127 | * alternative session to the other peer and waiting for a | ||
128 | * CONNECT_ACK to make this our primary connection. | ||
129 | */ | ||
130 | public static final int CONNECTED_SWITCHING_CONNECT_SENT = 8; | ||
131 | |||
132 | /** | ||
133 | * Disconnect in progress (we're sending the DISCONNECT message to the | ||
134 | * other peer; after that is finished, the state will be cleaned up). | ||
135 | */ | ||
136 | public static final int DISCONNECT = 9; | ||
137 | |||
138 | /** | ||
139 | * We're finished with the disconnect; and are cleaning up the state | ||
140 | * now! We put the struct into this state when we are really in the | ||
141 | * task that calls 'free' on it and are about to remove the record | ||
142 | * from the map. We should never find a 'struct NeighbourMapEntry' | ||
143 | * in this state in the map. Accessing a 'struct NeighbourMapEntry' | ||
144 | * in this state virtually always means using memory that has been | ||
145 | * freed (the exception being the cleanup code in #free_neighbour()). | ||
146 | */ | ||
147 | public static final int DISCONNECT_FINISHED = 10; | ||
148 | } | ||