-
Notifications
You must be signed in to change notification settings - Fork 16
Clean connections when killing an agent/closing the experiment #659
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,8 @@ | |||||||||||
| import gama.core.messaging.GamaMessage; | ||||||||||||
| import gama.core.messaging.MessagingSkill; | ||||||||||||
| import gama.core.metamodel.agent.IAgent; | ||||||||||||
| import gama.core.metamodel.population.IPopulation; | ||||||||||||
| import gama.core.metamodel.population.IPopulation.Listener; | ||||||||||||
| import gama.core.runtime.GAMA; | ||||||||||||
| import gama.core.runtime.IScope; | ||||||||||||
| import gama.core.runtime.exceptions.GamaRuntimeException; | ||||||||||||
|
|
@@ -78,6 +80,41 @@ public class NetworkSkill extends MessagingSkill { | |||||||||||
|
|
||||||||||||
| /** The Constant REGISTRED_SERVER. */ | ||||||||||||
| final static String REGISTERED_SERVER = "registered_servers"; | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class NetworkListener implements Listener { | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void notifyPopulationCleared(IScope scope, IPopulation<? extends IAgent> pop) { | ||||||||||||
| // do nothing | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void notifyAgentsRemoved(IScope sc, IPopulation<? extends IAgent> pop, | ||||||||||||
| Collection<? extends IAgent> agents) { | ||||||||||||
| // for(IAgent ag : agents) { | ||||||||||||
| // disconnect(ag.getScope()); | ||||||||||||
| // } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void notifyAgentsAdded(IScope scope, IPopulation<? extends IAgent> pop, | ||||||||||||
| Collection<? extends IAgent> agents) { | ||||||||||||
| // do nothing | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void notifyAgentRemoved(IScope sc, IPopulation<? extends IAgent> pop, IAgent agent) { | ||||||||||||
| // do nothing | ||||||||||||
| disconnect(agent.getScope()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void notifyAgentAdded(IScope scope, IPopulation<? extends IAgent> pop, IAgent agent) { | ||||||||||||
| // do nothing | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * System exec. | ||||||||||||
|
|
@@ -203,6 +240,11 @@ public boolean connectToServer(final IScope scope) throws GamaRuntimeException { | |||||||||||
|
|
||||||||||||
| // Fix to Issue #2618 | ||||||||||||
| final String serverKey = createServerKey(serverURL, port); | ||||||||||||
|
|
||||||||||||
| if (scope.getAgent() != null) { | ||||||||||||
| scope.getAgent().getPopulation().addListener(new NetworkListener()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| final Map<String, IConnector> myConnectors = this.getRegisteredServers(scope); | ||||||||||||
| IConnector connector = myConnectors.get(serverKey); | ||||||||||||
|
|
@@ -324,21 +366,29 @@ public boolean connectToServer(final IScope scope) throws GamaRuntimeException { | |||||||||||
| doc = @doc ( | ||||||||||||
| value = "Disconnects from all the servers previously connected to. Will return true if everything went well, false in case of an error.")) | ||||||||||||
| public Boolean disconnect(final IScope scope) { | ||||||||||||
| final Map<String, IConnector> connectors = this.getRegisteredServers(scope); | ||||||||||||
| final Map<String, IConnector> connectors = getRegisteredServers(scope); | ||||||||||||
| final IAgent agent = scope.getAgent(); | ||||||||||||
| List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER); | ||||||||||||
|
||||||||||||
| List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER); | |
| List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER); | |
| if (serverList == null) { | |
| serverList = new ArrayList<>(); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -109,10 +109,10 @@ public void stopService() { | |||||||||||||||||
| this.isAlive = false; | ||||||||||||||||||
| if (sender != null) { sender.close(); } | ||||||||||||||||||
| try { | ||||||||||||||||||
| if (receiver != null) { receiver.close(); } | ||||||||||||||||||
| if (socket != null) { | ||||||||||||||||||
| socket.close(); | ||||||||||||||||||
| } | ||||||||||||||||||
| if (receiver != null) { receiver.close(); } | ||||||||||||||||||
|
Comment on lines
112
to
+115
|
||||||||||||||||||
| if (socket != null) { | |
| socket.close(); | |
| } | |
| if (receiver != null) { receiver.close(); } | |
| if (receiver != null) { receiver.close(); } | |
| if (socket != null) { | |
| socket.close(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registering a new listener on every connect causes multiple redundant listeners; ensure only one listener per population by tracking registration or moving it to initialization.