From ded9cfb23b3f94b5d9ea6d892470bb1fc85e2235 Mon Sep 17 00:00:00 2001 From: mo khan Date: Thu, 8 Jul 2010 18:03:58 -0600 Subject: got the client and the server to play ping pong. --- code/client/Client.cs | 4 +- code/client/StartServiceBus.cs | 6 +- code/client/StartedApplicationHandler.cs | 13 --- code/client/client.csproj | 1 - code/common/AbstractHandler.cs | 44 ++++---- code/common/Command.cs | 7 ++ code/common/Logging.cs | 30 +----- code/common/MessageHandler.cs | 68 ++++++------ code/common/NeedStartup.cs | 8 +- code/common/RequestHandler.cs | 28 +++++ code/common/RhinoPublisher.cs | 113 ++++++++++--------- code/common/common.csproj | 168 +++++++++++++++-------------- code/common/messages/Message.cs | 21 ++++ code/common/messages/StartedApplication.cs | 18 ---- code/server/Server.cs | 15 +-- code/server/StartServiceBus.cs | 11 +- code/server/StartedApplicationHandler.cs | 13 --- code/server/server.csproj | 1 - 18 files changed, 269 insertions(+), 300 deletions(-) delete mode 100644 code/client/StartedApplicationHandler.cs create mode 100644 code/common/Command.cs create mode 100644 code/common/RequestHandler.cs create mode 100644 code/common/messages/Message.cs delete mode 100644 code/common/messages/StartedApplication.cs delete mode 100644 code/server/StartedApplicationHandler.cs diff --git a/code/client/Client.cs b/code/client/Client.cs index 108fdfa..f465c6c 100644 --- a/code/client/Client.cs +++ b/code/client/Client.cs @@ -11,7 +11,7 @@ namespace client { class Client { - static void Main(string[] args) + static void Main() { Process.Start(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\server\bin\Debug\server.exe"))); @@ -65,7 +65,7 @@ namespace client builder.Register().As().SingletonScoped(); - builder.Register().As(); + builder.Register().As(); Resolve.the>().each(x => x.run()); Resolve.the().run(); diff --git a/code/client/StartServiceBus.cs b/code/client/StartServiceBus.cs index 380a555..e7787c9 100644 --- a/code/client/StartServiceBus.cs +++ b/code/client/StartServiceBus.cs @@ -14,7 +14,11 @@ namespace client handler.handler(x); }); Resolve.the().add(receiver); - Resolve.the().publish(x => x.message = "client"); + Resolve.the().publish(x => + { + x.source = "client"; + x.message = "ping"; + }); } } } \ No newline at end of file diff --git a/code/client/StartedApplicationHandler.cs b/code/client/StartedApplicationHandler.cs deleted file mode 100644 index 65c7419..0000000 --- a/code/client/StartedApplicationHandler.cs +++ /dev/null @@ -1,13 +0,0 @@ -using common; -using common.messages; - -namespace client -{ - public class StartedApplicationHandler : AbstractHandler - { - public override void handle(StartedApplication item) - { - "received {0}".log(item.message); - } - } -} \ No newline at end of file diff --git a/code/client/client.csproj b/code/client/client.csproj index 17cee4a..138428b 100644 --- a/code/client/client.csproj +++ b/code/client/client.csproj @@ -54,7 +54,6 @@ - diff --git a/code/common/AbstractHandler.cs b/code/common/AbstractHandler.cs index 7575291..7387e21 100644 --- a/code/common/AbstractHandler.cs +++ b/code/common/AbstractHandler.cs @@ -1,24 +1,22 @@ -using System; - -namespace common -{ - public abstract class AbstractHandler : Handler, Handler - { - bool can_handle(Type type) - { - this.log().debug("{0} can handle {1} = {2}", this, type, typeof (T).Equals(type)); - return typeof (T).Equals(type); - } - - public void handle(object item) - { - if (can_handle(item.GetType())) - { - this.log().debug("handling... {0}", item); - handle((T) item); - } - } - - public abstract void handle(T item); - } +using System; + +namespace common +{ + public abstract class AbstractHandler : Handler, Handler + { + bool can_handle(Type type) + { + return typeof (T).Equals(type); + } + + public void handle(object item) + { + if (can_handle(item.GetType())) + { + handle((T) item); + } + } + + public abstract void handle(T item); + } } \ No newline at end of file diff --git a/code/common/Command.cs b/code/common/Command.cs new file mode 100644 index 0000000..31075ec --- /dev/null +++ b/code/common/Command.cs @@ -0,0 +1,7 @@ +namespace common +{ + public interface Command + { + void run(); + } +} \ No newline at end of file diff --git a/code/common/Logging.cs b/code/common/Logging.cs index 4afa631..e1db1bf 100644 --- a/code/common/Logging.cs +++ b/code/common/Logging.cs @@ -1,44 +1,18 @@ using System; -using System.IO; using System.Reflection; namespace common { static public class Logging { - static public Logger log(this T item) - { - return new TextLogger(Console.Out); - } - static public void log(this string item, params object[] arguments) { - new TextLogger(Console.Out).debug(item, arguments); + Console.Out.WriteLine("{0}: {1}".format(Assembly.GetEntryAssembly().GetName().Name, item.format(arguments))); } static public void add_to_log(this Exception item) { - new TextLogger(Console.Out).debug(item.Message); + item.Message.log(); } } - - public class TextLogger : Logger - { - readonly TextWriter writer; - - public TextLogger(TextWriter writer) - { - this.writer = writer; - } - - public void debug(string message, params object[] arguments) - { - writer.WriteLine("{0}: {1}".format(Assembly.GetEntryAssembly().GetName().Name, message.format(arguments))); - } - } - - public interface Logger - { - void debug(string message, params object[] arguments); - } } \ No newline at end of file diff --git a/code/common/MessageHandler.cs b/code/common/MessageHandler.cs index b990271..acd63eb 100644 --- a/code/common/MessageHandler.cs +++ b/code/common/MessageHandler.cs @@ -1,37 +1,33 @@ -using System; -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; -using ProtoBuf; -using Rhino.Queues.Model; - -namespace common -{ - public class MessageHandler - { - BinaryFormatter formatter = new BinaryFormatter(); - DependencyRegistry registry; - - public MessageHandler(DependencyRegistry registry) - { - this.registry = registry; - } - - public void handler(Message item) - { - var payload = parse_payload_from(item); - this.log().debug("received: {0}", payload); - registry - .get_all() - .each(x => x.handle(payload)); - } - - object parse_payload_from(Message item) - { - using (var stream = new MemoryStream(item.Data)) - { - //return formatter.Deserialize(stream); - return Serializer.NonGeneric.Deserialize(Type.GetType(item.Headers["type"]), stream); - } - } - } +using System; +using System.IO; +using ProtoBuf; +using Rhino.Queues.Model; + +namespace common +{ + public class MessageHandler + { + DependencyRegistry registry; + + public MessageHandler(DependencyRegistry registry) + { + this.registry = registry; + } + + public void handler(Message item) + { + var payload = parse_payload_from(item); + registry + .get_all() + .each(x => x.handle(payload)); + } + + object parse_payload_from(Message item) + { + using (var stream = new MemoryStream(item.Data)) + { + return Serializer.NonGeneric.Deserialize(Type.GetType(item.Headers["type"]), stream); + } + } + } } \ No newline at end of file diff --git a/code/common/NeedStartup.cs b/code/common/NeedStartup.cs index df5654e..d2264c2 100644 --- a/code/common/NeedStartup.cs +++ b/code/common/NeedStartup.cs @@ -1,6 +1,4 @@ -namespace common -{ - public interface NeedStartup : Command {} - - public interface Command { void run();} +namespace common +{ + public interface NeedStartup : Command {} } \ No newline at end of file diff --git a/code/common/RequestHandler.cs b/code/common/RequestHandler.cs new file mode 100644 index 0000000..416988e --- /dev/null +++ b/code/common/RequestHandler.cs @@ -0,0 +1,28 @@ +using System; +using System.Reflection; +using System.Threading; +using common.messages; + +namespace common +{ + public class RequestHandler : AbstractHandler + { + ServiceBus bus; + + public RequestHandler(ServiceBus bus) + { + this.bus = bus; + } + + public override void handle(Message item) + { + "received from {0}: {1} {2}".log(item.source, item.message, DateTime.Now); + Thread.Sleep(5000); + bus.publish(x => + { + x.message = item.message.Equals("ping") ? "pong" : "ping"; + x.source = Assembly.GetEntryAssembly().GetName().Name; + }); + } + } +} \ No newline at end of file diff --git a/code/common/RhinoPublisher.cs b/code/common/RhinoPublisher.cs index 705e7a9..0905b4d 100644 --- a/code/common/RhinoPublisher.cs +++ b/code/common/RhinoPublisher.cs @@ -1,60 +1,55 @@ -using System; -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; -using System.Transactions; -using ProtoBuf; -using Rhino.Queues; - -namespace common -{ - public class RhinoPublisher : ServiceBus - { - BinaryFormatter formatter = new BinaryFormatter(); - readonly int port; - string destination_queue; - IQueueManager sender; - - public RhinoPublisher(string destination_queue, int port, IQueueManager manager) - { - this.port = port; - this.destination_queue = destination_queue; - sender = manager; - } - - public void publish() where T : new() - { - publish(new T()); - } - - public void publish(T item) where T : new() - { - using (var transaction = new TransactionScope()) - { - var destination = "rhino.queues://localhost:{0}/{1}".format(port, destination_queue); - this.log().debug("sending {0} to {1}", item, destination); - sender.Send(new Uri(destination), create_payload_from(item)); - transaction.Complete(); - } - } - - MessagePayload create_payload_from(T item) - { - using (var stream = new MemoryStream()) - { - Serializer.Serialize(stream, item); - //formatter.Serialize(stream, item); - - var payload = new MessagePayload {Data = stream.ToArray()}; - payload.Headers["type"] = typeof (T).FullName; - return payload; - } - } - - public void publish(Action configure) where T : new() - { - var item = new T(); - configure(item); - publish(item); - } - } +using System; +using System.IO; +using System.Transactions; +using ProtoBuf; +using Rhino.Queues; + +namespace common +{ + public class RhinoPublisher : ServiceBus + { + readonly int port; + string destination_queue; + IQueueManager sender; + + public RhinoPublisher(string destination_queue, int port, IQueueManager manager) + { + this.port = port; + this.destination_queue = destination_queue; + sender = manager; + } + + public void publish() where T : new() + { + publish(new T()); + } + + public void publish(T item) where T : new() + { + using (var transaction = new TransactionScope()) + { + var destination = "rhino.queues://localhost:{0}/{1}".format(port, destination_queue); + sender.Send(new Uri(destination), create_payload_from(item)); + transaction.Complete(); + } + } + + MessagePayload create_payload_from(T item) + { + using (var stream = new MemoryStream()) + { + Serializer.Serialize(stream, item); + var payload = new MessagePayload {Data = stream.ToArray()}; + payload.Headers["type"] = typeof (T).FullName; + return payload; + } + } + + public void publish(Action configure) where T : new() + { + var item = new T(); + configure(item); + publish(item); + } + } } \ No newline at end of file diff --git a/code/common/common.csproj b/code/common/common.csproj index 0047226..a8dd958 100644 --- a/code/common/common.csproj +++ b/code/common/common.csproj @@ -1,84 +1,86 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {B34D543A-B443-4344-BD0E-2CFC6283D643} - Library - Properties - common - common - v4.0 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\..\external\auto.fac\Autofac.dll - - - ..\..\external\proto-buf.net\protobuf-net.dll - - - ..\..\external\rhino.queues\Rhino.Queues.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {B34D543A-B443-4344-BD0E-2CFC6283D643} + Library + Properties + common + common + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\external\auto.fac\Autofac.dll + + + ..\..\external\proto-buf.net\protobuf-net.dll + + + ..\..\external\rhino.queues\Rhino.Queues.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/code/common/messages/Message.cs b/code/common/messages/Message.cs new file mode 100644 index 0000000..1a526f2 --- /dev/null +++ b/code/common/messages/Message.cs @@ -0,0 +1,21 @@ +using System; +using ProtoBuf; + +namespace common.messages +{ + [Serializable] + [ProtoContract] + public class Message + { + [ProtoMember(1)] + public string source { get; set; } + + [ProtoMember(2)] + public string message { get; set; } + + public override string ToString() + { + return base.ToString() + source; + } + } +} \ No newline at end of file diff --git a/code/common/messages/StartedApplication.cs b/code/common/messages/StartedApplication.cs deleted file mode 100644 index 83ccf0e..0000000 --- a/code/common/messages/StartedApplication.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using ProtoBuf; - -namespace common.messages -{ - [Serializable] - [ProtoContract] - public class StartedApplication - { - [ProtoMember(1)] - public string message { get; set; } - - public override string ToString() - { - return base.ToString() + message; - } - } -} \ No newline at end of file diff --git a/code/server/Server.cs b/code/server/Server.cs index 41791b8..6c374aa 100644 --- a/code/server/Server.cs +++ b/code/server/Server.cs @@ -9,7 +9,7 @@ namespace server { class Server { - static void Main(string[] args) + static void Main() { try { @@ -46,26 +46,15 @@ namespace server Resolve.initialize_with(registry); builder.Register(x => registry).As().SingletonScoped(); - //needs startups builder.Register().As(); - // infrastructure - var manager = new QueueManager(new IPEndPoint(IPAddress.Loopback, 2200), "server.esent"); manager.CreateQueues("server"); builder.Register(x => new RhinoPublisher("client", 2201, manager)).As().SingletonScoped(); builder.Register(x => new RhinoReceiver(manager.GetQueue("server"), x.Resolve())).As().As().SingletonScoped(); - // commanding builder.Register().As().SingletonScoped(); - builder.Register().As(); - - // queries - - // repositories - //builder.Register().As().FactoryScoped(); - //builder.Register().As().FactoryScoped(); - + builder.Register().As(); Resolve.the>().each(x => x.run()); Resolve.the().run(); diff --git a/code/server/StartServiceBus.cs b/code/server/StartServiceBus.cs index 9a90cec..1b3b56e 100644 --- a/code/server/StartServiceBus.cs +++ b/code/server/StartServiceBus.cs @@ -13,13 +13,16 @@ namespace server { //using (var unit_of_work = Resolve.the().create()) //{ - handler.handler(x); - //unit_of_work.commit(); + handler.handler(x); + //unit_of_work.commit(); //} }); Resolve.the().add(receiver); - //ThreadPool.QueueUserWorkItem(x => receiver.run()); - Resolve.the().publish(x => x.message = "server"); + Resolve.the().publish(x => + { + x.source = "server"; + x.message = "ping"; + }); } } } \ No newline at end of file diff --git a/code/server/StartedApplicationHandler.cs b/code/server/StartedApplicationHandler.cs deleted file mode 100644 index f73b5ae..0000000 --- a/code/server/StartedApplicationHandler.cs +++ /dev/null @@ -1,13 +0,0 @@ -using common; -using common.messages; - -namespace server -{ - public class StartedApplicationHandler : AbstractHandler - { - public override void handle(StartedApplication item) - { - "received {0}".log(item.message); - } - } -} \ No newline at end of file diff --git a/code/server/server.csproj b/code/server/server.csproj index 475586b..0ba5770 100644 --- a/code/server/server.csproj +++ b/code/server/server.csproj @@ -52,7 +52,6 @@ - -- cgit v1.2.3