Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm not sure if this a common scenario or not, but I have a need along the following lines:

I have an object (I'll call it node) and this object has methods X, Y, and Z. I want to be able to have subscribers be notified if the methods are called in a specific order with specific timing (a range of time would be ideal). For instance:

  1. Node.X() get called
  2. Node.Z() is called 5 to 7 milliseconds latter
  3. Node.X() is called 1 to 2 milliseconds after that
  4. Node.Y() is called 2 milliseconds seconds after that.
  5. Finally, An event calling a subscriber to this sequence is fired.

Now, in this example I am simply using parameter-less methods, but in real life I'd need to be more specific about the contents of the parameters (I'd be looking for specific messages).

For reference I am using C# .net 4.

Are there any frameworks/patterns/libraries that exist out there that are built for doing this?

share|improve this question
2  
How are these methods going to be called? It seems like something in your system is responding to asynchronous external events. –  kevin cline Oct 3 '11 at 14:42
    
@kevincline that is somewhat up in the air right now (research phase) but the plan now is that each node would be hosted within a WCF process and other nodes would talk to each other via asynchronous named pipes calls. –  aceinthehole Oct 3 '11 at 14:52
    
Seeks like this SO question on implementing actors in C# might be relevant –  Joseph Weissman Oct 3 '11 at 14:59
    
@all for context, we are attempting to build a monitoring tool that uses a type of adaptive pattern recognition that will identify and subscribe to various events (this is a piece of that) as they happen in real time. –  aceinthehole Oct 3 '11 at 15:00
    
@aceinthehole Are you referring to complex event processing or event stream processing? I'm not sure about particular patterns, but (as of a couple of years ago) the canonical book on the subject is David Luckham's The Power of Events (2002). There are probably more resources and libraries out there that are newer if you search around for the terms I mentioned. –  Thomas Owens Oct 3 '11 at 16:54
add comment

4 Answers

I don't know of a framework or a library that can handle this out of the box, but one solution to your problem is implementing a finite state machine for event generation. As your example suggests, the FSM has 5 states. The transitions from i to i+1 are your conditions on your list. Every time a node function is called, you can pass in the event to the FSM, and it can check if the condition for state i is satisfied, and adjust state accordingly (condition holds => go to state i+1; else go to state 1). If you reach state 5, emit an event, and go back to state 1.

For example, if you are on state 3, and you receive EVENT_X_CALLED, then you check the current time, and compare it against transition time. If it is 1 to 2 ms apart, change state to 4. If not, go back to state 1.

share|improve this answer
add comment

I have used Microsoft PRISM's EventAggregator or MVVM Light's Messenger for message systems like this, and I would assume you'd have a class that subscribes to all 3 events and routes them to the same method.

In that method, check the message and see if it is the first type that handler is interested in. If yes, set a flag and a timer (optional) and wait for the next one. When you receive the 2nd event, check if the last received event was Event1 and if so set last received Event to Event2 and reset timer again. Repeat until you get all your events, and then process.

Here's a rough example:

public MyClassConstructor()
{
    eventAggregator.Subscribe<EventX>(ProcessEvents);
    eventAggregator.Subscribe<EventY>(ProcessEvents);
    eventAggregator.Subscribe<EventZ>(ProcessEvents);
}

private int eventCount = 0;
void ProcessEvents(object e)
{
    if ((e is EventX && eventCount == 0)
        || (e is EventZ && eventCount == 1)
        || (e is EventX && eventCount == 2)
    {
        eventCount++;
        // optionally start/reset timer which sets eventCount = 0
    }
    else if (e is EventY && eventCount == 3)
    {
        // Execute code
        eventCount = 0;
    }
}

Each Node.Method would be responsible for firing off an event message with parameters based on which method got executed

eventAggregator.Publish<EventX>(new EventX(params));
share|improve this answer
add comment

.NET (and ultimately, out of the box Windows) may not be your best option for this.

All events are going to have a significant amount of wiggle room as to when they are addressed by the operating system this SO thread goes into a bit of the background.

share|improve this answer
add comment

The Reactive Extensions for .NET could possibly be used to implement the scenario you describe. You can think of them as "LINQ to events". Check out the samples.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.