Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
briar
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Julian Dehm
briar
Commits
6925dfcb
Commit
6925dfcb
authored
Sep 18, 2018
by
akwizgran
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1240-message-refactoring' into 'master'
Remove raw representation from Message class See merge request
!915
parents
817df9c7
ba592821
Changes
32
Hide whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
176 additions
and
130 deletions
+176
-130
bramble-api/src/main/java/org/briarproject/bramble/api/client/BdfMessageValidator.java
.../briarproject/bramble/api/client/BdfMessageValidator.java
+4
-6
bramble-api/src/main/java/org/briarproject/bramble/api/sync/Message.java
.../main/java/org/briarproject/bramble/api/sync/Message.java
+9
-11
bramble-api/src/main/java/org/briarproject/bramble/api/sync/MessageFactory.java
...ava/org/briarproject/bramble/api/sync/MessageFactory.java
+1
-1
bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java
...rc/test/java/org/briarproject/bramble/test/TestUtils.java
+4
-5
bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java
...ava/org/briarproject/bramble/client/ClientHelperImpl.java
+2
-7
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java
...main/java/org/briarproject/bramble/db/DatabaseModule.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
...src/main/java/org/briarproject/bramble/db/H2Database.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
...in/java/org/briarproject/bramble/db/HyperSqlDatabase.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
...c/main/java/org/briarproject/bramble/db/JdbcDatabase.java
+12
-4
bramble-core/src/main/java/org/briarproject/bramble/sync/MessageFactoryImpl.java
...ava/org/briarproject/bramble/sync/MessageFactoryImpl.java
+9
-15
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
...riarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
+6
-2
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
...a/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
+6
-2
bramble-core/src/test/java/org/briarproject/bramble/client/BdfMessageValidatorTest.java
.../briarproject/bramble/client/BdfMessageValidatorTest.java
+6
-19
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseMigrationTest.java
...va/org/briarproject/bramble/db/DatabaseMigrationTest.java
+3
-0
bramble-core/src/test/java/org/briarproject/bramble/db/DatabasePerformanceComparisonTest.java
...project/bramble/db/DatabasePerformanceComparisonTest.java
+6
-2
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseTraceTest.java
...t/java/org/briarproject/bramble/db/DatabaseTraceTest.java
+5
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabasePerformanceTest.java
...rg/briarproject/bramble/db/H2DatabasePerformanceTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
...test/java/org/briarproject/bramble/db/H2DatabaseTest.java
+4
-6
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTraceTest.java
...java/org/briarproject/bramble/db/H2DatabaseTraceTest.java
+3
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2HyperSqlDatabasePerformanceComparisonTest.java
...amble/db/H2HyperSqlDatabasePerformanceComparisonTest.java
+6
-3
bramble-core/src/test/java/org/briarproject/bramble/db/H2MigrationTest.java
...est/java/org/briarproject/bramble/db/H2MigrationTest.java
+1
-1
bramble-core/src/test/java/org/briarproject/bramble/db/H2SelfDatabasePerformanceComparisonTest.java
...t/bramble/db/H2SelfDatabasePerformanceComparisonTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2SleepDatabasePerformanceComparisonTest.java
.../bramble/db/H2SleepDatabasePerformanceComparisonTest.java
+5
-3
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlDatabasePerformanceTest.java
...arproject/bramble/db/HyperSqlDatabasePerformanceTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlDatabaseTest.java
...ava/org/briarproject/bramble/db/HyperSqlDatabaseTest.java
+4
-6
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlMigrationTest.java
...va/org/briarproject/bramble/db/HyperSqlMigrationTest.java
+3
-3
bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
...st/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
+14
-10
bramble-core/src/test/java/org/briarproject/bramble/db/SingleDatabasePerformanceTest.java
...riarproject/bramble/db/SingleDatabasePerformanceTest.java
+5
-2
bramble-core/src/test/java/org/briarproject/bramble/sync/SyncIntegrationTest.java
...va/org/briarproject/bramble/sync/SyncIntegrationTest.java
+1
-1
bramble-core/src/test/java/org/briarproject/bramble/test/TestMessageFactory.java
...ava/org/briarproject/bramble/test/TestMessageFactory.java
+30
-0
bramble-core/src/test/java/org/briarproject/bramble/test/ValidatorTestCase.java
...java/org/briarproject/bramble/test/ValidatorTestCase.java
+0
-1
briar-core/src/test/java/org/briarproject/briar/introduction/MessageEncoderTest.java
...g/briarproject/briar/introduction/MessageEncoderTest.java
+3
-4
No files found.
bramble-api/src/main/java/org/briarproject/bramble/api/client/BdfMessageValidator.java
View file @
6925dfcb
...
@@ -16,7 +16,6 @@ import java.util.logging.Logger;
...
@@ -16,7 +16,6 @@ import java.util.logging.Logger;
import
javax.annotation.concurrent.Immutable
;
import
javax.annotation.concurrent.Immutable
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
@Immutable
@Immutable
...
@@ -49,14 +48,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
...
@@ -49,14 +48,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
throw
new
InvalidMessageException
(
throw
new
InvalidMessageException
(
"Timestamp is too far in the future"
);
"Timestamp is too far in the future"
);
}
}
byte
[]
raw
=
m
.
getRaw
();
byte
[]
body
=
m
.
getBody
();
if
(
raw
.
length
<=
MESSAGE_HEADER_LENGTH
)
{
if
(
body
.
length
==
0
)
{
throw
new
InvalidMessageException
(
"Message is too short"
);
throw
new
InvalidMessageException
(
"Message is too short"
);
}
}
try
{
try
{
BdfList
body
=
clientHelper
.
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
BdfList
bodyList
=
clientHelper
.
toList
(
body
);
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
BdfMessageContext
result
=
validateMessage
(
m
,
g
,
bodyList
);
BdfMessageContext
result
=
validateMessage
(
m
,
g
,
body
);
Metadata
meta
=
metadataEncoder
.
encode
(
result
.
getDictionary
());
Metadata
meta
=
metadataEncoder
.
encode
(
result
.
getDictionary
());
return
new
MessageContext
(
meta
,
result
.
getDependencies
());
return
new
MessageContext
(
meta
,
result
.
getDependencies
());
}
catch
(
FormatException
e
)
{
}
catch
(
FormatException
e
)
{
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/sync/Message.java
View file @
6925dfcb
package
org.briarproject.bramble.api.sync
;
package
org.briarproject.bramble.api.sync
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_
BODY_
LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
public
class
Message
{
public
class
Message
{
...
@@ -13,17 +13,15 @@ public class Message {
...
@@ -13,17 +13,15 @@ public class Message {
private
final
MessageId
id
;
private
final
MessageId
id
;
private
final
GroupId
groupId
;
private
final
GroupId
groupId
;
private
final
long
timestamp
;
private
final
long
timestamp
;
private
final
byte
[]
raw
;
private
final
byte
[]
body
;
public
Message
(
MessageId
id
,
GroupId
groupId
,
long
timestamp
,
byte
[]
raw
)
{
public
Message
(
MessageId
id
,
GroupId
groupId
,
long
timestamp
,
byte
[]
body
)
{
if
(
raw
.
length
<=
MESSAGE_HEADER_LENGTH
)
if
(
body
.
length
>
MAX_MESSAGE_BODY_LENGTH
)
throw
new
IllegalArgumentException
();
if
(
raw
.
length
>
MAX_MESSAGE_LENGTH
)
throw
new
IllegalArgumentException
();
throw
new
IllegalArgumentException
();
this
.
id
=
id
;
this
.
id
=
id
;
this
.
groupId
=
groupId
;
this
.
groupId
=
groupId
;
this
.
timestamp
=
timestamp
;
this
.
timestamp
=
timestamp
;
this
.
raw
=
raw
;
this
.
body
=
body
;
}
}
/**
/**
...
@@ -51,14 +49,14 @@ public class Message {
...
@@ -51,14 +49,14 @@ public class Message {
* Returns the length of the raw message in bytes.
* Returns the length of the raw message in bytes.
*/
*/
public
int
getRawLength
()
{
public
int
getRawLength
()
{
return
raw
.
length
;
return
MESSAGE_HEADER_LENGTH
+
body
.
length
;
}
}
/**
/**
* Returns the
raw message
.
* Returns the
message body
.
*/
*/
public
byte
[]
get
Raw
()
{
public
byte
[]
get
Body
()
{
return
raw
;
return
body
;
}
}
@Override
@Override
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/sync/MessageFactory.java
View file @
6925dfcb
...
@@ -9,5 +9,5 @@ public interface MessageFactory {
...
@@ -9,5 +9,5 @@ public interface MessageFactory {
Message
createMessage
(
byte
[]
raw
);
Message
createMessage
(
byte
[]
raw
);
Message
createMessage
(
MessageId
m
,
byte
[]
raw
);
byte
[]
getRawMessage
(
Message
m
);
}
}
bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java
View file @
6925dfcb
...
@@ -33,7 +33,6 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants
...
@@ -33,7 +33,6 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ClientId
.
MAX_CLIENT_ID_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ClientId
.
MAX_CLIENT_ID_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_GROUP_DESCRIPTOR_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_GROUP_DESCRIPTOR_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_BODY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_BODY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
public
class
TestUtils
{
public
class
TestUtils
{
...
@@ -132,13 +131,13 @@ public class TestUtils {
...
@@ -132,13 +131,13 @@ public class TestUtils {
public
static
Message
getMessage
(
GroupId
groupId
)
{
public
static
Message
getMessage
(
GroupId
groupId
)
{
int
bodyLength
=
1
+
random
.
nextInt
(
MAX_MESSAGE_BODY_LENGTH
);
int
bodyLength
=
1
+
random
.
nextInt
(
MAX_MESSAGE_BODY_LENGTH
);
return
getMessage
(
groupId
,
MESSAGE_HEADER_LENGTH
+
bodyLength
);
return
getMessage
(
groupId
,
bodyLength
);
}
}
public
static
Message
getMessage
(
GroupId
groupId
,
int
raw
Length
)
{
public
static
Message
getMessage
(
GroupId
groupId
,
int
body
Length
)
{
MessageId
id
=
new
MessageId
(
getRandomId
());
MessageId
id
=
new
MessageId
(
getRandomId
());
byte
[]
raw
=
getRandomBytes
(
raw
Length
);
byte
[]
body
=
getRandomBytes
(
body
Length
);
return
new
Message
(
id
,
groupId
,
timestamp
,
raw
);
return
new
Message
(
id
,
groupId
,
timestamp
,
body
);
}
}
public
static
double
getMedian
(
Collection
<?
extends
Number
>
samples
)
{
public
static
double
getMedian
(
Collection
<?
extends
Number
>
samples
)
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java
View file @
6925dfcb
...
@@ -41,7 +41,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
...
@@ -41,7 +41,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_PUBLIC_KEY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_PUBLIC_KEY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTIES_PER_TRANSPORT
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTIES_PER_TRANSPORT
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkLength
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkLength
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkSize
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkSize
;
...
@@ -147,9 +146,7 @@ class ClientHelperImpl implements ClientHelper {
...
@@ -147,9 +146,7 @@ class ClientHelperImpl implements ClientHelper {
@Override
@Override
public
BdfList
getMessageAsList
(
Transaction
txn
,
MessageId
m
)
public
BdfList
getMessageAsList
(
Transaction
txn
,
MessageId
m
)
throws
DbException
,
FormatException
{
throws
DbException
,
FormatException
{
byte
[]
raw
=
db
.
getMessage
(
txn
,
m
).
getRaw
();
return
toList
(
db
.
getMessage
(
txn
,
m
).
getBody
());
return
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
}
}
@Override
@Override
...
@@ -361,9 +358,7 @@ class ClientHelperImpl implements ClientHelper {
...
@@ -361,9 +358,7 @@ class ClientHelperImpl implements ClientHelper {
@Override
@Override
public
BdfList
toList
(
Message
m
)
throws
FormatException
{
public
BdfList
toList
(
Message
m
)
throws
FormatException
{
byte
[]
raw
=
m
.
getRaw
();
return
toList
(
m
.
getBody
());
return
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
}
}
@Override
@Override
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java
View file @
6925dfcb
...
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
...
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.event.EventBus
;
import
org.briarproject.bramble.api.event.EventBus
;
import
org.briarproject.bramble.api.lifecycle.ShutdownManager
;
import
org.briarproject.bramble.api.lifecycle.ShutdownManager
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.api.system.Clock
;
import
java.sql.Connection
;
import
java.sql.Connection
;
...
@@ -18,8 +19,9 @@ public class DatabaseModule {
...
@@ -18,8 +19,9 @@ public class DatabaseModule {
@Provides
@Provides
@Singleton
@Singleton
Database
<
Connection
>
provideDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
Database
<
Connection
>
provideDatabase
(
DatabaseConfig
config
,
return
new
H2Database
(
config
,
clock
);
MessageFactory
messageFactory
,
Clock
clock
)
{
return
new
H2Database
(
config
,
messageFactory
,
clock
);
}
}
@Provides
@Provides
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
View file @
6925dfcb
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.util.StringUtils
;
import
org.briarproject.bramble.util.StringUtils
;
...
@@ -36,9 +37,10 @@ class H2Database extends JdbcDatabase {
...
@@ -36,9 +37,10 @@ class H2Database extends JdbcDatabase {
private
volatile
SecretKey
key
=
null
;
private
volatile
SecretKey
key
=
null
;
@Inject
@Inject
H2Database
(
DatabaseConfig
config
,
Clock
clock
)
{
H2Database
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
clock
);
messageFactory
,
clock
);
this
.
config
=
config
;
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
View file @
6925dfcb
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.util.StringUtils
;
import
org.briarproject.bramble.util.StringUtils
;
...
@@ -37,9 +38,10 @@ class HyperSqlDatabase extends JdbcDatabase {
...
@@ -37,9 +38,10 @@ class HyperSqlDatabase extends JdbcDatabase {
private
volatile
SecretKey
key
=
null
;
private
volatile
SecretKey
key
=
null
;
@Inject
@Inject
HyperSqlDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
HyperSqlDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
clock
);
messageFactory
,
clock
);
this
.
config
=
config
;
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
View file @
6925dfcb
...
@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.sync.Group;
...
@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.sync.Group;
import
org.briarproject.bramble.api.sync.Group.Visibility
;
import
org.briarproject.bramble.api.sync.Group.Visibility
;
import
org.briarproject.bramble.api.sync.GroupId
;
import
org.briarproject.bramble.api.sync.GroupId
;
import
org.briarproject.bramble.api.sync.Message
;
import
org.briarproject.bramble.api.sync.Message
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.sync.MessageId
;
import
org.briarproject.bramble.api.sync.MessageId
;
import
org.briarproject.bramble.api.sync.MessageStatus
;
import
org.briarproject.bramble.api.sync.MessageStatus
;
import
org.briarproject.bramble.api.sync.ValidationManager.State
;
import
org.briarproject.bramble.api.sync.ValidationManager.State
;
...
@@ -61,6 +62,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
...
@@ -61,6 +62,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
INVISIBLE
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
INVISIBLE
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
SHARED
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
SHARED
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
VISIBLE
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
Group
.
Visibility
.
VISIBLE
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
DELIVERED
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
DELIVERED
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
PENDING
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
PENDING
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
UNKNOWN
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ValidationManager
.
State
.
UNKNOWN
;
...
@@ -305,6 +307,7 @@ abstract class JdbcDatabase implements Database<Connection> {
...
@@ -305,6 +307,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Different database libraries use different names for certain types
// Different database libraries use different names for certain types
private
final
String
hashType
,
secretType
,
binaryType
;
private
final
String
hashType
,
secretType
,
binaryType
;
private
final
String
counterType
,
stringType
;
private
final
String
counterType
,
stringType
;
private
final
MessageFactory
messageFactory
;
private
final
Clock
clock
;
private
final
Clock
clock
;
// Locking: connectionsLock
// Locking: connectionsLock
...
@@ -320,12 +323,14 @@ abstract class JdbcDatabase implements Database<Connection> {
...
@@ -320,12 +323,14 @@ abstract class JdbcDatabase implements Database<Connection> {
private
final
Condition
connectionsChanged
=
connectionsLock
.
newCondition
();
private
final
Condition
connectionsChanged
=
connectionsLock
.
newCondition
();
JdbcDatabase
(
String
hashType
,
String
secretType
,
String
binaryType
,
JdbcDatabase
(
String
hashType
,
String
secretType
,
String
binaryType
,
String
counterType
,
String
stringType
,
Clock
clock
)
{
String
counterType
,
String
stringType
,
MessageFactory
messageFactory
,
Clock
clock
)
{
this
.
hashType
=
hashType
;
this
.
hashType
=
hashType
;
this
.
secretType
=
secretType
;
this
.
secretType
=
secretType
;
this
.
binaryType
=
binaryType
;
this
.
binaryType
=
binaryType
;
this
.
counterType
=
counterType
;
this
.
counterType
=
counterType
;
this
.
stringType
=
stringType
;
this
.
stringType
=
stringType
;
this
.
messageFactory
=
messageFactory
;
this
.
clock
=
clock
;
this
.
clock
=
clock
;
}
}
...
@@ -727,7 +732,7 @@ abstract class JdbcDatabase implements Database<Connection> {
...
@@ -727,7 +732,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps
.
setLong
(
3
,
m
.
getTimestamp
());
ps
.
setLong
(
3
,
m
.
getTimestamp
());
ps
.
setInt
(
4
,
state
.
getValue
());
ps
.
setInt
(
4
,
state
.
getValue
());
ps
.
setBoolean
(
5
,
messageShared
);
ps
.
setBoolean
(
5
,
messageShared
);
byte
[]
raw
=
m
.
getRaw
(
);
byte
[]
raw
=
m
essageFactory
.
getRawMessage
(
m
);
ps
.
setInt
(
6
,
raw
.
length
);
ps
.
setInt
(
6
,
raw
.
length
);
ps
.
setBytes
(
7
,
raw
);
ps
.
setBytes
(
7
,
raw
);
int
affected
=
ps
.
executeUpdate
();
int
affected
=
ps
.
executeUpdate
();
...
@@ -741,7 +746,7 @@ abstract class JdbcDatabase implements Database<Connection> {
...
@@ -741,7 +746,7 @@ abstract class JdbcDatabase implements Database<Connection> {
boolean
offered
=
removeOfferedMessage
(
txn
,
c
,
m
.
getId
());
boolean
offered
=
removeOfferedMessage
(
txn
,
c
,
m
.
getId
());
boolean
seen
=
offered
||
(
sender
!=
null
&&
c
.
equals
(
sender
));
boolean
seen
=
offered
||
(
sender
!=
null
&&
c
.
equals
(
sender
));
addStatus
(
txn
,
m
.
getId
(),
c
,
m
.
getGroupId
(),
m
.
getTimestamp
(),
addStatus
(
txn
,
m
.
getId
(),
c
,
m
.
getGroupId
(),
m
.
getTimestamp
(),
m
.
getRawLength
()
,
state
,
e
.
getValue
(),
messageShared
,
raw
.
length
,
state
,
e
.
getValue
(),
messageShared
,
false
,
seen
);
false
,
seen
);
}
}
// Update denormalised column in messageDependencies if dependency
// Update denormalised column in messageDependencies if dependency
...
@@ -1501,7 +1506,10 @@ abstract class JdbcDatabase implements Database<Connection> {
...
@@ -1501,7 +1506,10 @@ abstract class JdbcDatabase implements Database<Connection> {
rs
.
close
();
rs
.
close
();
ps
.
close
();
ps
.
close
();
if
(
raw
==
null
)
throw
new
MessageDeletedException
();
if
(
raw
==
null
)
throw
new
MessageDeletedException
();
return
new
Message
(
m
,
g
,
timestamp
,
raw
);
if
(
raw
.
length
<
MESSAGE_HEADER_LENGTH
)
throw
new
AssertionError
();
byte
[]
body
=
new
byte
[
raw
.
length
-
MESSAGE_HEADER_LENGTH
];
System
.
arraycopy
(
raw
,
MESSAGE_HEADER_LENGTH
,
body
,
0
,
body
.
length
);
return
new
Message
(
m
,
g
,
timestamp
,
body
);
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
tryToClose
(
rs
);
tryToClose
(
rs
);
tryToClose
(
ps
);
tryToClose
(
ps
);
...
...
bramble-core/src/main/java/org/briarproject/bramble/sync/MessageFactoryImpl.java
View file @
6925dfcb
...
@@ -39,11 +39,7 @@ class MessageFactoryImpl implements MessageFactory {
...
@@ -39,11 +39,7 @@ class MessageFactoryImpl implements MessageFactory {
if
(
body
.
length
>
MAX_MESSAGE_BODY_LENGTH
)
if
(
body
.
length
>
MAX_MESSAGE_BODY_LENGTH
)
throw
new
IllegalArgumentException
();
throw
new
IllegalArgumentException
();
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
byte
[]
raw
=
new
byte
[
MESSAGE_HEADER_LENGTH
+
body
.
length
];
return
new
Message
(
id
,
g
,
timestamp
,
body
);
System
.
arraycopy
(
g
.
getBytes
(),
0
,
raw
,
0
,
UniqueId
.
LENGTH
);
ByteUtils
.
writeUint64
(
timestamp
,
raw
,
UniqueId
.
LENGTH
);
System
.
arraycopy
(
body
,
0
,
raw
,
MESSAGE_HEADER_LENGTH
,
body
.
length
);
return
new
Message
(
id
,
g
,
timestamp
,
raw
);
}
}
private
MessageId
getMessageId
(
GroupId
g
,
long
timestamp
,
byte
[]
body
)
{
private
MessageId
getMessageId
(
GroupId
g
,
long
timestamp
,
byte
[]
body
)
{
...
@@ -69,18 +65,16 @@ class MessageFactoryImpl implements MessageFactory {
...
@@ -69,18 +65,16 @@ class MessageFactoryImpl implements MessageFactory {
byte
[]
body
=
new
byte
[
raw
.
length
-
MESSAGE_HEADER_LENGTH
];
byte
[]
body
=
new
byte
[
raw
.
length
-
MESSAGE_HEADER_LENGTH
];
System
.
arraycopy
(
raw
,
MESSAGE_HEADER_LENGTH
,
body
,
0
,
body
.
length
);
System
.
arraycopy
(
raw
,
MESSAGE_HEADER_LENGTH
,
body
,
0
,
body
.
length
);
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
return
new
Message
(
id
,
g
,
timestamp
,
raw
);
return
new
Message
(
id
,
g
,
timestamp
,
body
);
}
}
@Override
@Override
public
Message
createMessage
(
MessageId
m
,
byte
[]
raw
)
{
public
byte
[]
getRawMessage
(
Message
m
)
{
if
(
raw
.
length
<
MESSAGE_HEADER_LENGTH
)
byte
[]
body
=
m
.
getBody
();
throw
new
IllegalArgumentException
();
byte
[]
raw
=
new
byte
[
MESSAGE_HEADER_LENGTH
+
body
.
length
];
if
(
raw
.
length
>
MAX_MESSAGE_LENGTH
)
System
.
arraycopy
(
m
.
getGroupId
().
getBytes
(),
0
,
raw
,
0
,
UniqueId
.
LENGTH
);
throw
new
IllegalArgumentException
();
ByteUtils
.
writeUint64
(
m
.
getTimestamp
(),
raw
,
UniqueId
.
LENGTH
);
byte
[]
groupId
=
new
byte
[
UniqueId
.
LENGTH
];
System
.
arraycopy
(
body
,
0
,
raw
,
MESSAGE_HEADER_LENGTH
,
body
.
length
);
System
.
arraycopy
(
raw
,
0
,
groupId
,
0
,
UniqueId
.
LENGTH
);
return
raw
;
long
timestamp
=
ByteUtils
.
readUint64
(
raw
,
UniqueId
.
LENGTH
);
return
new
Message
(
m
,
new
GroupId
(
groupId
),
timestamp
,
raw
);
}
}
}
}
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
View file @
6925dfcb
...
@@ -3,6 +3,7 @@ package org.briarproject.bramble.sync;
...
@@ -3,6 +3,7 @@ package org.briarproject.bramble.sync;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.record.RecordWriterFactory
;
import
org.briarproject.bramble.api.record.RecordWriterFactory
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.sync.SyncRecordWriter
;
import
org.briarproject.bramble.api.sync.SyncRecordWriter
;
import
org.briarproject.bramble.api.sync.SyncRecordWriterFactory
;
import
org.briarproject.bramble.api.sync.SyncRecordWriterFactory
;
...
@@ -13,16 +14,19 @@ import javax.inject.Inject;
...
@@ -13,16 +14,19 @@ import javax.inject.Inject;
@NotNullByDefault
@NotNullByDefault
class
SyncRecordWriterFactoryImpl
implements
SyncRecordWriterFactory
{
class
SyncRecordWriterFactoryImpl
implements
SyncRecordWriterFactory
{
private
final
MessageFactory
messageFactory
;
private
final
RecordWriterFactory
recordWriterFactory
;
private
final
RecordWriterFactory
recordWriterFactory
;
@Inject
@Inject
SyncRecordWriterFactoryImpl
(
RecordWriterFactory
recordWriterFactory
)
{
SyncRecordWriterFactoryImpl
(
MessageFactory
messageFactory
,
RecordWriterFactory
recordWriterFactory
)
{
this
.
messageFactory
=
messageFactory
;
this
.
recordWriterFactory
=
recordWriterFactory
;
this
.
recordWriterFactory
=
recordWriterFactory
;
}
}
@Override
@Override
public
SyncRecordWriter
createRecordWriter
(
OutputStream
out
)
{
public
SyncRecordWriter
createRecordWriter
(
OutputStream
out
)
{
RecordWriter
writer
=
recordWriterFactory
.
createRecordWriter
(
out
);
RecordWriter
writer
=
recordWriterFactory
.
createRecordWriter
(
out
);
return
new
SyncRecordWriterImpl
(
writer
);
return
new
SyncRecordWriterImpl
(
messageFactory
,
writer
);
}
}
}
}
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
View file @
6925dfcb
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.record.Record;
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.record.Record;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.sync.Ack
;
import
org.briarproject.bramble.api.sync.Ack
;
import
org.briarproject.bramble.api.sync.Message
;
import
org.briarproject.bramble.api.sync.Message
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.sync.MessageId
;
import
org.briarproject.bramble.api.sync.MessageId
;
import
org.briarproject.bramble.api.sync.Offer
;
import
org.briarproject.bramble.api.sync.Offer
;
import
org.briarproject.bramble.api.sync.Request
;
import
org.briarproject.bramble.api.sync.Request
;
...
@@ -25,10 +26,12 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
...
@@ -25,10 +26,12 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
@NotNullByDefault
@NotNullByDefault
class
SyncRecordWriterImpl
implements
SyncRecordWriter
{
class
SyncRecordWriterImpl
implements
SyncRecordWriter
{
private
final
MessageFactory
messageFactory
;
private
final
RecordWriter
writer
;
private
final
RecordWriter
writer
;
private
final
ByteArrayOutputStream
payload
=
new
ByteArrayOutputStream
();
private
final
ByteArrayOutputStream
payload
=
new
ByteArrayOutputStream
();
SyncRecordWriterImpl
(
RecordWriter
writer
)
{
SyncRecordWriterImpl
(
MessageFactory
messageFactory
,
RecordWriter
writer
)
{
this
.
messageFactory
=
messageFactory
;
this
.
writer
=
writer
;
this
.
writer
=
writer
;
}
}
...
@@ -46,7 +49,8 @@ class SyncRecordWriterImpl implements SyncRecordWriter {
...
@@ -46,7 +49,8 @@ class SyncRecordWriterImpl implements SyncRecordWriter {
@Override
@Override
public
void
writeMessage
(
Message
m
)
throws
IOException
{
public
void
writeMessage
(
Message
m
)
throws
IOException
{
writer
.
writeRecord
(
new
Record
(
PROTOCOL_VERSION
,
MESSAGE
,
m
.
getRaw
()));
byte
[]
raw
=
messageFactory
.
getRawMessage
(
m
);
writer
.
writeRecord
(
new
Record
(
PROTOCOL_VERSION
,
MESSAGE
,
raw
));
}
}
@Override
@Override
...
...
bramble-core/src/test/java/org/briarproject/bramble/client/BdfMessageValidatorTest.java
View file @
6925dfcb
...
@@ -16,10 +16,8 @@ import org.jmock.Expectations;
...
@@ -16,10 +16,8 @@ import org.jmock.Expectations;
import
org.jmock.lib.legacy.ClassImposteriser
;
import
org.jmock.lib.legacy.ClassImposteriser
;
import
org.junit.Test
;
import
org.junit.Test
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getMessage
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getMessage
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomBytes
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertSame
;
...
@@ -58,8 +56,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
...
@@ -58,8 +56,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
context
.
checking
(
new
Expectations
()
{{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
clock
).
currentTimeMillis
();
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
-
MAX_CLOCK_DIFFERENCE
));
will
(
returnValue
(
timestamp
-
MAX_CLOCK_DIFFERENCE
));
oneOf
(
clientHelper
).
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
oneOf
(
clientHelper
).
toList
(
message
.
getBody
());
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
will
(
returnValue
(
body
));
will
(
returnValue
(
body
));
oneOf
(
metadataEncoder
).
encode
(
dictionary
);
oneOf
(
metadataEncoder
).
encode
(
dictionary
);
will
(
returnValue
(
meta
));
will
(
returnValue
(
meta
));
...
@@ -84,18 +81,11 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
...
@@ -84,18 +81,11 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {