Skip to content

Commit

Permalink
The story so far, doesn't compile, lots of problems still
Browse files Browse the repository at this point in the history
  • Loading branch information
tjrobinson committed Dec 21, 2012
1 parent ac706e5 commit 3f845d3
Show file tree
Hide file tree
Showing 68 changed files with 3,192 additions and 414 deletions.
32 changes: 32 additions & 0 deletions examples/C#/ZHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroMQ;

namespace zguide
{
public class ZHelpers
{
public static void Dump(ZmqSocket socket, Encoding encoding)
{
if (socket == null)
{
throw new ArgumentNullException("socket");
}

Console.WriteLine(new String('-', 38));
ZmqMessage message = socket.ReceiveMessage();
foreach (var frame in message)
{
Console.Write("[{0:d3}] ", frame.BufferSize);

if (frame.BufferSize == 5 && frame.Buffer[0] == 0)
Console.WriteLine("{0}", BitConverter.ToString(frame.Buffer).Replace("-", string.Empty));
else
Console.WriteLine("{0}", encoding.GetString(frame.Buffer));
}
}
}
}
39 changes: 20 additions & 19 deletions examples/C#/asyncsrv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// While this example runs in a single process, that is just to make
// it easier to start and stop the example. Each task has its own
// context and conceptually acts as a separate process.
// ZmqContext and conceptually acts as a separate process.

// Author: Michael Compton, Tomas Roos
// Email: michael.compton@littleedge.co.uk, ptomasroos@gmail.com
Expand All @@ -12,7 +12,8 @@
using System.Collections.Generic;
using System.Text;
using System.Threading;
using ZMQ;
using ZeroMQ;
using zguide;

namespace ZMQGuide
{
Expand Down Expand Up @@ -42,18 +43,18 @@ public static void Main(string[] args)
// run several client tasks in parallel, each with a different random ID.
public static void ClientTask()
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (Socket client = context.Socket(SocketType.DEALER))
using (ZmqSocket client = context.CreateSocket(SocketType.DEALER))
{
// Generate printable identity for the client
ZHelpers.SetID(client, Encoding.Unicode);
string identity = client.IdentityToString(Encoding.Unicode);
client.Connect("tcp://localhost:5570");

client.PollInHandler += (socket, revents) =>
client.PollInHandler += (ZmqSocket, revents) =>
{
var zmsg = new ZMessage(socket);
var zmsg = new ZMessage(ZmqSocket);
Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString());
};

Expand All @@ -64,7 +65,7 @@ public static void ClientTask()
// Tick once per second, pulling in arriving messages
for (int centitick = 0; centitick < 100; centitick++)
{
Context.Poller(new List<Socket>(new[] { client }), 10000);
ZmqContext.Poller(new List<ZmqSocket>(new[] { client }), 10000);
}
var zmsg = new ZMessage("");
zmsg.StringToBody(String.Format("request: {0}", ++requestNumber));
Expand All @@ -83,54 +84,54 @@ public static void ClientTask()
private static void ServerTask()
{
var workers = new List<Thread>(5);
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (Socket frontend = context.Socket(SocketType.ROUTER), backend = context.Socket(SocketType.DEALER))
using (ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER), backend = context.CreateSocket(SocketType.DEALER))
{
frontend.Bind("tcp://*:5570");
backend.Bind("inproc://backend");

for (int workerNumber = 0; workerNumber < 5; workerNumber++)
{
workers.Add(new Thread(ServerWorker));
workers[workerNumber].Start(context);
workers[workerNumber].Start(ZmqContext);
}

// Switch messages between frontend and backend
frontend.PollInHandler += (socket, revents) =>
frontend.PollInHandler += (ZmqSocket, revents) =>
{
var zmsg = new ZMessage(socket);
var zmsg = new ZMessage(ZmqSocket);
zmsg.Send(backend);
};

backend.PollInHandler += (socket, revents) =>
backend.PollInHandler += (ZmqSocket, revents) =>
{
var zmsg = new ZMessage(socket);
var zmsg = new ZMessage(ZmqSocket);
zmsg.Send(frontend);
};

var sockets = new List<Socket> {frontend, backend};
var sockets = new List<ZmqSocket> {frontend, backend};

while (true)
{
Context.Poller(sockets);
ZmqContext.Poller(sockets);
}
}
}
}

