Skip to content

Commit

Permalink
add race tracker example
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Mar 3, 2024
1 parent 09800e2 commit 630f5a4
Show file tree
Hide file tree
Showing 6 changed files with 623 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fluxtion.example.cookbook.racing;

import com.fluxtion.runtime.annotations.ExportService;
import com.fluxtion.runtime.annotations.OnEventHandler;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

public class RaceCalculator {

public record RunnerStarted(long runnerId, Instant startTime) {
}

public record RunnerFinished(long runnerId, Instant finishTime) {
}

public interface ResultsPublisher {
void publishAllResults();
}

@Getter
public static class RaceTimeTracker {

private final transient Map<Long, Long> runnerRaceTimeMap = new HashMap<>();

@OnEventHandler(propagate = false)
public boolean runnerStarted(RunnerStarted runnerStarted) {
//add runner start time to map
return false;
}

@OnEventHandler
public boolean runnerFinished(RunnerFinished runnerFinished) {
//calc runner total race time and add to map
return true;
}
}

@RequiredArgsConstructor
public static class ResultsPublisherImpl implements @ExportService ResultsPublisher{

private final RaceTimeTracker raceTimeTracker;

@OnEventHandler(propagate = false)
public boolean runnerFinished(RunnerFinished runnerFinished) {
//get the runner race time and send individual their results
long raceTime = raceTimeTracker.getRunnerRaceTimeMap().get(runnerFinished.runnerId());
return false;
}

@Override
public void publishAllResults() {
//get all results and publish
var runnerRaceTimeMap = raceTimeTracker.getRunnerRaceTimeMap();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.fluxtion.example.cookbook.racing;

import com.fluxtion.compiler.EventProcessorConfig;
import com.fluxtion.compiler.FluxtionCompilerConfig;
import com.fluxtion.compiler.FluxtionGraphBuilder;
import com.fluxtion.example.cookbook.racing.RaceCalculator.RaceTimeTracker;
import com.fluxtion.example.cookbook.racing.RaceCalculator.ResultsPublisherImpl;

public class RaceCalculatorAotBuilder implements FluxtionGraphBuilder {
@Override
public void buildGraph(EventProcessorConfig eventProcessorConfig) {
RaceTimeTracker raceCalculator = eventProcessorConfig.addNode(new RaceTimeTracker(), "raceCalculator");
eventProcessorConfig.addNode(new ResultsPublisherImpl(raceCalculator), "resultsPublisher");
}

@Override
public void configureGeneration(FluxtionCompilerConfig fluxtionCompilerConfig) {
fluxtionCompilerConfig.setClassName("RaceCalculatorProcessor");
fluxtionCompilerConfig.setPackageName("com.fluxtion.example.cookbook.racing.generated");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fluxtion.example.cookbook.racing;

import com.fluxtion.example.cookbook.racing.generated.RaceCalculatorProcessor;

import java.time.Instant;

import static com.fluxtion.example.cookbook.racing.RaceCalculator.*;

public class RaceCalculatorApp {
public static void main(String[] args) {
RaceCalculatorProcessor raceCalculatorProcessor = new RaceCalculatorProcessor();
raceCalculatorProcessor.init();

ResultsPublisher resultsPublisher = raceCalculatorProcessor.getExportedService();

//connect to event stream and process runner timing events
raceCalculatorProcessor.onEvent(new RunnerStarted(1, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerStarted(2, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerStarted(3, Instant.now()));

raceCalculatorProcessor.onEvent(new RunnerFinished(2, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerFinished(3, Instant.now()));
raceCalculatorProcessor.onEvent(new RunnerFinished(1, Instant.now()));

//publish full results
resultsPublisher.publishAllResults();
}
}
Loading

0 comments on commit 630f5a4

Please sign in to comment.