This is not really part 3, but a correction to the design of the fractal processing server sample. Currently, the design we use is the one in the following picture:

I remind you that the component PixelStoreLogic receives events from the RequestListener and events from the ColorConvertLogic. The events from the RequestListener tell our logic to store the pixels received from the ColorConvertLogic. Once PixelStoreLogic receives all colored pixels of the image, it publishes an event containing all these pixels, and also cleans these events from its storage.

The problem with the current design is that since components work in parallel, it might happen that a colored pixel event from ColorConvertLogic arrives to PixelStoreLogic before the event’s corresponding request arrives! Therefore the PixelStoreLogic will receive an event it was not told to store its pixels!

The solution is pretty simple: we make all requests first go through PixelStoreLogic, and then forward them to the PixelGeneratorLogic.

In terms of code:

In the component builder we replace this line:

1
requestListener.Subscribe(pixelGeneratorLogic, new ActivateAllRule());

with this line:

1
pixelStoreLogic.Subscribe(pixelGeneratorLogic, new ActivateAllRule());

And in PixelStoreLogic we change the behavior of what happens when we receive a request event:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public override IEvent ProcessEvent(IEvent eventToProcess)
{
    switch (eventToProcess)
    {
        case FractalRequestEvent requestEvent:
            return ProcessFractalRequest(requestEvent);
        case ColoredPixelEvent coloredPixel:
            ProcessColoredPixel(coloredPixel);
            break;
    }

    return null;
}

private FractalRequestEvent ProcessFractalRequest(FractalRequestEvent requestEvent)
{
    _requestToPixels[requestEvent] = new List<ColoredPixelEvent>();
    return requestEvent;
}

We also need to add now rules between the PixelStoreLogic and the PixelGeneratorLogic and the BitmapConvertLogic, so that they receive only the events of the types they expect to handle. This can be done with a TypeRule, which filters events according to their type, and will be discussed later.

So we make two more modifications to the component builder code:

1
2
3
pixelStoreLogic.Subscribe(bitmapConvertLogic, new TypeRule(typeof(ImageEvent)));
// ...
pixelStoreLogic.Subscribe(pixelGeneratorLogic, new TypeRule(typeof(FractalRequestEvent)));

Where before the modifications, we subscribed the components to PixelStoreLogic using ActivateAllRule.

That’s it, we fixed our issue with the server’s design! You can find the relevant diff here.