Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malicious behavior #7

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
/bin/
/processeddata/
/rawdata/
/rome/
sanfrancisco/
/beijing/
/ARCHIVES/
.idea
78 changes: 78 additions & 0 deletions Sim2Car.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<libelement value="jar://$MODULE_DIR$/lib/jcommon-1.0.18.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/jfreechart-1.0.15.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/jcommander-master.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/dropbox-core-sdk-3.0.5.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/jackson-core-2.7.4.jar!/" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src" expected_position="0" />
<src_folder value="file://$MODULE_DIR$/lib/jmapviewer-1.02/src" expected_position="1" />
</src_description>
</component>
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/lib/jmapviewer-1.02/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="module-library">
<library name="jcommon-1.0.18.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jcommon-1.0.18.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="jfreechart-1.0.15.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jfreechart-1.0.15.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="jcommander-master.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jcommander-master.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="dropbox-core-sdk-3.0.5.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/dropbox-core-sdk-3.0.5.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="jackson-core-2.7.4.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jackson-core-2.7.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="junit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
4 changes: 3 additions & 1 deletion src/application/ApplicationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public enum ApplicationType {
ROUTING_APP, // the smart Navigator application
STREET_VISITS_APP, // application that records the streets visit count
TRAFFIC_LIGHT_CONTROL_APP,
SYNCHRONIZE_INTERSECTIONS_APP // app that syncronize traffic lights in multiple intersections
SYNCHRONIZE_INTERSECTIONS_APP, // app that syncronize traffic lights in multiple intersections
TRAFFIC_LIGHT_ROUTING_APP,
CAR_ROUTING_APP
}
10 changes: 9 additions & 1 deletion src/application/ApplicationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Vector;

import application.dynamic_routing.CarDynamicRoutingApp;
import application.dynamic_routing.TrafficLightDynamicRoutingApp;
import controller.engine.EngineSimulation;
import controller.newengine.SimulationEngine;
import application.multipleIntersections.SynchronizeIntersectionsApplication;
Expand Down Expand Up @@ -38,6 +40,9 @@ public static Application activateApplicationCar( ApplicationType type, GeoCar c
return EngineSimulation.getInstance() != null ? new TileApplicationCar(car) : null;
case STREET_VISITS_APP:
return SimulationEngine.getInstance() != null ? new StreetVisitsApplication(car) : null;

case CAR_ROUTING_APP:
return SimulationEngine.getInstance() != null ? new CarDynamicRoutingApp(true, car, ApplicationType.CAR_ROUTING_APP) : null;
default:
return null;
}
Expand All @@ -54,6 +59,9 @@ public static Application activateApplicationTrafficLight( ApplicationType type,
switch (type) {
case TRAFFIC_LIGHT_CONTROL_APP:
return SimulationEngine.getInstance() != null ? new ApplicationTrafficLightControl(trafficLight) : null;

case TRAFFIC_LIGHT_ROUTING_APP:
return SimulationEngine.getInstance() != null ? new TrafficLightDynamicRoutingApp(true, trafficLight) : null;
default:
return null;
}
Expand Down Expand Up @@ -94,7 +102,7 @@ public static Application activateApplicationServer( ApplicationType type, GeoSe
}

/**
* @param netInterfaces - String with active Applications
* @param //(netInterfaces) - String with active Applications
*/
public static void parseApplications(String activeApplications) {
String activeApps[] = activeApplications.split(",");
Expand Down
182 changes: 182 additions & 0 deletions src/application/dynamic_routing/CarDynamicRoutingApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package application.dynamic_routing;

import application.Application;
import application.ApplicationType;
import controller.network.NetworkInterface;
import controller.network.NetworkType;
import model.GeoCar;
import model.mobility.MobilityEngine;
import model.network.Message;
import model.network.MessageType;
import model.parameters.Globals;
import application.streetCostSharing.StreetsCostSharing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class CarDynamicRoutingApp extends Application {

private boolean isActive;
private GeoCar car;
private ApplicationType type;
private MobilityEngine mobilityEngine;

public CarDynamicRoutingApp(boolean isActive, GeoCar car, ApplicationType type) {
this.isActive = isActive;
this.car = car;
this.type = type;
this.mobilityEngine = MobilityEngine.getInstance();
}

@Override
public boolean getStatus() {
return false;
}

@Override
public String run() {
NetworkInterface net = car.getNetworkInterface(NetworkType.Net_WiFi);
net.processOutputQueue();
return "";
}

@Override
public String stop() {
return null;
}

@Override
public String getInfoApp() {
return null;
}

@Override
public Object getData() {
return null;
}

@Override
public ApplicationType getType() {
return type;
}


public void sendOutdatedDataToTrafficLight(HashMap<Long, VotingStreetCostData> outdated, long destinationTrafficLightId) {
NetworkInterface networkInterface = this.car.getNetworkInterface(NetworkType.Net_WiFi);
Message message = new Message(this.car.getId(), destinationTrafficLightId, outdated.clone(),
MessageType.OUTDATED_COSTS, ApplicationType.TRAFFIC_LIGHT_ROUTING_APP);
networkInterface.putMessage(message);
}


public void sendOutdatedDataToCar(HashMap<Long, VotingStreetCostData> outdated, long neighbourCarId) {
NetworkInterface networkInterface = this.car.getNetworkInterface(NetworkType.Net_WiFi);
Message message = new Message(this.car.getId(), neighbourCarId, outdated.clone(),
MessageType.OUTDATED_COSTS, ApplicationType.CAR_ROUTING_APP);
networkInterface.putMessage(message);
}


public boolean isOutdated(double oldCost, double updateCost, long currentWayId, VotingStreetCostData votingStreetCostData) {
boolean returnValue = true;

/* if the cost itself is new for the car's KB*/
if (Math.abs(oldCost - updateCost) >
(oldCost * StreetsCostSharing.STREET_COST_UPDATE_THRESHOLD)) {
returnValue = false;
}

/*if the cost does not have any new stamp cases:
* has new stamps -> not outdated
* does not have -> is outdated
* it is a new cost to share -> not outdated*/

if (returnValue && Globals.useVotingSystem) {
if (this.car.getStreetsCostSharing().getStreetUpdates().containsKey(currentWayId)) {
boolean hasNewStamps = this.car.getStreetsCostSharing().getStreetUpdates()
.get(currentWayId).hasNewStamps(votingStreetCostData);
returnValue = !hasNewStamps;
} else {
returnValue = false;
}
}

return returnValue;
}


/* this function has the role to iterate through the reported costs and to
* split them into valid data or outdated data (outdated data is useful only
* in CARS communication context)*/
private ArrayList<HashMap<Long, VotingStreetCostData>> updatesPreProcessing(Message m) {
HashMap<Long, VotingStreetCostData> updates;
HashMap<Long, VotingStreetCostData> validUpdates = new HashMap<Long, VotingStreetCostData>();
HashMap<Long, VotingStreetCostData> outdatedUpdates = new HashMap<Long, VotingStreetCostData>();
Iterator<Long> itr;

updates = (HashMap<Long, VotingStreetCostData>) m.getData();
itr = updates.keySet().iterator();
long currentWayId;
double oldCost, updateCost;

while (itr.hasNext()) {
currentWayId = itr.next();
oldCost = car.getStreetsCostSharing().getWayCost(mobilityEngine.getWay(currentWayId));
updateCost = updates.get(currentWayId).streetCost;

/*check outdated*/
if (!isOutdated(oldCost, updateCost, currentWayId, updates.get(currentWayId))) {

car.getStreetsCostSharing().updateWayCost(currentWayId, updates.get(currentWayId));
validUpdates.put(currentWayId, updates.get(currentWayId));

} else {
outdatedUpdates.put(currentWayId, updates.get(currentWayId));
}
}
ArrayList<HashMap<Long, VotingStreetCostData>> aux = new ArrayList<HashMap<Long, VotingStreetCostData>>();
aux.add(validUpdates);
aux.add(outdatedUpdates);
return aux;
}


@Override
public void process(Message m) {
ArrayList<HashMap<Long, VotingStreetCostData>> aux;
HashMap<Long, VotingStreetCostData> outdated;

switch (m.getType()) {
case TRAFFIC_LIGHT_INFORMS:

aux = updatesPreProcessing(m);
outdated = aux.get(1);

/*if there is some outdated data, inform the traffic light*/
if (!outdated.isEmpty()) {
sendOutdatedDataToTrafficLight(outdated, m.getSourceId());
}

break;

case CAR_INFORMS:
/*some other car informs current car*/

aux = updatesPreProcessing(m);
outdated = aux.get(1);

if (!outdated.isEmpty()) {
sendOutdatedDataToCar(outdated, m.getSourceId());
}
break;

case OUTDATED_COSTS:

HashMap<Long, VotingStreetCostData> outdatedUpdates = (HashMap<Long, VotingStreetCostData>) m.getData();
this.car.getStreetsCostSharing().removeOutdatedInfo(outdatedUpdates);

break;
}
}
}
Loading