Skip to content

Commit

Permalink
Fixes to the text
Browse files Browse the repository at this point in the history
  • Loading branch information
hintjens committed Oct 12, 2010
1 parent 7eb05bf commit 8a9663c
Show file tree
Hide file tree
Showing 83 changed files with 1,578 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The text of "ØMQ - The Guide" is copyright (c) 2010 Pieter Hintjens, and is lic
Thanks
------

Thanks to Bill Desmarais, Brian Dorsey, CAF, Daniel Lin, Eric Desgranges, Gonzalo Diethelm, Guido Goldstein, Hunter Ford, Kamil Shakirov, Martin Sustrik, Mike Castleman, Naveen Chawla, Nicola Peduzzi, Oliver Smith, Olivier Chamoux, Peter Alexander, Pierre Rouleau, Randy Dryburgh, and Zed Shaw for their contributions. Thanks to Stathis Sideris for Ditaa.
Thanks to Bill Desmarais, Brian Dorsey, CAF, Daniel Lin, Eric Desgranges, Gonzalo Diethelm, Guido Goldstein, Hunter Ford, Kamil Shakirov, Martin Sustrik, Mike Castleman, Naveen Chawla, Nicola Peduzzi, Oliver Smith, Olivier Chamoux, Peter Alexander, Pierre Rouleau, Randy Dryburgh, and Zed Shaw for their contributions, and to Stathis Sideris for [Ditaa](ditaa.org).

<A name="toc2-52" title="General" />
General
Expand Down
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The text of "0MQ - The Guide" is copyright (c) 2010 Pieter Hintjens, and is lice
Thanks
------

Thanks to Bill Desmarais, Brian Dorsey, CAF, Daniel Lin, Eric Desgranges, Gonzalo Diethelm, Guido Goldstein, Hunter Ford, Kamil Shakirov, Martin Sustrik, Mike Castleman, Naveen Chawla, Nicola Peduzzi, Oliver Smith, Olivier Chamoux, Peter Alexander, Pierre Rouleau, Randy Dryburgh, and Zed Shaw for their contributions. Thanks to Stathis Sideris for Ditaa.
Thanks to Bill Desmarais, Brian Dorsey, CAF, Daniel Lin, Eric Desgranges, Gonzalo Diethelm, Guido Goldstein, Hunter Ford, Kamil Shakirov, Martin Sustrik, Mike Castleman, Naveen Chawla, Nicola Peduzzi, Oliver Smith, Olivier Chamoux, Peter Alexander, Pierre Rouleau, Randy Dryburgh, and Zed Shaw for their contributions, and to Stathis Sideris for [Ditaa](ditaa.org).

General
-------
Expand Down
Binary file added articles/images/multithreading_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/multithreading_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
183 changes: 183 additions & 0 deletions articles/multithreading.md

Large diffs are not rendered by default.

349 changes: 349 additions & 0 deletions articles/multithreading.txt

Large diffs are not rendered by default.

Binary file added articles/src/multithreading/core
Binary file not shown.
5 changes: 5 additions & 0 deletions articles/src/multithreading/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "zmsg.c"
void main (void)
{
s_version ();
}
Binary file added articles/src/multithreading/testit
Binary file not shown.
19 changes: 19 additions & 0 deletions articles/src/multithreading/testit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Show inter-thread signalling using 0MQ sockets
//
#include "zmsg.c"

int main () {
void *context = zmq_init (1);
void *socket = zmq_socket (context, ZMQ_PUB);
s_version ();

assert (zmq_bind (socket, "ipc://signal") == 0);
zmsg_t *zmsg = zmsg_new ();
zmsg_body_set (zmsg, "happy");
zmsg_send (&zmsg, socket);

sleep (1);
zmq_term (context);
return 0;
}
Binary file added articles/src/multithreading/testit.o
Binary file not shown.
Binary file added articles/src/multithreading/threads
Binary file not shown.
45 changes: 45 additions & 0 deletions articles/src/multithreading/threads.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Show inter-thread signalling using 0MQ sockets
//
#include "zhelpers.h"

static void *
child_thread (void *context)
{
void *socket = zmq_socket (context, ZMQ_PAIR);
assert (zmq_connect (socket, "inproc://sink") == 0);

s_send (socket, "happy");
s_send (socket, "sad");
s_send (socket, "done");

zmq_close (socket);
return (NULL);
}

