Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julian Dehm
briar
Commits
e214c40b
Commit
e214c40b
authored
13 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Code cleanup and comments.
parent
0ef1fcb6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
components/net/sf/briar/transport/stream/StreamConnection.java
+51
-34
51 additions, 34 deletions
...nents/net/sf/briar/transport/stream/StreamConnection.java
with
51 additions
and
34 deletions
components/net/sf/briar/transport/stream/StreamConnection.java
+
51
−
34
View file @
e214c40b
...
...
@@ -52,9 +52,9 @@ abstract class StreamConnection implements DatabaseListener {
protected
final
ContactId
contactId
;
protected
final
StreamTransportConnection
connection
;
// The
following
fields must only be accessed with this's lock held
// The
se
fields must only be accessed with this's lock held
private
int
writerFlags
=
0
;
private
Collection
<
MessageId
>
o
utgoingO
ffer
=
null
;
private
Collection
<
MessageId
>
offer
ed
=
null
;
private
Collection
<
MessageId
>
requested
=
null
;
private
Offer
incomingOffer
=
null
;
...
...
@@ -107,6 +107,7 @@ abstract class StreamConnection implements DatabaseListener {
db
.
receiveBatch
(
contactId
,
b
);
}
else
if
(
proto
.
hasOffer
())
{
Offer
o
=
proto
.
readOffer
();
// Store the incoming offer and notify the writer
synchronized
(
this
)
{
writerFlags
|=
Flags
.
OFFER_RECEIVED
;
incomingOffer
=
o
;
...
...
@@ -114,31 +115,33 @@ abstract class StreamConnection implements DatabaseListener {
}
}
else
if
(
proto
.
hasRequest
())
{
Request
r
=
proto
.
readRequest
();
Collection
<
MessageId
>
offered
,
seen
,
unseen
;
// Retrieve the offered message IDs
Collection
<
MessageId
>
off
;
synchronized
(
this
)
{
if
(
o
utgoingO
ffer
==
null
)
if
(
offer
ed
==
null
)
throw
new
IOException
(
"Unexpected request packet"
);
offered
=
outgoingOffer
;
off
=
offered
;
offered
=
null
;
}
// Work out which messages were requested
BitSet
b
=
r
.
getBitmap
();
seen
=
new
Array
List
<
MessageId
>();
un
seen
=
new
Linked
List
<
MessageId
>();
Collection
<
MessageId
>
req
=
new
Linked
List
<
MessageId
>();
Collection
<
MessageId
>
seen
=
new
Array
List
<
MessageId
>();
int
i
=
0
;
for
(
MessageId
m
:
off
ered
)
{
if
(
b
.
get
(
i
++))
unseen
.
add
(
m
);
for
(
MessageId
m
:
off
)
{
if
(
b
.
get
(
i
++))
req
.
add
(
m
);
else
seen
.
add
(
m
);
}
// Mark the unrequested messages as seen
db
.
setSeen
(
contactId
,
seen
);
// Store the requested message IDs and notify the writer
synchronized
(
this
)
{
assert
outgoingOffer
!=
null
;
if
(
requested
!=
null
&&
!
requested
.
isEmpty
())
if
(
requested
!=
null
)
throw
new
IOException
(
"Unexpected request packet"
);
outgoingOffer
=
null
;
requested
=
unseen
;
requested
=
req
;
writerFlags
|=
Flags
.
REQUEST_RECEIVED
;
notifyAll
();
}
db
.
setSeen
(
contactId
,
seen
);
}
else
if
(
proto
.
hasSubscriptionUpdate
())
{
SubscriptionUpdate
s
=
proto
.
readSubscriptionUpdate
();
db
.
receiveSubscriptionUpdate
(
contactId
,
s
);
...
...
@@ -179,12 +182,12 @@ abstract class StreamConnection implements DatabaseListener {
sendAcks
(
ackWriter
);
State
state
=
State
.
SEND_OFFER
;
// Main loop
boolean
close
=
false
;
while
(!
close
)
{
while
(
true
)
{
int
flags
=
0
;
switch
(
state
)
{
case
SEND_OFFER:
// Try to send an offer
if
(
sendOffer
(
offerWriter
))
state
=
State
.
AWAIT_REQUEST
;
else
state
=
State
.
IDLE
;
break
;
...
...
@@ -203,8 +206,8 @@ abstract class StreamConnection implements DatabaseListener {
// Handle the flags in approximate order of urgency
if
((
flags
&
Flags
.
CONTACTS_UPDATED
)
!=
0
)
{
if
(!
db
.
getContacts
().
contains
(
contactId
))
{
c
lose
=
true
;
b
re
ak
;
c
onnection
.
dispose
(
true
)
;
re
turn
;
}
}
if
((
flags
&
Flags
.
TRANSPORTS_UPDATED
)
!=
0
)
{
...
...
@@ -220,6 +223,7 @@ abstract class StreamConnection implements DatabaseListener {
sendRequest
(
requestWriter
);
}
if
((
flags
&
Flags
.
REQUEST_RECEIVED
)
!=
0
)
{
// Should only be received in state AWAIT_REQUEST
throw
new
IOException
(
"Unexpected request packet"
);
}
if
((
flags
&
Flags
.
MESSAGES_ADDED
)
!=
0
)
{
...
...
@@ -241,8 +245,8 @@ abstract class StreamConnection implements DatabaseListener {
// Handle the flags in approximate order of urgency
if
((
flags
&
Flags
.
CONTACTS_UPDATED
)
!=
0
)
{
if
(!
db
.
getContacts
().
contains
(
contactId
))
{
c
lose
=
true
;
b
re
ak
;
c
onnection
.
dispose
(
true
)
;
re
turn
;
}
}
if
((
flags
&
Flags
.
TRANSPORTS_UPDATED
)
!=
0
)
{
...
...
@@ -274,8 +278,8 @@ abstract class StreamConnection implements DatabaseListener {
// Handle the flags in approximate order of urgency
if
((
flags
&
Flags
.
CONTACTS_UPDATED
)
!=
0
)
{
if
(!
db
.
getContacts
().
contains
(
contactId
))
{
c
lose
=
true
;
b
re
ak
;
c
onnection
.
dispose
(
true
)
;
re
turn
;
}
}
if
((
flags
&
Flags
.
TRANSPORTS_UPDATED
)
!=
0
)
{
...
...
@@ -291,12 +295,13 @@ abstract class StreamConnection implements DatabaseListener {
sendRequest
(
requestWriter
);
}
if
((
flags
&
Flags
.
REQUEST_RECEIVED
)
!=
0
)
{
// Should only be received in state AWAIT_REQUEST
throw
new
IOException
(
"Unexpected request packet"
);
}
if
((
flags
&
Flags
.
MESSAGES_ADDED
)
!=
0
)
{
// Ignored in this state
}
//
S
end a batch
if possible, otherwise an offer
//
Try to s
end a batch
if
(!
sendBatch
(
batchWriter
))
state
=
State
.
SEND_OFFER
;
break
;
}
...
...
@@ -317,35 +322,47 @@ abstract class StreamConnection implements DatabaseListener {
}
private
boolean
sendBatch
(
BatchWriter
b
)
throws
DbException
,
IOException
{
Collection
<
MessageId
>
ids
;
Collection
<
MessageId
>
req
;
// Retrieve the requested message IDs
synchronized
(
this
)
{
assert
o
utgoingO
ffer
==
null
;
assert
offer
ed
==
null
;
assert
requested
!=
null
;
ids
=
requested
;
req
=
requested
;
}
// Try to generate a batch, updating the collection of message IDs
boolean
anyAdded
=
db
.
generateBatch
(
contactId
,
b
,
req
);
// If no more batches can be generated, discard the remaining IDs
if
(!
anyAdded
)
{
synchronized
(
this
)
{
assert
offered
==
null
;
assert
requested
==
req
;
requested
=
null
;
}
}
boolean
anyAdded
=
db
.
generateBatch
(
contactId
,
b
,
ids
);
if
(!
anyAdded
)
ids
.
clear
();
return
anyAdded
;
}
private
boolean
sendOffer
(
OfferWriter
o
)
throws
DbException
,
IOException
{
Collection
<
MessageId
>
ids
=
db
.
generateOffer
(
contactId
,
o
);
// Generate an offer
Collection
<
MessageId
>
off
=
db
.
generateOffer
(
contactId
,
o
);
// Store the offered message IDs
synchronized
(
this
)
{
assert
o
utgoingO
ffer
==
null
;
assert
requested
==
null
||
requested
.
isEmpty
()
;
o
utgoingO
ffer
=
ids
;
assert
offer
ed
==
null
;
assert
requested
==
null
;
offer
ed
=
off
;
}
boolean
anyOffered
=
!
ids
.
isEmpty
();
return
anyOffered
;
return
!
off
.
isEmpty
();
}
private
void
sendRequest
(
RequestWriter
r
)
throws
DbException
,
IOException
{
Offer
o
;
// Retrieve the incoming offer
synchronized
(
this
)
{
assert
incomingOffer
!=
null
;
o
=
incomingOffer
;
incomingOffer
=
null
;
}
// Process the offer and generate a request
db
.
receiveOffer
(
contactId
,
o
,
r
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment