io.vlingo.actors.Environment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vlingo-actors Show documentation
Show all versions of vlingo-actors Show documentation
Type safe Actor Model toolkit for reactive concurrency and resiliency using Java and other JVM languages.
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.actors;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.pcollections.PVector;
import org.pcollections.TreePVector;
public class Environment {
final Address address;
PVector children;
Address completesEventuallyAddress;
final Definition definition;
final FailureMark failureMark;
final Logger logger;
final Mailbox mailbox;
final Supervisor maybeSupervisor;
final Actor parent;
final Map proxyCache;
final Stage stage;
// final Stowage stowage;
// final Stowage suspended;
private final AtomicBoolean secured;
private final AtomicBoolean stopped;
private Class>[] stowageOverrides;
protected Environment(
final Stage stage,
final Address address,
final Definition definition,
final Actor parent,
final Mailbox mailbox,
final Supervisor maybeSupervisor,
final Logger logger) {
assert(stage != null);
this.stage = stage;
assert(address != null);
this.address = address;
assert(definition != null);
this.definition = definition;
if (address.id() != World.PRIVATE_ROOT_ID) assert(parent != null);
this.parent = parent;
assert(mailbox != null);
this.mailbox = mailbox;
this.maybeSupervisor = maybeSupervisor;
this.failureMark = new FailureMark();
this.logger = logger;
this.children = TreePVector.empty();
this.proxyCache = new HashMap<>(1);
// this.stowage = new Stowage();
this.stowageOverrides = null;
// this.suspended = new Stowage();
this.secured = new AtomicBoolean(false);
this.stopped = new AtomicBoolean(false);
}
void addChild(final Actor child) {
children = children.plus(child);
}
CompletesEventually completesEventually(final ResultReturns result) {
if (completesEventuallyAddress == null) {
final CompletesEventually completesEventually = stage.world().completesFor(result.clientReturns());
completesEventuallyAddress = completesEventually.address();
return completesEventually;
}
return stage.world().completesFor(completesEventuallyAddress, result.clientReturns());
}
void cacheProxy(final T proxy) {
proxyCache.put(proxy.getClass().getName(), proxy);
}
@SuppressWarnings("unchecked")
T lookUpProxy(final Class protocol) {
return (T) proxyCache.get(protocol.getName());
}
boolean isSecured() {
return secured.get();
}
void setSecured() {
secured.set(true);
}
boolean isStopped() {
return stopped.get();
}
void stop() {
if (stopped.compareAndSet(false, true)) {
stopChildren();
// suspended.reset();
// stowage.reset();
mailbox.close();
}
}
boolean isStowageOverride(final Class> protocol) {
if (stowageOverrides != null) {
for (final Class> override : stowageOverrides) {
if (override == protocol) {
return true;
}
}
}
return false;
}
void stowageOverrides(final Class>... overrides) {
stowageOverrides = overrides;
}
private void stopChildren() {
// TODO: re-implement as: children.forEach(child -> selfAs(Stoppable.class).stop());
children.forEach(child -> child.stop());
children = TreePVector.empty();
}
}