// Accept a request and reply with the same text a random number of
// times, with random delays between replies.
private static void ServerWorker(object context)
private static void ServerWorker(object ZmqContext)
{
var randomizer = new Random(DateTime.Now.Millisecond);
using (Socket worker = ((Context)context).Socket(SocketType.DEALER))
using (ZmqSocket worker = ((ZmqContext)ZmqContext).CreateSocket(SocketType.DEALER))
{
worker.Connect("inproc://backend");

while (true)
{
// The DEALER socket gives us the address envelope and message
// The DEALER ZmqSocket gives us the address envelope and message
var zmsg = new ZMessage(worker);
// Send 0..4 replies back
int replies = randomizer.Next(5);
Expand Down
10 changes: 5 additions & 5 deletions examples/C#/clonecli1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using ZMQ;
using ZeroMQ;

namespace ZMQGuide
{
internal class Program
internal class Program26
{
public static void Main(string[] args)
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (var subscriber = context.Socket(SocketType.SUB))
using (var subscriber = context.CreateSocket(SocketType.SUB))
{
subscriber.Connect("tcp://localhost:5556");
subscriber.Subscribe(string.Empty, Encoding.Unicode);
Expand All @@ -36,7 +36,7 @@ public static void Main(string[] args)

try
{
kvmsg = KvMsg.Recv(subscriber);
kvmsg = KvMsg.Receive(subscriber);
Console.WriteLine("Received {0}", kvmsg);
}
catch (System.Exception)
Expand Down
8 changes: 4 additions & 4 deletions examples/C#/clonesrv1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
using System;
using System.Collections.Generic;
using System.Threading;
using ZMQ;
using ZeroMQ;

namespace ZMQGuide
{
internal class Program
internal class Program29
{
public static void Main(string[] args)
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (var publisher = context.Socket(SocketType.PUB))
using (var publisher = ZmqContext.CreateSocket(SocketType.PUB))
{
publisher.Bind("tcp://*:5556");
Thread.Sleep(TimeSpan.FromMilliseconds(200));
Expand Down
12 changes: 6 additions & 6 deletions examples/C#/hwclient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Connects REQ ZmqSocket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//

Expand All @@ -9,17 +9,17 @@

using System;
using System.Text;
using ZMQ;
using ZeroMQ;

namespace ZMQGuide
{
internal class Program
internal class Program25
{
public static void Main(string[] args)
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (Socket requester = context.Socket(SocketType.REQ))
using (ZmqSocket requester = context.CreateSocket(SocketType.REQ))
{
requester.Connect("tcp://localhost:5555");

Expand All @@ -31,7 +31,7 @@ public static void Main(string[] args)
Console.WriteLine("Sending request {0}...", requestNumber);
requester.Send(requestMessage, Encoding.Unicode);

string reply = requester.Recv(Encoding.Unicode);
string reply = requester.Receive(Encoding.Unicode);
Console.WriteLine("Received reply {0}: {1}", requestNumber, reply);
}
}
Expand Down
12 changes: 6 additions & 6 deletions examples/C#/hwclient.cs.v1
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace Examples
// allocate a buffer
byte[] zmq_buffer = new byte[1024];

// Prepare our context and socket
ZMQ.Context context = new ZMQ.Context(1);
ZMQ.Socket socket = context.Socket(ZMQ.REQ);
socket.Connect("tcp://localhost:5555");
// Prepare our ZmqContext and ZmqSocket
ZMQ.ZmqContext ZmqContext = new ZMQ.ZmqContext(1);
ZMQ.ZmqSocket ZmqSocket = context.CreateSocket(ZMQ.REQ);
ZmqSocket.Connect("tcp://localhost:5555");

string request = "";
for (long requestNum = 0; requestNum != 10; requestNum++)
{
socket.Send(Encoding.ASCII.GetBytes("Hello".ToCharArray()));
ZmqSocket.Send(Encoding.ASCII.GetBytes("Hello".ToCharArray()));
// Wait for next request from client
socket.Recv(out zmq_buffer);
ZmqSocket.Receive(out zmq_buffer);
request = Encoding.ASCII.GetString(zmq_buffer);
}
}
Expand Down
12 changes: 6 additions & 6 deletions examples/C#/hwserver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Hello World server
// Binds REP socket to tcp://*:5555
// Binds REP ZmqSocket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//

Expand All @@ -10,25 +10,25 @@
using System;
using System.Text;
using System.Threading;
using ZMQ;
using ZeroMQ;

namespace ZMQGuide
{
internal class Program
internal class Program28
{
public static void Main(string[] args)
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (Socket replyer = context.Socket(SocketType.REP))
using (ZmqSocket replyer = context.CreateSocket(SocketType.REP))
{
replyer.Bind("tcp://*:5555");

const string replyMessage = "World";

while (true)
{
string message = replyer.Recv(Encoding.Unicode);
string message = replyer.Receive(Encoding.Unicode);
Console.WriteLine("Received request: {0}", message);

// Simulate work, by sleeping
Expand Down
12 changes: 6 additions & 6 deletions examples/C#/hwserver.cs.v1
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace Examples {
// allocate a buffer
byte[] zmq_buffer = new byte[1024];

// Prepare our context and socket
ZMQ.Context context = new ZMQ.Context(1);
ZMQ.Socket socket = context.Socket(ZMQ.REP);
socket.Bind("tcp://*:5555");
// Prepare our ZmqContext and ZmqSocket
ZMQ.ZmqContext ZmqContext = new ZMQ.ZmqContext(1);
ZMQ.ZmqSocket ZmqSocket = context.CreateSocket(ZMQ.REP);
ZmqSocket.Bind("tcp://*:5555");

while (true) {
try {
// Wait for next request from client
socket.Recv(out zmq_buffer);
ZmqSocket.Receive(out zmq_buffer);
string request = Encoding.ASCII.GetString(zmq_buffer);

// log that we got one
Expand All @@ -33,7 +33,7 @@ namespace Examples {
Thread.Sleep(1);

// Send reply back to client
socket.Send(Encoding.ASCII.GetBytes("World".ToCharArray()));
ZmqSocket.Send(Encoding.ASCII.GetBytes("World".ToCharArray()));

} catch (ZMQ.Exception z) {
// report the exception
Expand Down
11 changes: 6 additions & 5 deletions examples/C#/identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
// Email: michael.compton@littleedge.co.uk, ptomasroos@gmail.com

using System.Text;
using ZMQ;
using ZeroMQ;
using zguide;

namespace ZMQGuide
{
internal class Program
internal class Program3
{
public static void Main(string[] args)
{
using (var context = new Context())
using (var context = ZmqContext.Create())
{
using (Socket sink = context.Socket(SocketType.ROUTER), anonymous = context.Socket(SocketType.REQ), identified = context.Socket(SocketType.REQ))
using (ZmqSocket sink = context.CreateSocket(SocketType.ROUTER), anonymous = context.CreateSocket(SocketType.REQ), identified = context.CreateSocket(SocketType.REQ))
{
sink.Bind("inproc://example");

Expand All @@ -28,7 +29,7 @@ public static void Main(string[] args)
// Then set the identity ourself
identified.StringToIdentity("Hello", Encoding.Unicode);
identified.Connect("inproc://example");
identified.Send("ROUTER socket uses REQ's socket identity", Encoding.Unicode);
identified.Send("ROUTER ZmqSocket uses REQ's ZmqSocket identity", Encoding.Unicode);
ZHelpers.Dump(sink, Encoding.Unicode);
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/C#/interrupt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
using System;
using System.Text;
using System.Threading;
using ZMQ;
using ZeroMQ;

namespace ZMQGuide
{
internal class Program
internal class Program12
{

public static void Main(string[] args)
{
using (var context = new Context(1))
using (var context = ZmqContext.Create())
{
using (Socket replyer = context.Socket(SocketType.REP))
using (ZmqSocket replyer = context.CreateSocket(SocketType.REP))
{
replyer.Bind("tcp://*:5555");

Expand All @@ -31,7 +31,7 @@ public static void Main(string[] args)

while (!interrupted)
{
string message = replyer.Recv(Encoding.Unicode);
string message = replyer.Receive(Encoding.Unicode);
Console.WriteLine("Received request: {0}", message);

// Simulate work, by sleeping
Expand Down
Loading

0 comments on commit 3f845d3

Please sign in to comment.