Skip to content

Commit

Permalink
simplified the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
paxel committed Aug 15, 2023
1 parent 6a999e1 commit 017031a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 126 deletions.
6 changes: 6 additions & 0 deletions src/main/java/paxel/lintstone/api/ActorSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* The actor settings for the creation of configured actors.
*/
public interface ActorSettings {

/**
* Non-blocking, unlimited and one by one processing Settings. Save but not optimal.
*/
ActorSettings DEFAULT = ActorSettings.create().build();

/**
* Defines if the Actor receives messages from multiple sources.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,16 @@ public interface LintStoneMessageEventContext {
*/
LintStoneActorAccess getActor(String name);

/**
* This method deleagtes to
* {@link LintStoneSystem#registerActor(String, LintStoneActorFactory, Optional)}.
*
* @param name The name of the actor.
* @param factory The factory.
* @param initMessage The init message.
* @return The new or old actor access.
*/
LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);

/**
* This method deleagtes to
* {@link LintStoneSystem#registerSingleSourceActor(String, LintStoneActorFactory, Optional)}.
*
* @param name The name of the actor.
* @param factory The factory.
* @param initMessage The init message.
* @return The new or old actor access.
*/
LintStoneActorAccess registerSingleSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);

/**
* This method delegates to
* {@link LintStoneSystem#registerMultiSourceActor(java.lang.String, paxel.lintstone.api.LintStoneActorFactory, java.util.Optional)}.
* {@link LintStoneSystem#registerActor(String, LintStoneActorFactory, Optional, ActorSettings)}.
*
* @param name The name of the actor.
* @param factory The factory.
* @param initMessage The init message.
* @return The new or old actor access.
*/
LintStoneActorAccess registerMultiSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);
LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, ActorSettings settings);

/**
* Unregisters this Actor.
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/paxel/lintstone/api/LintStoneSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,6 @@
*/
public interface LintStoneSystem {

/**
* This checks if there is already an Actor with the given name, and if not
* uses the {@link LintStoneActorFactory} to create one. The initMessage
* will be the first message the newly created actor will receive.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param initMessage The optional init message.
* @return The {@link LintStoneActorAccess} object
*/
@Deprecated
LintStoneActorAccess registerMultiSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);

/**
* This is the same method as {@link #registerMultiSourceActor(String, LintStoneActorFactory, Optional)}
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param initMessage The optional init message.
* @return The {@link LintStoneActorAccess} object
*/
@Deprecated
LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);

/**
* This checks if there is already an Actor with the given name, and if not
* uses the {@link LintStoneActorFactory} to create one. The initMessage
* will be the first message the newly created actor will receive.
* <p>
* The resulting actor is a bit better performing than the one resulting from {@link #registerMultiSourceActor(String, LintStoneActorFactory, Optional)},
* but can only receive messages from a single thread.
*
* @param name The name of the actor. The name must be unique in the system.
* @param factory The factory to create the actor if not already exists.
* @param initMessage The optional init message.
* @return The {@link LintStoneActorAccess} object
*/
@Deprecated
LintStoneActorAccess registerSingleSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage);

