Skip to content

Commit

Permalink
[lang][core] Add observer on the SRE start and stop.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Apr 5, 2020
1 parent 2e33893 commit bfabc21
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 2 deletions.
10 changes: 10 additions & 0 deletions main/coreplugins/io.sarl.lang.core/src/io/sarl/bootstrap/SRE.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ public void shutdown(boolean blocking, int timeout) throws InterruptedException
throw new UnsupportedOperationException();
}

@Override
public void addSREListener(SREListener listener) {
//
}

@Override
public void removeSREListener(SREListener listener) {
//
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,20 @@ default void shutdown(int timeout) throws InterruptedException {
@Pure
<T> T getService(Class<T> serviceType);

/**
* Add an observer on the SRE.
*
* @param listener the observer to add.
* @since 0.11
*/
void addSREListener(SREListener listener);

/**
* Remove the given observer on the SRE.
*
* @param listener the observer to remove.
* @since 0.11
*/
void removeSREListener(SREListener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2020 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.bootstrap;

import java.util.EventListener;

/**
* Observer on SRE events.
*
* <p>This observer will be notified depending on the the implementation of the SRE.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.11
*/
public interface SREListener extends EventListener {

/** Invoked when the SRE is started.
*
* @param sre the started SRE.
*/
void sreStarted(SREBootstrap sre);

/** Invoked when the SRE is stopped.
*
* @param sre the stopped SRE.
*/
void sreStopped(SREBootstrap sre);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import io.bootique.help.HelpOptions
import io.bootique.meta.application.ApplicationMetadata
import io.sarl.bootstrap.SRE
import io.sarl.bootstrap.SREBootstrap
import io.sarl.bootstrap.SREListener
import io.sarl.lang.core.Agent
import io.sarl.lang.core.AgentContext
import io.sarl.sre.Kernel
Expand All @@ -48,6 +49,7 @@ import org.apache.log4j.Logger
import org.arakhne.afc.bootique.log4j.configs.Level
import org.arakhne.afc.bootique.log4j.configs.Log4jIntegrationConfig
import org.arakhne.afc.bootique.variables.VariableNames
import org.arakhne.afc.util.ListenerCollection
import org.eclipse.xtend.lib.annotations.Accessors

import static io.sarl.bootstrap.SRE.*
Expand All @@ -74,6 +76,8 @@ class SreMain implements SREBootstrap {
var userDefinedContextId : UUID

var userDefinedSpaceId : UUID

val listeners = new ListenerCollection<SREListener>

/** Set the Bootique runtime instance.
*
Expand Down Expand Up @@ -243,9 +247,11 @@ class SreMain implements SREBootstrap {
val listener : KernelAgentLifecycleListener = [
this.kernel = null
this.runtime = null
fireSREStopped
]
k.getService(typeof(LifecycleService)).addKernelAgentLifecycleListener(listener)
this.kernel = k
fireSREStarted
}
// Force the bootstrap to be this object
SRE::bootstrap = this
Expand Down Expand Up @@ -347,4 +353,46 @@ class SreMain implements SREBootstrap {
return null
}

override addSREListener(listener : SREListener) {
if (listener !== null) {
this.listeners.add(typeof(SREListener), listener)
}
}

override removeSREListener(listener : SREListener) {
if (listener !== null) {
this.listeners.remove(typeof(SREListener), listener)
}
}

/** Notifies the observers that the SRE has been started.
*
* @since 0.11
*/
protected def fireSREStarted {
val observers = this.listeners.getListeners(typeof(SREListener))
for (observer : observers) {
try {
observer.sreStarted(this)
} catch (exception : Throwable) {
getKernelLogger.log(java.util.logging.Level::SEVERE, exception.localizedMessage, exception)
}
}
}

/** Notifies the observers that the SRE has been stopped.
*
* @since 0.11
*/
protected def fireSREStopped {
val observers = this.listeners.getListeners(typeof(SREListener))
for (observer : observers) {
try {
observer.sreStopped(this)
} catch (exception : Throwable) {
getKernelLogger.log(java.util.logging.Level::SEVERE, exception.localizedMessage, exception)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import io.sarl.lang.core.SpaceID
import io.sarl.sre.capacities.InternalEventBusCapacity
import io.sarl.sre.services.context.Context
import io.sarl.sre.services.logging.LoggingService
import java.lang.ref.WeakReference
import java.util.List
import java.util.Map
import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package io.sarl.sre.tests.runtime.boot.bootstrap

import io.sarl.bootstrap.SREBootstrap
import io.sarl.bootstrap.SREListener
import io.sarl.sre.boot.configs.subconfigs.BootConfig
import io.sarl.sre.boot.configs.subconfigs.RootContextType
import io.sarl.sre.test.framework.context.SreRunContext
Expand All @@ -39,9 +41,12 @@ import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith

import static extension org.junit.jupiter.api.Assertions.*
import static io.sarl.sre.test.framework.Constants.*

import static extension org.junit.jupiter.api.Assertions.*
import static extension org.mockito.ArgumentCaptor.*
import static extension org.mockito.Mockito.*

/**
* @author $Author: sgalland$
* @version $FullVersion$
Expand Down Expand Up @@ -118,4 +123,25 @@ class BootstrapTest {
id.assertEquals(actual)
}

@Test
@DisplayName("sreStopped")
def sreStartedStopped(extension rc : SreRunContext) {
val observer = typeof(SREListener).mock

val kern = setupTheSreKernel(null, null)
kern.addSREListener(observer)

val id = UUID::randomUUID
kern.startAgentWithID(typeof(TestIdAgent0), id, agentInitializationParameters)
waitForTheKernel(SHORT_TIMEOUT)

var arg = typeof(SREBootstrap).forClass

// Never called because the event is fired before setupTheSreKernel returns
observer.verify(never).sreStarted(any)

observer.verify(1.times).sreStopped(arg.capture)
kern.assertSame(arg.value)
}

}
Loading

0 comments on commit bfabc21

Please sign in to comment.