int main ()
{
s_version ();
// Threads communicate via shared context
void *context = zmq_init (1);

// Create sink socket, bind to inproc endpoint
void *socket = zmq_socket (context, ZMQ_PAIR);
assert (zmq_bind (socket, "inproc://sink") == 0);

// Start child thread
pthread_t thread;
pthread_create (&thread, NULL, child_thread, context);

// Get messages from child thread
while (1) {
char *mood = s_recv (socket);
printf ("You're %s\n", mood);
if (strcmp (mood, "done") == 0)
break;
free (mood);
}
zmq_close (socket);
zmq_term (context);
return 0;
}
Binary file added articles/src/multithreading/threads.o
Binary file not shown.
152 changes: 152 additions & 0 deletions articles/src/multithreading/zhelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* =========================================================================
zhelpers.h
Helper header file for example applications.
-------------------------------------------------------------------------
Copyright (c) 1991-2010 iMatix Corporation <www.imatix.com>
Copyright other contributors as noted in the AUTHORS file.
This file is part of the ZeroMQ Guide: http://zguide.zeromq.org
This is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABIL-
ITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=========================================================================
*/

#ifndef __ZHELPERS_H_INCLUDED__
#define __ZHELPERS_H_INCLUDED__

// Include a bunch of headers that we will need in the examples

#include <zmq.h>

#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>

// Provide random number from 0..(num-1)
#define within(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))

// Receive 0MQ string from socket and convert into C string
// Caller must free returned string.
static char *
s_recv (void *socket) {
zmq_msg_t message;
zmq_msg_init (&message);
if (zmq_recv (socket, &message, 0))
exit (1); // Context terminated, exit

int size = zmq_msg_size (&message);
char *string = malloc (size + 1);
memcpy (string, zmq_msg_data (&message), size);
zmq_msg_close (&message);
string [size] = 0;
return (string);
}

// Convert C string to 0MQ string and send to socket
static int
s_send (void *socket, char *string) {
int rc;
zmq_msg_t message;
zmq_msg_init_size (&message, strlen (string));
memcpy (zmq_msg_data (&message), string, strlen (string));
rc = zmq_send (socket, &message, 0);
assert (!rc);
zmq_msg_close (&message);
return (rc);
}

// Sends string as 0MQ string, as multipart non-terminal
static int
s_sendmore (void *socket, char *string) {
int rc;
zmq_msg_t message;
zmq_msg_init_size (&message, strlen (string));
memcpy (zmq_msg_data (&message), string, strlen (string));
rc = zmq_send (socket, &message, ZMQ_SNDMORE);
zmq_msg_close (&message);
assert (!rc);
return (rc);
}

// Receives all message parts from socket, prints neatly
//
static void
s_dump (void *socket)
{
puts ("----------------------------------------");
while (1) {
// Process all parts of the message
zmq_msg_t message;
zmq_msg_init (&message);
zmq_recv (socket, &message, 0);

// Dump the message as text or binary
char *data = zmq_msg_data (&message);
int size = zmq_msg_size (&message);
int is_text = 1;
int char_nbr;
for (char_nbr = 0; char_nbr < size; char_nbr++)
if ((unsigned char) data [char_nbr] < 32
|| (unsigned char) data [char_nbr] > 127)
is_text = 0;

printf ("[%03d] ", size);
for (char_nbr = 0; char_nbr < size; char_nbr++) {
if (is_text)
printf ("%c", data [char_nbr]);
else
printf ("%02X", (unsigned char) data [char_nbr]);
}
printf ("\n");

int64_t more; // Multipart detection
size_t more_size = sizeof (more);
zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
zmq_msg_close (&message);
if (!more)
break; // Last message part
}
}

// Set simple random printable identity on socket
//
static void
s_set_id (void *socket)
{
char identity [10];
sprintf (identity, "%04X-%04X", within (0x10000), within (0x10000));
zmq_setsockopt (socket, ZMQ_IDENTITY, identity, strlen (identity));
}


// Report 0MQ version number
//
static void
s_version (void)
{
int major, minor, patch;
zmq_version (&major, &minor, &patch);
printf ("Current 0MQ version is %d.%d.%d\n", major, minor, patch);
}

#endif
Loading

0 comments on commit 8a9663c

Please sign in to comment.