1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use anyhow::{Context,Result,anyhow};
use log::{debug,error};
use prost::{Message};
use crate::axon_utils::{ApplicableTo, AxonConnection, AxonServerHandle, EmitApplicableEventsAndResponse, HandlerRegistry, command_worker, create_aggregate_definition, emit_applicable, emit_applicable_events_and_response, empty_handler_registry, empty_aggregate_registry};
use crate::grpc_example::{Acknowledgement,GreetCommand,GreetedEvent,GreeterProjection,RecordCommand,StartedRecordingEvent,StopCommand,StoppedRecordingEvent};

pub async fn handle_commands(axon_server_handle : AxonServerHandle) {
    if let Err(e) = internal_handle_commands(axon_server_handle).await {
        error!("Error while handling commands: {:?}", e);
    }
    debug!("Stopped handling commands for example application");
}

async fn internal_handle_commands(axon_server_handle : AxonServerHandle) -> Result<()> {
    debug!("Handle commands for example application");
    let axon_connection = AxonConnection {
        id: axon_server_handle.display_name,
        conn: axon_server_handle.conn,
    };
    debug!("Axon connection: {:?}", axon_connection);

    let mut aggregate_id_extractor_registry = empty_handler_registry();
    let mut sourcing_handler_registry = empty_handler_registry();
    let mut command_handler_registry = empty_handler_registry();

    sourcing_handler_registry.insert_with_output(
        "GreetedEvent",
        &GreetedEvent::decode,
        &(|c, p| Box::pin(handle_sourcing_event(Box::from(c), p)))
    )?;

    sourcing_handler_registry.insert_with_output(
        "StoppedRecordingEvent",
        &StoppedRecordingEvent::decode,
        &(|c, p| Box::pin(handle_sourcing_event(Box::from(c), p)))
    )?;

    sourcing_handler_registry.insert_with_output(
        "StartedRecordingEvent",
        &StartedRecordingEvent::decode,
        &(|c, p| Box::pin(handle_sourcing_event(Box::from(c), p)))
    )?;

    aggregate_id_extractor_registry.insert_with_output(
        "GreetCommand",
        &GreetCommand::decode,
        &(|_, _| Box::pin(fixed_aggregate_id()))
    )?;

    command_handler_registry.insert_with_output(
        "GreetCommand",
        &GreetCommand::decode,
        &(|c, p| Box::pin(handle_greet_command(c, p)))
    )?;

    aggregate_id_extractor_registry.insert_with_output(
        "RecordCommand",
        &RecordCommand::decode,
        &(|_, _| Box::pin(fixed_aggregate_id()))
    )?;

    command_handler_registry.insert_with_output(
        "RecordCommand",
        &RecordCommand::decode,
        &(|c, p| Box::pin(handle_record_command(c, p)))
    )?;

    aggregate_id_extractor_registry.insert_with_output(
        "StopCommand",
        &StopCommand::decode,
        &(|_, _| Box::pin(fixed_aggregate_id()))
    )?;

    command_handler_registry.insert_with_output(
        "StopCommand",
        &StopCommand::decode,
        &(|c, p| Box::pin(handle_stop_command(c, p)))
    )?;

    let empty_projection = Box::new(|| {
        let mut projection = GreeterProjection::default();
        projection.is_recording = true;
        projection
    });

    let aggregate_definition = create_aggregate_definition(
        "GreeterProjection".to_string(),
        empty_projection,
        aggregate_id_extractor_registry,
        command_handler_registry,
        sourcing_handler_registry
    );

    let mut aggregate_registry = empty_aggregate_registry();
    aggregate_registry.handlers.insert(aggregate_definition.projection_name.clone(), Box::from(aggregate_definition));

    command_worker(axon_connection, aggregate_registry).await.context("Error while handling commands")
}

async fn handle_sourcing_event<T: ApplicableTo<P>,P: Clone>(event: Box<T>, projection: P) -> Result<Option<P>> {
    let mut p = projection.clone();
    event.apply_to(&mut p)?;
    Ok(Some(p))
}

async fn fixed_aggregate_id() -> Result<Option<String>> {
    Ok(Some("xxx".to_string()))
}

impl ApplicableTo<GreeterProjection> for GreetedEvent {

    fn apply_to(self: &Self, projection: &mut GreeterProjection) -> Result<()> {
        debug!("Apply greeted event to GreeterProjection: {:?}", projection.is_recording);
        Ok(())
    }

    fn box_clone(self: &Self) -> Box<dyn ApplicableTo<GreeterProjection>> {
        Box::from(GreetedEvent::clone(self))
    }
}

impl ApplicableTo<GreeterProjection> for StartedRecordingEvent {

    fn apply_to(self: &Self, projection: &mut GreeterProjection) -> Result<()> {
        debug!("Apply StartedRecordingEvent to GreeterProjection: {:?}", projection.is_recording);
        projection.is_recording = true;
        Ok(())
    }

    fn box_clone(self: &Self) -> Box<dyn ApplicableTo<GreeterProjection>> {
        Box::from(StartedRecordingEvent::clone(self))
    }
}

impl ApplicableTo<GreeterProjection> for StoppedRecordingEvent {

    fn apply_to(self: &Self, projection: &mut GreeterProjection) -> Result<()> {
        debug!("Apply StoppedRecordingEvent to GreeterProjection: {:?}", projection.is_recording);
        projection.is_recording = false;
        Ok(())
    }

    fn box_clone(self: &Self) -> Box<dyn ApplicableTo<GreeterProjection>> {
        Box::from(StoppedRecordingEvent::clone(self))
    }
}

async fn handle_greet_command (command: GreetCommand, projection: GreeterProjection) -> Result<Option<EmitApplicableEventsAndResponse<GreeterProjection>>> {
    debug!("Greet command handler: {:?}", command);
    if !projection.is_recording {
        debug!("Not recording, so no events emitted nor acknowledgement returned");
        return Ok(None);
    }
    debug!("Recording, so proceed");
    let greeting = command.message;
    let message = greeting.clone().map(|g| g.message).unwrap_or("-/-".to_string());
    if message == "ERROR" {
        return Err(anyhow!("Panicked at reading 'ERROR'"));
    }
    let mut emit_events = emit_applicable_events_and_response("Acknowledgement", &Acknowledgement {
        message: format!("ACK! {}", message),
    })?;
    emit_applicable(&mut emit_events, "GreetedEvent", Box::from(GreetedEvent {
        message: greeting,
    }))?;
    debug!("Emit events and response: {:?}", emit_events);
    Ok(Some(emit_events))
}

async fn handle_record_command (command: RecordCommand, projection: GreeterProjection) -> Result<Option<EmitApplicableEventsAndResponse<GreeterProjection>>> {
    debug!("Record command handler: {:?}", command);
    if projection.is_recording {
        return Ok(None)
    }
    let mut emit_events = emit_applicable_events_and_response("Empty", &())?;
    emit_applicable(&mut emit_events, "StartedRecordingEvent", Box::from(StartedRecordingEvent {}))?;
    Ok(Some(emit_events))
}

async fn handle_stop_command (command: StopCommand, projection: GreeterProjection) -> Result<Option<EmitApplicableEventsAndResponse<GreeterProjection>>> {
    debug!("Stop command handler: {:?}", command);
    if !projection.is_recording {
        return Ok(None)
    }
    let mut emit_events = emit_applicable_events_and_response("Empty", &())?;
    emit_applicable(&mut emit_events, "StoppedRecordingEvent", Box::from(StoppedRecordingEvent {}))?;
    Ok(Some(emit_events))
}