Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Simonstrator
PeerfactSim.KOM
Commits
421a101d
Commit
421a101d
authored
Dec 05, 2016
by
Marc Schiller
Browse files
Added IncrementableHashMap.
parent
b0b1e4bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/de/tud/kom/p2psim/impl/topology/views/fiveg/ModelBasedSegmentDatabase.java
View file @
421a101d
...
...
@@ -20,9 +20,12 @@
package
de.tud.kom.p2psim.impl.topology.views.fiveg
;
import
de.tud.kom.p2psim.api.linklayer.mac.MacLayer
;
import
de.tud.kom.p2psim.impl.linklayer.mac.AbstractMacLayer
;
import
de.tud.kom.p2psim.impl.linklayer.mac.SimpleMacLayer
;
import
java.util.HashMap
;
import
de.tud.kom.p2psim.api.linklayer.mac.MacAddress
;
import
de.tud.kom.p2psim.api.linklayer.mac.PhyType
;
import
de.tud.kom.p2psim.impl.linklayer.ModularLinkLayer
;
import
de.tud.kom.p2psim.impl.util.IncrementableHashMap
;
import
de.tudarmstadt.maki.simonstrator.api.Host
;
import
de.tudarmstadt.maki.simonstrator.api.Time
;
import
de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException
;
...
...
@@ -32,6 +35,10 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationLi
public
class
ModelBasedSegmentDatabase
extends
AbstractGridBasedTopologyDatabase
implements
LocationListener
{
private
HashMap
<
MacAddress
,
Integer
>
currentPositions
=
new
HashMap
<>();
private
IncrementableHashMap
<
Integer
>
segmentSize
=
new
IncrementableHashMap
<>();
public
ModelBasedSegmentDatabase
()
{
super
(
100
,
true
);
super
.
setSupportCloudlets
(
true
);
...
...
@@ -46,10 +53,28 @@ public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
@Override
public
void
onLocationChanged
(
Host
host
,
Location
location
)
{
try
{
System
.
out
.
println
(
host
.
getComponent
(
SimpleMacLayer
.
class
).
getMacAddress
());
// Get MAC Address and the SegementID the user is now in
MacAddress
mac
=
host
.
getComponent
(
ModularLinkLayer
.
class
)
.
getMac
(
PhyType
.
UMTS
).
getMacAddress
();
Integer
segmentID
=
new
Integer
(
this
.
getSegmentID
(
location
.
getLongitude
(),
location
.
getLatitude
()));
// Check if it is a new host
if
(
currentPositions
.
containsKey
(
mac
))
{
Integer
oldSegmentID
=
currentPositions
.
get
(
mac
);
// Host changed segment
if
(
oldSegmentID
.
compareTo
(
segmentID
)
!=
0
)
{
segmentSize
.
decrement
(
oldSegmentID
);
segmentSize
.
increment
(
segmentID
);
currentPositions
.
put
(
mac
,
segmentID
);
}
}
else
{
// New host
currentPositions
.
put
(
mac
,
segmentID
);
segmentSize
.
increment
(
segmentID
);
}
System
.
out
.
println
(
segmentSize
);
}
catch
(
ComponentNotAvailableException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
// TODO Handle unknown host type
}
}
...
...
src/de/tud/kom/p2psim/impl/util/IncrementableHashMap.java
0 → 100644
View file @
421a101d
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package
de.tud.kom.p2psim.impl.util
;
import
java.util.HashMap
;
public
class
IncrementableHashMap
<
K
>
{
private
HashMap
<
K
,
Integer
>
storage
;
public
IncrementableHashMap
()
{
storage
=
new
HashMap
<>();
}
public
int
get
(
K
key
)
{
return
storage
.
get
(
key
).
intValue
();
}
public
void
increment
(
K
key
)
{
if
(
storage
.
containsKey
(
key
))
{
Integer
tmp
=
storage
.
get
(
key
);
storage
.
put
(
key
,
new
Integer
(
tmp
.
intValue
()
+
1
));
}
else
{
storage
.
put
(
key
,
new
Integer
(
1
));
}
}
public
void
decrement
(
K
key
)
{
if
(
storage
.
containsKey
(
key
))
{
Integer
tmp
=
storage
.
get
(
key
);
if
(
tmp
.
compareTo
(
new
Integer
(
1
))
==
0
)
{
storage
.
put
(
key
,
new
Integer
(
0
));
}
else
{
storage
.
put
(
key
,
new
Integer
(
tmp
.
intValue
()
-
1
));
}
}
else
{
storage
.
put
(
key
,
new
Integer
(
0
));
}
}
public
boolean
containsKey
(
K
key
)
{
return
storage
.
containsKey
(
key
);
}
public
String
toString
()
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
K
key:
storage
.
keySet
())
{
sb
.
append
(
key
.
toString
()
+
": "
+
storage
.
get
(
key
).
toString
()
+
"\n"
);
}
return
sb
.
toString
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment