Class ComponentDefinition
public abstract class ComponentDefinition extends Object
ComponentDefinition
class.- Version:
- $Id$
- Author:
- Cosmin Arad <[email protected]>, Jim Dowling <[email protected]>, Lars Kroll <[email protected]>
-
Field Summary
Fields Modifier and Type Field Description protected Negative<ControlPort>
control
The components own control port.protected org.slf4j.Logger
logger
Kompics provided slf4j logger with managed diagnostic context.protected Negative<LoopbackPort>
loopback
The receiving side of the loopback port.static String
MDC_KEY_CID
Pre-configured MDC key for the unique component id.static String
MDC_KEY_CSTATE
Pre-configured MDC key for the current component lifecycle state.protected Positive<LoopbackPort>
onSelf
The sending side of the loopback port.ComponentProxy
proxy
The component's proxy instance for reflective configuration. -
Constructor Summary
Constructors Modifier Constructor Description protected
ComponentDefinition()
Instantiate a new component definition.protected
ComponentDefinition(Class<? extends ComponentCore> coreClass)
Instantiate a new component definition. -
Method Summary
Modifier and Type Method Description protected void
answer(Direct.Request<?> event)
Reply to a request directly, using the stored origin and the preset response.protected void
answer(Direct.Request<?> req, Direct.Response resp)
Reply to a request directly, using the stored origin and the given response.Config
config()
Get the component's config instance.protected <P extends PortType>
Channel<P>connect(Negative<P> negative, Positive<P> positive)
Deprecated.Useconnect(Positive, Negative, ChannelFactory)
with `Channel.TWO_WAY` insteadprotected <P extends PortType>
Channel<P>connect(Negative<P> negative, Positive<P> positive, ChannelFactory factory)
Connect two ports via a channel.protected <P extends PortType>
Channel<P>connect(Negative<P> negative, Positive<P> positive, ChannelSelector<?,?> selector)
Deprecated.Useconnect(Positive, Negative, ChannelSelector, ChannelFactory)
with `Channel.TWO_WAY` insteadprotected <P extends PortType>
Channel<P>connect(Negative<P> negative, Positive<P> positive, ChannelSelector<?,?> selector, ChannelFactory factory)
Connect two ports via a channel.protected <P extends PortType>
Channel<P>connect(Positive<P> positive, Negative<P> negative)
Deprecated.Useconnect(Positive, Negative, ChannelFactory)
with `Channel.TWO_WAY` insteadprotected <P extends PortType>
Channel<P>connect(Positive<P> positive, Negative<P> negative, ChannelFactory factory)
Connect two ports via a channel.protected <P extends PortType>
Channel<P>connect(Positive<P> positive, Negative<P> negative, ChannelSelector<?,?> selector)
Deprecated.Useconnect(Positive, Negative, ChannelSelector, ChannelFactory)
with `Channel.TWO_WAY` insteadprotected <P extends PortType>
Channel<P>connect(Positive<P> positive, Negative<P> negative, ChannelSelector<?,?> selector, ChannelFactory factory)
Connect two ports via a channel.protected <T extends ComponentDefinition>
Componentcreate(Class<T> definition, Init.None initEvent)
Create a child component with no init event.protected <T extends ComponentDefinition>
Componentcreate(Class<T> definition, Init.None initEvent, ConfigUpdate update)
Create a child component with no init event and a configuration update.protected <T extends ComponentDefinition>
Componentcreate(Class<T> definition, Init<T> initEvent)
Create a child component with a given init event.protected <T extends ComponentDefinition>
Componentcreate(Class<T> definition, Init<T> initEvent, ConfigUpdate update)
Create a child component with a given init event and a configuration update.protected void
destroy(Component component)
Force destruction of a child component.protected <P extends PortType>
voiddisconnect(Channel<P> channel)
Disconnect a channel connecting two ports.protected <P extends PortType>
voiddisconnect(Negative<P> negative, Positive<P> positive)
Deprecated.Usedisconnect(Channel)
orChannel.disconnect()
insteadprotected <P extends PortType>
voiddisconnect(Positive<P> positive, Negative<P> negative)
Deprecated.Usedisconnect(Channel)
orChannel.disconnect()
insteadComponentCore
getComponentCore()
Get the component's core instance.Negative<ControlPort>
getControlPort()
Get the component's control port.protected <E extends KompicsEvent, P extends PortType>
Handler<E>handle(Port<P> port, Class<E> type, Consumer<E> fun)
Create a functionl handler and automatically subscribe it to a port.Fault.ResolveAction
handleFault(Fault fault)
Define how to handle a fault.UpdateAction
handleUpdate(ConfigUpdate update)
Deal with a configuration update.UUID
id()
Get the component's unique id.protected String
loggingContextGet(String key)
Get the value associated with key in the current logging diagnostic context.protected void
loggingContextPut(String key, String value)
Associate key with value in the logging diagnostic context.protected void
loggingContextPutAlways(String key, String value)
Associate key permanently with value in the logging diagnostic context.protected void
loggingContextRemove(String key)
Disassociate any value with the key in the logging diagnostic context.protected void
loggingContextReset()
Reset the current logging diagnostic context.protected String
loggingCtxGet(String key)
Deprecated.Since 1.2.0, useloggingContextGet(String)
instead.protected void
loggingCtxPut(String key, String value)
Deprecated.Since 1.2.0, useloggingContextPut(String, String)
instead.protected void
loggingCtxPutAlways(String key, String value)
Deprecated.Since 1.2.0, useloggingContextPutAlways(String, String)
instead.protected void
loggingCtxRemove(String key)
Deprecated.Since 1.2.0, useloggingContextRemove(String)
instead.protected void
loggingCtxReset()
Deprecated.Since 1.2.0, useloggingContextReset()
instead.protected <P extends PortType>
Negative<P>negative(Class<P> portType)
Create a negative (provided) port instance and return it.protected Handler<Kill>
onKill(Consumer<Kill> fun)
Subscribe a handler to theKill
event.protected Handler<Start>
onStart(Consumer<Start> fun)
Subscribe a handler to theStart
event.protected Handler<Stop>
onStop(Consumer<Stop> fun)
Subscribe a handler to theStop
event.protected <P extends PortType>
Positive<P>positive(Class<P> portType)
Create a positive (required) port instance and return it.void
postUpdate()
Override to perform actions after a ConfigUpdate was applied and forwarded.protected <P extends PortType>
Negative<P>provides(Class<P> portType)
Create a provided (negative) port instance and return it.protected <P extends PortType>
Positive<P>requires(Class<P> portType)
Create a required (positive) port instance and return it.boolean
separateConfigId()
Override to allow components of this type to start their own independentConfig
id lines.protected void
setMDC()
Should not be necessary to call usually, as ComponentCore will do it.protected <E extends KompicsEvent, P extends PortType>
voidsubscribe(Handler<E> handler, Port<P> port)
Subscribe a handler to a port.protected void
subscribe(MatchedHandler<?,?,?> handler, Port<?> port)
Subscribe a matched handler to a port.void
suicide()
Send a kill event to the component itself.void
tearDown()
Cleanup the component before shutdown.protected <P extends PortType>
voidtrigger(KompicsEvent event, Port<P> port)
Trigger an event on a port instance.protected <E extends KompicsEvent, P extends PortType>
voidunsubscribe(Handler<E> handler, Port<P> port)
Unsubscribe a handler from a portprotected void
unsubscribe(MatchedHandler<?,?,?> handler, Port<?> port)
Unsubscribe a matched handler from a portvoid
updateConfig(ConfigUpdate update)
Perform a configuration update.
-
Field Details
-
control
The components own control port. -
loopback
The receiving side of the loopback port. The sending side is atonSelf
. -
onSelf
The sending side of the loopback port. The receiving side is atloopback
. -
proxy
The component's proxy instance for reflective configuration. -
MDC_KEY_CID
Pre-configured MDC key for the unique component id.See the logback manuel for how to use this with logback.
- See Also:
- Constant Field Values
-
MDC_KEY_CSTATE
Pre-configured MDC key for the current component lifecycle state.See the logback manuel for how to use this with logback.
- See Also:
- Constant Field Values
-
logger
Kompics provided slf4j logger with managed diagnostic context.See the logback manuel for how to use this with logback.
-
-
Constructor Details
-
ComponentDefinition
protected ComponentDefinition()Instantiate a new component definition. -
ComponentDefinition
Instantiate a new component definition.- Parameters:
coreClass
- to use as the runtime core for the definition
-
-
Method Details
-
negative
Create a negative (provided) port instance and return it.- Type Parameters:
P
- the type of the port type- Parameters:
portType
- the class instance of the port type- Returns:
- the new instance
-
provides
Create a provided (negative) port instance and return it. Same asnegative(Class)
.- Type Parameters:
P
- the type of the port type- Parameters:
portType
- the class instance of the port type- Returns:
- the new instance
-
positive
Create a positive (required) port instance and return it.- Type Parameters:
P
- the type of the port type- Parameters:
portType
- the class instance of the port type- Returns:
- the new instance
-
requires
Create a required (positive) port instance and return it.- Type Parameters:
P
- the type of the port type- Parameters:
portType
- the class instance of the port type- Returns:
- the new instance
-
trigger
Trigger an event on a port instance.- Type Parameters:
P
- type of the port type- Parameters:
event
- the event to be triggeredport
- the port where the event is triggered on
-
answer
Reply to a request directly, using the stored origin and the preset response. The passed requests must have a preset response!- Parameters:
event
- the request event to reply to
-
answer
Reply to a request directly, using the stored origin and the given response. The passed requests must have a preset response!- Parameters:
req
- the request event to reply toresp
- the response to send
-
handle
protected final <E extends KompicsEvent, P extends PortType> Handler<E> handle(Port<P> port, Class<E> type, Consumer<E> fun)Create a functionl handler and automatically subscribe it to a port.- Type Parameters:
E
- type of the handled eventP
- type of the port being subscribed to- Parameters:
port
- a port instance to subcribe the handler totype
- a class instance of the handled event typefun
- the handler function- Returns:
- the newly created handler
-
onStart
Subscribe a handler to theStart
event.- Parameters:
fun
- the handler function- Returns:
- the newly created handler
-
onStop
Subscribe a handler to theStop
event.- Parameters:
fun
- the handler function- Returns:
- the newly created handler
-
onKill
Subscribe a handler to theKill
event.- Parameters:
fun
- the handler function- Returns:
- the newly created handler
-
subscribe
protected final <E extends KompicsEvent, P extends PortType> void subscribe(Handler<E> handler, Port<P> port)Subscribe a handler to a port.- Type Parameters:
E
- the type of event to subscribe toP
- the type of port to subscribe to- Parameters:
handler
- the handler to subcribeport
- the port to subcribe to- Throws:
ConfigurationException
- when trying to subscribe on instances that aren't of typeJavaPort
-
subscribe
Subscribe a matched handler to a port.- Parameters:
handler
- the handler to subcribeport
- the port to subcribe to- Throws:
ConfigurationException
- when trying to subscribe on foreign ports or instances that aren't of typeJavaPort
-
unsubscribe
Unsubscribe a matched handler from a port- Parameters:
handler
- the handler to unsubcribeport
- the port to unsubcribe from- Throws:
ConfigurationException
- when trying to subscribe on instances that aren't of typeJavaPort
-
unsubscribe
protected final <E extends KompicsEvent, P extends PortType> void unsubscribe(Handler<E> handler, Port<P> port) throws ConfigurationExceptionUnsubscribe a handler from a port- Type Parameters:
E
- the event type of the handle to subscribeP
- the port type to subscribe to- Parameters:
handler
- the handler to unsubcribeport
- the port to unsubcribe from- Throws:
ConfigurationException
- when trying to subscribe on instances that aren't of typeJavaPort
-
create
protected final <T extends ComponentDefinition> Component create(Class<T> definition, Init<T> initEvent)Create a child component with a given init event.- Type Parameters:
T
- the type of the child component- Parameters:
definition
- the component definition of the child componentinitEvent
- init event to be passed to constructor- Returns:
- the newly created component
-
create
protected final <T extends ComponentDefinition> Component create(Class<T> definition, Init.None initEvent)Create a child component with no init event. Use `Init.NONE` for the instance.- Type Parameters:
T
- the type of the child component- Parameters:
definition
- the component definition of the child componentinitEvent
- specialInit.None
event to be passed to constructor- Returns:
- the newly created component
-
create
protected final <T extends ComponentDefinition> Component create(Class<T> definition, Init<T> initEvent, ConfigUpdate update)Create a child component with a given init event and a configuration update.- Type Parameters:
T
- the type of the child component- Parameters:
definition
- the component definition of the child componentinitEvent
- init event to be passed to constructorupdate
- a configuration update to pass to the child- Returns:
- the newly created component
-
create
protected final <T extends ComponentDefinition> Component create(Class<T> definition, Init.None initEvent, ConfigUpdate update)Create a child component with no init event and a configuration update. Use `Init.NONE` for the instance.- Type Parameters:
T
- the type of the child component- Parameters:
definition
- the component definition of the child componentinitEvent
- specialInit.None
event to be passed to constructorupdate
- a configuration update to pass to the child- Returns:
- the newly created component
-
destroy
Force destruction of a child component. Only ever call this on a component in the PASSIVE state!- Parameters:
component
- the child component to destroy
-
connect
@Deprecated protected final <P extends PortType> Channel<P> connect(Positive<P> positive, Negative<P> negative)Deprecated.Useconnect(Positive, Negative, ChannelFactory)
with `Channel.TWO_WAY` insteadConnect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instance- Returns:
- the newly created channel intance
-
connect
@Deprecated protected final <P extends PortType> Channel<P> connect(Negative<P> negative, Positive<P> positive)Deprecated.Useconnect(Positive, Negative, ChannelFactory)
with `Channel.TWO_WAY` insteadConnect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instance- Returns:
- the newly created channel intance
-
connect
@Deprecated protected <P extends PortType> Channel<P> connect(Positive<P> positive, Negative<P> negative, ChannelSelector<?,?> selector)Deprecated.Useconnect(Positive, Negative, ChannelSelector, ChannelFactory)
with `Channel.TWO_WAY` insteadConnect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instanceselector
- the channel selector to use- Returns:
- the newly created channel intance
-
connect
@Deprecated protected <P extends PortType> Channel<P> connect(Negative<P> negative, Positive<P> positive, ChannelSelector<?,?> selector)Deprecated.Useconnect(Positive, Negative, ChannelSelector, ChannelFactory)
with `Channel.TWO_WAY` insteadConnect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instanceselector
- the channel selector to use- Returns:
- the newly created channel intance
-
connect
protected <P extends PortType> Channel<P> connect(Negative<P> negative, Positive<P> positive, ChannelSelector<?,?> selector, ChannelFactory factory)Connect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instanceselector
- the channel selector to usefactory
- a one-way or two-way factory (seeChannel
)- Returns:
- the newly created channel intance
-
connect
protected <P extends PortType> Channel<P> connect(Positive<P> positive, Negative<P> negative, ChannelSelector<?,?> selector, ChannelFactory factory)Connect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instanceselector
- the channel selector to usefactory
- a one-way or two-way factory (seeChannel
)- Returns:
- the newly created channel intance
-
connect
protected <P extends PortType> Channel<P> connect(Negative<P> negative, Positive<P> positive, ChannelFactory factory)Connect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instancefactory
- a one-way or two-way factory (seeChannel
)- Returns:
- the newly created channel intance
-
connect
protected <P extends PortType> Channel<P> connect(Positive<P> positive, Negative<P> negative, ChannelFactory factory)Connect two ports via a channel.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instancefactory
- a one-way or two-way factory (seeChannel
)- Returns:
- the newly created channel intance
-
disconnect
@Deprecated protected final <P extends PortType> void disconnect(Negative<P> negative, Positive<P> positive)Deprecated.Usedisconnect(Channel)
orChannel.disconnect()
insteadDisconnect two ports that are connected via a channel. This is very inefficient, as it needs to find a matching channel first. Usedisconnect(Channel)
orChannel.disconnect()
instead.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instance
-
disconnect
@Deprecated protected final <P extends PortType> void disconnect(Positive<P> positive, Negative<P> negative)Deprecated.Usedisconnect(Channel)
orChannel.disconnect()
insteadDisconnect two ports that are connected via a channel. This is very inefficient, as it needs to find a matching channel first. Usedisconnect(Channel)
orChannel.disconnect()
instead.- Type Parameters:
P
- the shared port type- Parameters:
negative
- the negative port instancepositive
- the positive port instance
-
disconnect
Disconnect a channel connecting two ports.- Type Parameters:
P
- the shared port type- Parameters:
channel
- the channel to drop
-
getControlPort
Get the component's control port.- Returns:
- the control port
-
getComponentCore
Get the component's core instance.- Returns:
- the component core
-
config
Get the component's config instance.- Returns:
- the config instance
-
id
Get the component's unique id.- Returns:
- the component id
-
suicide
Send a kill event to the component itself. Only valid if the component's state is active! -
tearDown
Cleanup the component before shutdown. Use for custom cleanup. Will be called after all child components have stopped, but before sending a Stopped message to the parent. Does nothings by default. -
handleFault
Define how to handle a fault.Override this method for custom error handling.
Default action is ESCALATE.
Possible actions are:
- ESCALATE: Forward fault to parent.
- IGNORE: Drop fault. Resume component as if nothing happened.
- RESOLVED: Fault has been handled by user. Don't do anything else.
- Parameters:
fault
- the fault that must be handled- Returns:
- the action to take
-
handleUpdate
Deal with a configuration update.Override for custom update handling.
Default action is to propagate the original everywhere and apply to self
- Parameters:
update
- the update to be handled- Returns:
- the action to be taken
-
postUpdate
Override to perform actions after a ConfigUpdate was applied and forwarded. -
updateConfig
Perform a configuration update.- Parameters:
update
- the update to perform
-
separateConfigId
Override to allow components of this type to start their own independentConfig
id lines.This is helpful in simulation, when simulating multiple independent nodes. Make sure that no
ConfigUpdate
s are passed to siblings or parents of such nodes! (OverridehandleUpdate(se.sics.kompics.config.ConfigUpdate)
)- Returns:
- Whether to create a new config id line for this component (default:
true
)
-
setMDC
Should not be necessary to call usually, as ComponentCore will do it.Protected mainly for use by Kompics Scala.
Can also be used to set component MDC when executing related off-kompics work (check for concurrency issues, though!).
-
loggingCtxPut
Deprecated.Since 1.2.0, useloggingContextPut(String, String)
instead.Associate key with value in the logging diagnostic context.See the logback manuel for how to use this with logback.
- Parameters:
key
- the key to usevalue
- the value to associate with the key
-
loggingContextPut
Associate key with value in the logging diagnostic context.See the logback manuel for how to use this with logback.
- Parameters:
key
- the key to usevalue
- the value to associate with the key
-
loggingCtxPutAlways
Deprecated.Since 1.2.0, useloggingContextPutAlways(String, String)
instead.Associate key permanently with value in the logging diagnostic context.Keys set in this way are not removed by
loggingContextReset()
orloggingContextRemove(String)
.See the logback manuel for how to use this with logback.
- Parameters:
key
- the key to usevalue
- the value to associate with the key
-
loggingContextPutAlways
Associate key permanently with value in the logging diagnostic context.Keys set in this way are not removed by
loggingContextReset()
orloggingContextRemove(String)
.See the logback manuel for how to use this with logback.
- Parameters:
key
- the key to usevalue
- the value to associate with the key
-
loggingCtxRemove
Deprecated.Since 1.2.0, useloggingContextRemove(String)
instead.Disassociate any value with the key in the logging diagnostic context.- Parameters:
key
- the key to remove
-
loggingContextRemove
Disassociate any value with the key in the logging diagnostic context.- Parameters:
key
- the key to remove
-
loggingCtxGet
Deprecated.Since 1.2.0, useloggingContextGet(String)
instead.Get the value associated with key in the current logging diagnostic context.- Parameters:
key
- the key to fetch the value for- Returns:
- the value associated with key
-
loggingContextGet
Get the value associated with key in the current logging diagnostic context.- Parameters:
key
- the key to fetch the value for- Returns:
- the value associated with key
-
loggingCtxReset
Deprecated.Since 1.2.0, useloggingContextReset()
instead.Reset the current logging diagnostic context.Removes all items added to context by the user that weren't set with
loggingContextPutAlways(String, String)
-
loggingContextReset
Reset the current logging diagnostic context.Removes all items added to context by the user that weren't set with
loggingContextPutAlways(String, String)
-