Skip to content

Commit

Permalink
Reduce nesting in sample code.
Browse files Browse the repository at this point in the history
Document code in README a little.
  • Loading branch information
drewnoakes committed Feb 23, 2015
1 parent 771f395 commit 53e1674
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 134 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,29 @@ There are also a few blog posts available, which you can read about here
Here is a small example

```csharp
using (NetMQContext ctx = NetMQContext.Create())
using (var context = NetMQContext.Create())
using (var server = context.CreateResponseSocket())
using (var client = context.CreateRequestSocket())
{
using (var server = ctx.CreateResponseSocket())
{
server.Bind("tcp://127.0.0.1:5556");
using (var client = ctx.CreateRequestSocket())
{
client.Connect("tcp://127.0.0.1:5556");
client.Send("Hello");

string m1 = server.ReceiveString();
Console.WriteLine("From Client: {0}", m1);
server.Send("Hi Back");

string m2 = client.ReceiveString();
Console.WriteLine("From Server: {0}", m2);
Console.ReadLine();
}
}
// Bind the server to a local TCP address
server.Bind("tcp://localhost:5556");

// Connect the client to the server
client.Connect("tcp://localhost:5556");

// Send a message from the client socket
client.Send("Hello");

// Receive the message from the server socket
string m1 = server.ReceiveString();
Console.WriteLine("From Client: {0}", m1);

// Send a response back from the server
server.Send("Hi Back");

// Receive the response from the client socket
string m2 = client.ReceiveString();
Console.WriteLine("From Server: {0}", m2);
}
```

Expand Down
52 changes: 21 additions & 31 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,22 @@ So let's start with some code, the "Hello world" example (of course).
### Server

