Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
9cb98020
Commit
9cb98020
authored
5 years ago
by
Terry D. Norbraten
Browse files
Options
Downloads
Patches
Plain Diff
minimize object creation in loops
parent
245ebf9d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
+48
-24
48 additions, 24 deletions
src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
with
48 additions
and
24 deletions
src/edu/nps/moves/dis7/utilities/DisThreadedNetIF.java
+
48
−
24
View file @
9cb98020
...
@@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream;
...
@@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream;
import
java.io.DataOutputStream
;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.*
;
import
java.net.*
;
import
java.nio.ByteBuffer
;
import
java.util.*
;
import
java.util.*
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.LinkedBlockingQueue
;
...
@@ -72,10 +73,10 @@ public class DisThreadedNetIF
...
@@ -72,10 +73,10 @@ public class DisThreadedNetIF
}
}
/* *********** queues and lists and public methods ************** */
/* *********** queues and lists and public methods ************** */
private
final
Array
List
<
PduListener
>
everyTypeListeners
=
new
ArrayList
<>();
private
final
List
<
PduListener
>
everyTypeListeners
=
new
ArrayList
<>();
private
final
Hash
Map
<
DISPDUType
,
Array
List
<
PduListener
>>
typeListeners
=
new
HashMap
<>();
private
final
Map
<
DISPDUType
,
List
<
PduListener
>>
typeListeners
=
new
HashMap
<>();
private
final
Array
List
<
RawPduListener
>
rawListeners
=
new
ArrayList
<>();
private
final
List
<
RawPduListener
>
rawListeners
=
new
ArrayList
<>();
private
final
LinkedBlockingQueue
<
Pdu
>
pdus2send
=
new
LinkedBlockingQueue
<>();
private
final
LinkedBlockingQueue
<
Pdu
>
pdus2send
=
new
LinkedBlockingQueue
<>();
...
@@ -89,7 +90,7 @@ public class DisThreadedNetIF
...
@@ -89,7 +90,7 @@ public class DisThreadedNetIF
if
(
typ
==
null
)
if
(
typ
==
null
)
addListener
(
lis
);
addListener
(
lis
);
else
{
else
{
Array
List
<
PduListener
>
arLis
=
typeListeners
.
get
(
typ
);
List
<
PduListener
>
arLis
=
typeListeners
.
get
(
typ
);
if
(
arLis
==
null
)
{
if
(
arLis
==
null
)
{
arLis
=
new
ArrayList
<>();
arLis
=
new
ArrayList
<>();
typeListeners
.
put
(
typ
,
arLis
);
typeListeners
.
put
(
typ
,
arLis
);
...
@@ -116,7 +117,7 @@ public class DisThreadedNetIF
...
@@ -116,7 +117,7 @@ public class DisThreadedNetIF
everyTypeListeners
.
remove
(
lis
);
everyTypeListeners
.
remove
(
lis
);
typeListeners
.
entrySet
().
stream
().
forEach
(
entry
->
{
typeListeners
.
entrySet
().
stream
().
forEach
(
entry
->
{
Array
List
<
PduListener
>
arLis
=
entry
.
getValue
();
List
<
PduListener
>
arLis
=
entry
.
getValue
();
if
(
arLis
.
contains
(
lis
))
if
(
arLis
.
contains
(
lis
))
arLis
.
remove
(
lis
);
arLis
.
remove
(
lis
);
});
});
...
@@ -182,22 +183,31 @@ public class DisThreadedNetIF
...
@@ -182,22 +183,31 @@ public class DisThreadedNetIF
int
counter
=
0
;
int
counter
=
0
;
private
Runnable
receiveThread
=
()
->
{
private
Runnable
receiveThread
=
()
->
{
DatagramPacket
packet
;
byte
buffer
[]
=
new
byte
[
MAX_DIS_PDU_SIZE
];
DatagramPacket
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
);
InetAddress
maddr
;
InetSocketAddress
group
;
Pdu
pdu
;
ByteBuffer
byteBuffer
;
while
(!
killed
)
{
// keep trying on error
while
(!
killed
)
{
// keep trying on error
try
{
try
{
socket
=
new
MulticastSocket
(
disPort
);
socket
=
new
MulticastSocket
(
disPort
);
InetAddress
maddr
=
InetAddress
.
getByName
(
mcastGroup
);
maddr
=
InetAddress
.
getByName
(
mcastGroup
);
socket
.
setNetworkInterface
(
findIp4Interface
()
);
group
=
new
InetSocketAddress
(
maddr
,
disPort
);
socket
.
joinGroup
(
maddr
);
socket
.
joinGroup
(
group
,
findIp4Interface
()
);
while
(!
killed
)
{
while
(!
killed
)
{
byte
buffer
[]
=
new
byte
[
MAX_DIS_PDU_SIZE
];
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
);
socket
.
receive
(
packet
);
//blocks here waiting for next DIS pdu to be received on multicast IP and specified port
socket
.
receive
(
packet
);
//blocks here waiting for next DIS pdu to be received on multicast IP and specified port
toRawListeners
(
packet
.
getData
(),
packet
.
getLength
());
toRawListeners
(
packet
.
getData
(),
packet
.
getLength
());
Pdu
pdu
=
pduFactory
.
createPdu
(
packet
.
getData
());
// Uses the NIO byte buffer class--wrap a ByteBuffer instance around
// the data we get from the packet
byteBuffer
=
ByteBuffer
.
wrap
(
packet
.
getData
());
pdu
=
pduFactory
.
createPdu
(
byteBuffer
);
if
(
pdu
!=
null
)
if
(
pdu
!=
null
)
{
{
counter
++;
// TODO experimental, add to generator as a commented-out diagnostic; consider adding diagnostic mode
counter
++;
// TODO experimental, add to generator as a commented-out diagnostic; consider adding diagnostic mode
...
@@ -221,21 +231,31 @@ public class DisThreadedNetIF
...
@@ -221,21 +231,31 @@ public class DisThreadedNetIF
};
};
private
final
Runnable
sendThread
=
()
->
{
private
final
Runnable
sendThread
=
()
->
{
InetAddress
maddr
;
Pdu
pdu
;
byte
[]
data
;
DatagramPacket
packet
;
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
while
(!
killed
)
{
while
(!
killed
)
{
try
{
try
{
InetAddress
maddr
=
InetAddress
.
getByName
(
mcastGroup
);
maddr
=
InetAddress
.
getByName
(
mcastGroup
);
while
(!
killed
)
{
while
(!
killed
)
{
Pdu
pdu
=
pdus2send
.
take
();
pdu
=
pdus2send
.
take
();
// turn object into byte stream
// turn object into byte stream
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
DataOutputStream
dos
=
new
DataOutputStream
(
baos
);
pdu
.
marshal
(
dos
);
pdu
.
marshal
(
dos
);
byte
[]
data
=
baos
.
toByteArray
();
data
=
baos
.
toByteArray
();
// load byte buffer into packet and send
DatagramPacket
packet
=
new
DatagramPacket
(
data
,
data
.
length
,
maddr
,
disPort
);
// Since we don't know the size of the PDU, reluctantly, we create
// new packet object each listen cycle
packet
=
new
DatagramPacket
(
data
,
data
.
length
,
maddr
,
disPort
);
socket
.
send
(
packet
);
socket
.
send
(
packet
);
baos
.
reset
();
}
}
}
}
...
@@ -259,7 +279,7 @@ public class DisThreadedNetIF
...
@@ -259,7 +279,7 @@ public class DisThreadedNetIF
{
{
everyTypeListeners
.
stream
().
forEach
(
lis
->
lis
.
incomingPdu
(
pdu
));
everyTypeListeners
.
stream
().
forEach
(
lis
->
lis
.
incomingPdu
(
pdu
));
if
(
pdu
!=
null
)
{
if
(
pdu
!=
null
)
{
Array
List
<
PduListener
>
arLis
=
typeListeners
.
get
(
pdu
.
getPduType
());
List
<
PduListener
>
arLis
=
typeListeners
.
get
(
pdu
.
getPduType
());
if
(
arLis
!=
null
)
if
(
arLis
!=
null
)
arLis
.
stream
().
forEach
(
lis
->
lis
.
incomingPdu
(
pdu
));
arLis
.
stream
().
forEach
(
lis
->
lis
.
incomingPdu
(
pdu
));
}
}
...
@@ -294,11 +314,15 @@ public class DisThreadedNetIF
...
@@ -294,11 +314,15 @@ public class DisThreadedNetIF
private
static
NetworkInterface
findIp4Interface
()
throws
SocketException
private
static
NetworkInterface
findIp4Interface
()
throws
SocketException
{
{
Enumeration
<
NetworkInterface
>
ifaces
=
NetworkInterface
.
getNetworkInterfaces
();
Enumeration
<
NetworkInterface
>
ifaces
=
NetworkInterface
.
getNetworkInterfaces
();
NetworkInterface
nif
;
Enumeration
<
InetAddress
>
addresses
;
InetAddress
addr
;
while
(
ifaces
.
hasMoreElements
())
{
while
(
ifaces
.
hasMoreElements
())
{
NetworkInterface
nif
=
ifaces
.
nextElement
();
nif
=
ifaces
.
nextElement
();
Enumeration
<
InetAddress
>
addresses
=
nif
.
getInetAddresses
();
addresses
=
nif
.
getInetAddresses
();
while
(
addresses
.
hasMoreElements
())
{
while
(
addresses
.
hasMoreElements
())
{
InetAddress
addr
=
addresses
.
nextElement
();
addr
=
addresses
.
nextElement
();
if
(
addr
instanceof
Inet4Address
&&
!
addr
.
isLoopbackAddress
())
{
if
(
addr
instanceof
Inet4Address
&&
!
addr
.
isLoopbackAddress
())
{
//System.out.println("Using network interface " + nif.getDisplayName());
//System.out.println("Using network interface " + nif.getDisplayName());
return
nif
;
return
nif
;
...
...
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