/**
* This generates and registers an Actor according to the given {@link ActorSettings}.
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/paxel/lintstone/impl/ActorSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ public ActorSystem(ExecutorService executorService) {
groupingExecutor = new GroupingExecutor(executorService);
}

@Override
@Deprecated
public LintStoneActorAccess registerMultiSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
Optional<SelfUpdatingActorAccess> sender = Optional.empty();
return registerActor(name, factory, initMessage, sender, groupingExecutor.create().setMultiSource(true).build());
}

@Override
@Deprecated
public LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
return registerMultiSourceActor(name, factory, initMessage);
}

@Override
@Deprecated
public LintStoneActorAccess registerSingleSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
Optional<SelfUpdatingActorAccess> sender = Optional.empty();
return registerActor(name, factory, initMessage, sender, groupingExecutor.create().build());
}

@Override
public LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, ActorSettings settings) {
Expand All @@ -69,13 +50,6 @@ LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, O
return registerActor(name, factory, initMessage, sender, sequentialProcessorBuilder.build());
}

LintStoneActorAccess registerSingleSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, Optional<SelfUpdatingActorAccess> sender) {
return registerActor(name, factory, initMessage, sender, groupingExecutor.create().build());
}

LintStoneActorAccess registerMultiSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, Optional<SelfUpdatingActorAccess> sender) {
return registerActor(name, factory, initMessage, sender, groupingExecutor.create().setMultiSource(true).build());
}

private LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, Optional<SelfUpdatingActorAccess> sender, SequentialProcessor sequentialProcessor) {
synchronized (actors) {
Expand Down
28 changes: 8 additions & 20 deletions src/main/java/paxel/lintstone/impl/FailedMessage.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
package paxel.lintstone.impl;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import paxel.lintstone.api.LintStoneFailedMessage;

@ToString
@Getter
@AllArgsConstructor
@EqualsAndHashCode
public class FailedMessage implements LintStoneFailedMessage {

private final Object message;
private final Throwable cause;
private final String actorName;

public FailedMessage(Object message, Throwable cause, String actorName) {
this.message = message;
this.cause = cause;
this.actorName = actorName;
}

@Override
public Object getMessage() {
return message;
}

@Override
public Throwable getCause() {
return cause;
}

@Override
public String getActorName() {
return actorName;
}

}
14 changes: 4 additions & 10 deletions src/main/java/paxel/lintstone/impl/MessageContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,13 @@ public LintStoneActorAccess getActor(String name) {
return new SelfUpdatingActorAccess(name, null, actorSystem, Optional.of(self));
}

@Override
public LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
return actorSystem.registerActor(name, factory, initMessage, Optional.of(self));
}

@Override
public LintStoneActorAccess registerSingleSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
return actorSystem.registerMultiSourceActor(name, factory, initMessage, Optional.of(self));
}



@Override
public LintStoneActorAccess registerMultiSourceActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage) {
return actorSystem.registerMultiSourceActor(name, factory, initMessage, Optional.of(self));
public LintStoneActorAccess registerActor(String name, LintStoneActorFactory factory, Optional<Object> initMessage, ActorSettings settings) {
return actorSystem.registerActor(name, factory, initMessage, Optional.of(self),settings);
}

@Override
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/paxel/lintstone/api/FailingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public FailingTests() {
@Test
public void testSomeMethod() throws InterruptedException {
LintStoneSystem system = LintStoneSystemFactory.create(Executors.newWorkStealingPool());
LintStoneActorAccess stopper = system.registerActor("floor", () -> a -> latch.countDown(), Optional.empty(),ActorSettings.create().setMulti(true).build());
LintStoneActorAccess lala = system.registerActor("lala", () -> new StupidActor(), Optional.of("Go"),ActorSettings.create().setMulti(true).build());
LintStoneActorAccess stopper = system.registerActor("floor", () -> a -> latch.countDown(), Optional.empty(), ActorSettings.create().setMulti(true).build());
LintStoneActorAccess lala = system.registerActor("lala", () -> new StupidActor(), Optional.of("Go"), ActorSettings.create().setMulti(true).build());

LintStoneActorAccess lulu = system.registerActor("lulu", () -> a -> {
a.reply("nope");
}, Optional.empty(),ActorSettings.create().setMulti(true).build());
}, Optional.empty(), ActorSettings.create().setMulti(true).build());

lulu.send("you ok?");

Expand Down Expand Up @@ -57,12 +57,12 @@ public void newMessageEvent(LintStoneMessageEventContext mec) {
private void handleString(String go, LintStoneMessageEventContext mec) {
LintStoneActorAccess registered = mec.registerActor("dala", () -> m -> {
throw new IllegalArgumentException("Go away");
}, Optional.empty());
}, Optional.empty(), ActorSettings.DEFAULT);

if (registered.exists()) {
LintStoneActorAccess second = mec.registerActor("dala", () -> m -> {
System.out.print("I am ignored");
}, Optional.empty());
}, Optional.empty(), ActorSettings.DEFAULT);

if (second.exists()) {
// will fail on the other actor and produce a failed message for us.
Expand Down Expand Up @@ -106,14 +106,14 @@ private void handleString(FailedMessage go, LintStoneMessageEventContext m) {

LintStoneActorAccess oldActor = m.registerActor("someOne", () -> a -> {

}, Optional.empty());
}, Optional.empty(), ActorSettings.DEFAULT);
oldActor.send("lala");
// unregister that one
if (m.unregister("someOne")) {
// register a new one
m.registerActor("someOne", () -> a -> {

}, Optional.empty());
}, Optional.empty(), ActorSettings.DEFAULT);

oldActor.send("works");

Expand Down

0 comments on commit 017031a

Please sign in to comment.