:::csharp
static void Main(string[] args)
using (var context = NetMQContext.Create())
using (var server = context.CreateResponseSocket())
{
using (var context = NetMQContext.Create())
{
using (var server = context.CreateResponseSocket())
{
server.Bind("tcp://*:5555");
server.Bind("tcp://*:5555");

while (true)
{
var message = server.ReceiveString();
while (true)
{
var message = server.ReceiveString();

Console.WriteLine("Received {0}", message);
Console.WriteLine("Received {0}", message);

// processing the request
Thread.Sleep(100);
// processing the request
Thread.Sleep(100);

Console.WriteLine("Sending World");
server.Send("World");
}
}
Console.WriteLine("Sending World");
server.Send("World");
}
}

Expand All @@ -81,23 +76,18 @@ You can also see that we have zero configuration, we are just sending strings. N
### Client

:::csharp
static void Main(string[] args)
using (var context = NetMQContext.Create())
using (var client = context.CreateRequestSocket())
{
using (var context = NetMQContext.Create())
client.Connect("tcp://localhost:5555");

for (int i = 0; i < 10; i++)
{
using (var client = context.CreateRequestSocket())
{
client.Connect("tcp://localhost:5555");

for (int i = 0; i < 10; i++)
{
Console.WriteLine("Sending Hello");
client.Send("Hello");

var message = client.ReceiveString();
Console.WriteLine("Received {0}", message);
}
}
Console.WriteLine("Sending Hello");
client.Send("Hello");

var message = client.ReceiveString();
Console.WriteLine("Received {0}", message);
}
}

Expand Down
96 changes: 45 additions & 51 deletions docs/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@ So if you have come here after looking at some of the introductory material, you

:::csharp
using (NetMQContext ctx = NetMQContext.Create())
using (var server = ctx.CreateResponseSocket())
using (var client = ctx.CreateRequestSocket())
{
using (var server = ctx.CreateResponseSocket())
{
server.Bind("tcp://127.0.0.1:5556");
using (var client = ctx.CreateRequestSocket())
{
client.Connect("tcp://127.0.0.1:5556");
server.Bind("tcp://127.0.0.1:5556");
client.Connect("tcp://127.0.0.1:5556");

client.Send("Hello");
client.Send("Hello");

string fromClientMessage = server.ReceiveString();
string fromClientMessage = server.ReceiveString();

Console.WriteLine("From Client: {0}", fromClientMessage);
Console.WriteLine("From Client: {0}", fromClientMessage);

server.Send("Hi Back");
server.Send("Hi Back");

string fromServerMessage = client.ReceiveString();
string fromServerMessage = client.ReceiveString();

Console.WriteLine("From Server: {0}", fromServerMessage);
Console.WriteLine("From Server: {0}", fromServerMessage);

Console.ReadLine();
}
}
Console.ReadLine();
}

Where you may have noticed (or perhaps not) that the NetMQ socket(s) have a `RecieveString()` method. This is good, and extremely useful, but you may be fooled into thinking this is what you should be using all the time.
Expand Down Expand Up @@ -142,45 +138,43 @@ Just to solidify this information here is a complete example showing everything

:::csharp
using (NetMQContext ctx = NetMQContext.Create())
using (var server = ctx.CreateResponseSocket())
using (var client = ctx.CreateRequestSocket())
{
using (var server = ctx.CreateResponseSocket())
using (var client = ctx.CreateRequestSocket())
// connect the sockets
server.Bind("tcp://127.0.0.1:5556");
client.Connect("tcp://127.0.0.1:5556");

// client sends message consisting of two frames
Console.WriteLine("Client sending")
client.SendMore("A").Send("Hello");

// server receives frames
bool more = true;
while (more)
{
// connect the sockets
server.Bind("tcp://127.0.0.1:5556");
client.Connect("tcp://127.0.0.1:5556");

// client sends message consisting of two frames
Console.WriteLine("Client sending")
client.SendMore("A").Send("Hello");

// server receives frames
bool more = true;
while (more)
{
string frame = server.ReceiveString(out more);
Console.WriteLine("Server received frame={0} more={1}",
frame, more);
}

Console.WriteLine("================================");

// server sends message, this time using NetMqMessage
var msg = new NetMQMessage();
msg.Append("From");
msg.Append("Server");

Console.WriteLine("Server sending");
server.SendMessage(msg);

// client receives the message
msg = client.ReceiveMessage();
Console.WriteLine("Client received {0} frames", msg.FrameCount)
foreach (var frame in msg)
Console.WriteLine("Frame={0}", frame.ConvertToString());

Console.ReadLine();
string frame = server.ReceiveString(out more);
Console.WriteLine("Server received frame={0} more={1}",
frame, more);
}

Console.WriteLine("================================");

// server sends message, this time using NetMqMessage
var msg = new NetMQMessage();
msg.Append("From");
msg.Append("Server");

Console.WriteLine("Server sending");
server.SendMessage(msg);

// client receives the message
msg = client.ReceiveMessage();
Console.WriteLine("Client received {0} frames", msg.FrameCount)
foreach (var frame in msg)
Console.WriteLine("Frame={0}", frame.ConvertToString());

Console.ReadLine();
}

Which when run will give you some output like this:
Expand Down
62 changes: 28 additions & 34 deletions docs/push-pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,42 @@ Here is what we are trying to achieve :
// Sends batch of tasks to workers via that socket
Console.WriteLine("====== VENTILATOR ======");

using (NetMQContext ctx = NetMQContext.Create())
using (var ctx = NetMQContext.Create())
using (var sender = ctx.CreatePushSocket())
using (var sink = ctx.CreatePushSocket())
{
//socket to send messages on
using (var sender = ctx.CreatePushSocket())
{
sender.Bind("tcp://*:5557");

using (var sink = ctx.CreatePushSocket())
{
sink.Connect("tcp://localhost:5558");
sender.Bind("tcp://*:5557");
sink.Connect("tcp://localhost:5558");

Console.WriteLine("Press enter when worker are ready");
Console.ReadLine();
Console.WriteLine("Press enter when worker are ready");
Console.ReadLine();

//the first message it "0" and signals start of batch
//see the Sink.csproj Program.cs file for where this is used
Console.WriteLine("Sending start of batch to Sink");
sink.Send("0");
//the first message it "0" and signals start of batch
//see the Sink.csproj Program.cs file for where this is used
Console.WriteLine("Sending start of batch to Sink");
sink.Send("0");

Console.WriteLine("Sending tasks to workers");
Console.WriteLine("Sending tasks to workers");

//initialise random number generator
Random rand = new Random(0);
//initialise random number generator
Random rand = new Random(0);

//expected costs in Ms
int totalMs = 0;
//expected costs in Ms
int totalMs = 0;

//send 100 tasks (workload for tasks, is just some random sleep time that
//the workers can perform, in real life each work would do more than sleep
for (int taskNumber = 0; taskNumber < 100; taskNumber++)
{
//Random workload from 1 to 100 msec
int workload = rand.Next(0, 100);
totalMs += workload;
Console.WriteLine("Workload : {0}", workload);
sender.Send(workload.ToString());
}
Console.WriteLine("Total expected cost : {0} msec", totalMs);
Console.WriteLine("Press Enter to quit");
Console.ReadLine();
}
//send 100 tasks (workload for tasks, is just some random sleep time that
//the workers can perform, in real life each work would do more than sleep
for (int taskNumber = 0; taskNumber < 100; taskNumber++)
{
//Random workload from 1 to 100 msec
int workload = rand.Next(0, 100);
totalMs += workload;
Console.WriteLine("Workload : {0}", workload);
sender.Send(workload.ToString());
}
Console.WriteLine("Total expected cost : {0} msec", totalMs);
Console.WriteLine("Press Enter to quit");
Console.ReadLine();
}
}
}
Expand Down

0 comments on commit 53e1674

Please sign in to comment.