forked from peteraritchie/Event-handling
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IQueue<T>.Requeue, improvement to InMemQueue (renamed to InMemo…
…ryQueue), bus and supporting classes, and ThreadPoolPipe
- Loading branch information
1 parent
b4c67c8
commit 5d362fb
Showing
11 changed files
with
145 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
using System; | ||
namespace pvc.Core | ||
{ | ||
public class ActionWrappingConsumer<T> : Consumes<T> where T:Message | ||
{ | ||
private readonly Action<T> _action; | ||
public void Handle(T message) { | ||
_action(message); | ||
} | ||
using System; | ||
namespace pvc.Core | ||
{ | ||
public class ActionWrappingConsumer<T> : Consumes<T> where T:Message | ||
{ | ||
private readonly Action<T> _action; | ||
public void Handle(T message) { | ||
_action(message); | ||
} | ||
public ActionWrappingConsumer (Action<T> action) | ||
{ | ||
if (action == null) throw new ArgumentNullException("action"); | ||
_action = action; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
|
||
namespace pvc.Core.Bus | ||
{ | ||
public class ActionWrappingBusPublisher : IBus, Produces<Message> | ||
{ | ||
private readonly Action<Event> _eventAction; | ||
private readonly Action<Command> _commandAction; | ||
private Consumes<Message> _consumer; | ||
|
||
public ActionWrappingBusPublisher(Action<Event> eventAction, Action<Command> commandAction) | ||
{ | ||
_eventAction = eventAction; | ||
_commandAction = commandAction; | ||
} | ||
|
||
public void Publish(Event @event) | ||
{ | ||
_eventAction(@event); | ||
if (_consumer != null) | ||
{ | ||
_consumer.Handle(@event); | ||
} | ||
} | ||
|
||
public void Send(Command command) | ||
{ | ||
_commandAction(command); | ||
if (_consumer != null) | ||
{ | ||
_consumer.Handle(command); | ||
} | ||
} | ||
|
||
public void AttachConsumer(Consumes<Message> consumer) | ||
{ | ||
_consumer = consumer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace pvc.Core.Bus | ||
{ | ||
public interface Command : Message | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace pvc.Core.Bus | ||
{ | ||
public interface Event : Message | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace pvc.Core.Bus | ||
{ | ||
public interface IBus | ||
{ | ||
void Publish(Event @event); | ||
void Send(Command command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace pvc.Core | ||
{ | ||
public class InMemoryQueue<T> : IQueue<T> | ||
{ | ||
private readonly Queue<T> _queue = new Queue<T>(); //make this a concurrent queue, need to build on 3.5 for mono now | ||
|
||
public bool TryDequeue(out T item) | ||
{ | ||
lock (_queue) | ||
{ | ||
if (_queue.Count > 0) | ||
{ | ||
item = _queue.Dequeue(); | ||
return true; | ||
} | ||
Thread.Sleep(1); | ||
item = default(T); | ||
return false; | ||
} | ||
} | ||
|
||
public void Enqueue(T item) | ||
{ | ||
lock (_queue) | ||
{ | ||
_queue.Enqueue(item); | ||
} | ||
} | ||
|
||
public void MarkComplete(T item) | ||
{ | ||
|
||
} | ||
|
||
public void Requeue(T item) | ||
{ | ||
Enqueue(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Threading; | ||
|
||
namespace pvc.Core | ||
{ | ||
public class ThreadPoolPipe<T> : Pipe<T, T> where T : Message | ||
{ | ||
private Consumes<T> _consumer; | ||
|
||
public void Handle(T message) | ||
{ | ||
ThreadPool.QueueUserWorkItem(x => _consumer.Handle(message)); | ||
} | ||
|
||
public void AttachConsumer(Consumes<T> consumer) | ||
{ | ||
_consumer = consumer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters