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
239e532f
Commit
239e532f
authored
12 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
A ScheduledExecutorService is overkill for one task; use a Thread.
parent
80ac368c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
briar-core/src/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
+11
-16
11 additions, 16 deletions
...e/src/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
with
11 additions
and
16 deletions
briar-core/src/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
+
11
−
16
View file @
239e532f
package
net.sf.briar.plugins.bluetooth
;
package
net.sf.briar.plugins.bluetooth
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
MILLISECONDS
;
import
static
java
.
util
.
logging
.
Level
.
INFO
;
import
static
java
.
util
.
logging
.
Level
.
INFO
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
javax
.
bluetooth
.
DiscoveryAgent
.
GIAC
;
import
static
javax
.
bluetooth
.
DiscoveryAgent
.
GIAC
;
...
@@ -12,9 +11,6 @@ import java.util.Map;
...
@@ -12,9 +11,6 @@ import java.util.Map;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.UUID
;
import
java.util.UUID
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ScheduledFuture
;
import
java.util.concurrent.Semaphore
;
import
java.util.concurrent.Semaphore
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
...
@@ -55,12 +51,9 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -55,12 +51,9 @@ class BluetoothPlugin implements DuplexPlugin {
private
final
DuplexPluginCallback
callback
;
private
final
DuplexPluginCallback
callback
;
private
final
long
maxLatency
,
pollingInterval
;
private
final
long
maxLatency
,
pollingInterval
;
private
final
Semaphore
discoverySemaphore
=
new
Semaphore
(
1
);
private
final
Semaphore
discoverySemaphore
=
new
Semaphore
(
1
);
private
final
ScheduledExecutorService
scheduler
;
private
volatile
boolean
running
=
false
;
private
volatile
boolean
running
=
false
;
private
volatile
StreamConnectionNotifier
socket
=
null
;
private
volatile
StreamConnectionNotifier
socket
=
null
;
// Non-null if running has ever been true
private
volatile
LocalDevice
localDevice
=
null
;
private
volatile
LocalDevice
localDevice
=
null
;
BluetoothPlugin
(
Executor
pluginExecutor
,
Clock
clock
,
BluetoothPlugin
(
Executor
pluginExecutor
,
Clock
clock
,
...
@@ -72,7 +65,6 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -72,7 +65,6 @@ class BluetoothPlugin implements DuplexPlugin {
this
.
callback
=
callback
;
this
.
callback
=
callback
;
this
.
maxLatency
=
maxLatency
;
this
.
maxLatency
=
maxLatency
;
this
.
pollingInterval
=
pollingInterval
;
this
.
pollingInterval
=
pollingInterval
;
scheduler
=
Executors
.
newScheduledThreadPool
(
0
);
}
}
public
TransportId
getId
()
{
public
TransportId
getId
()
{
...
@@ -176,7 +168,6 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -176,7 +168,6 @@ class BluetoothPlugin implements DuplexPlugin {
public
void
stop
()
{
public
void
stop
()
{
running
=
false
;
running
=
false
;
if
(
socket
!=
null
)
tryToClose
(
socket
);
if
(
socket
!=
null
)
tryToClose
(
socket
);
scheduler
.
shutdownNow
();
}
}
public
boolean
shouldPoll
()
{
public
boolean
shouldPoll
()
{
...
@@ -276,7 +267,7 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -276,7 +267,7 @@ class BluetoothPlugin implements DuplexPlugin {
}
}
public
DuplexTransportConnection
acceptInvitation
(
PseudoRandom
r
,
public
DuplexTransportConnection
acceptInvitation
(
PseudoRandom
r
,
long
timeout
)
{
final
long
timeout
)
{
if
(!
running
)
return
null
;
if
(!
running
)
return
null
;
// Use the same pseudo-random UUID as the contact
// Use the same pseudo-random UUID as the contact
byte
[]
b
=
r
.
nextBytes
(
UUID_BYTES
);
byte
[]
b
=
r
.
nextBytes
(
UUID_BYTES
);
...
@@ -297,13 +288,19 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -297,13 +288,19 @@ class BluetoothPlugin implements DuplexPlugin {
return
null
;
return
null
;
}
}
// Close the socket when the invitation times out
// Close the socket when the invitation times out
Runnable
close
=
new
Runnable
()
{
new
Thread
()
{
@Override
public
void
run
()
{
public
void
run
()
{
try
{
Thread
.
sleep
(
timeout
);
}
catch
(
InterruptedException
e
)
{
if
(
LOG
.
isLoggable
(
WARNING
))
LOG
.
warning
(
"Interrupted while waiting for invitation"
);
}
tryToClose
(
scn
);
tryToClose
(
scn
);
}
}
};
}.
start
();
ScheduledFuture
<?>
f
=
scheduler
.
schedule
(
close
,
timeout
,
MILLISECONDS
);
// Try to accept a connection
// Try to accept a connection and close the socket
try
{
try
{
StreamConnection
s
=
scn
.
acceptAndOpen
();
StreamConnection
s
=
scn
.
acceptAndOpen
();
return
new
BluetoothTransportConnection
(
s
,
maxLatency
);
return
new
BluetoothTransportConnection
(
s
,
maxLatency
);
...
@@ -311,8 +308,6 @@ class BluetoothPlugin implements DuplexPlugin {
...
@@ -311,8 +308,6 @@ class BluetoothPlugin implements DuplexPlugin {
// This is expected when the socket is closed
// This is expected when the socket is closed
if
(
LOG
.
isLoggable
(
INFO
))
LOG
.
log
(
INFO
,
e
.
toString
(),
e
);
if
(
LOG
.
isLoggable
(
INFO
))
LOG
.
log
(
INFO
,
e
.
toString
(),
e
);
return
null
;
return
null
;
}
finally
{
if
(
f
.
cancel
(
false
))
tryToClose
(
scn
);
}
}
}
}
...
...
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