http://www.huffingtonpost.com/nadya-khoja/6-top-infographic-design-_b_11707120.html
https://en.wikipedia.org/wiki/Infographic
Friday, August 26, 2016
netstat No -p option
Get inode and port relationship
Find out inode and process relationship
#cat /proc/net/tcp
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 2495 1 ce037b40 100 0 0 10 -1
1: 0100007F:177A 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 2807 1 ce036940 100 0 0 10 -1
2: 0100007F:177B 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 3244 1 ce036dc0 100 0 0 10 -1
3: 652910AC:0016 A72810AC:CFF8 01 00000034:00000000 01:00000026 00000000 0 0 2793 3 ce036040 39 4 11 10 -1
4: 652910AC:0016 A72810AC:CFF9 01 00000000:00000000 00:00000000 00000000 0 0 2801 1 ce0364c0 24 4 25 10 -1
5: 652910AC:0016 A72810AC:D03A 01 00000000:00000000 00:00000000 00000000 0 0 3239 1 ce037240 24 4 25 10 -1
6: 652910AC:0016 A72810AC:D039 01 00000000:00000000 00:00000000 00000000 0 0 3231 1 ce0376c0 25 4 1 10 -1
Find out inode and process relationship
#find /proc/*/fd | xargs ls -l | grep socket | grep proc
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1040/fd/4 -> socket:[2258]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1040/fd/7 -> socket:[2261]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1040/fd/8 -> socket:[2262]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1049/fd/3 -> socket:[2300]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1052/fd/3 -> socket:[2303]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1100/fd/10 -> socket:[2354]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1100/fd/9 -> socket:[2024]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1101/fd/10 -> socket:[2355]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1101/fd/9 -> socket:[2024]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1258/fd/3 -> socket:[2490]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1258/fd/4 -> socket:[2495]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1352/fd/5 -> socket:[2708]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1399/fd/3 -> socket:[2490]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1399/fd/5 -> socket:[2793]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1399/fd/7 -> socket:[2807]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1400/fd/3 -> socket:[2490]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/1400/fd/5 -> socket:[2801]
lrwx------ 1 root root 64 Aug 26 12:49 /proc/1575/fd/3 -> socket:[2490]
lrwx------ 1 root root 64 Aug 26 12:49 /proc/1575/fd/5 -> socket:[3231]
lrwx------ 1 root root 64 Aug 26 12:49 /proc/1575/fd/7 -> socket:[3244]
lrwx------ 1 root root 64 Aug 26 12:49 /proc/1576/fd/3 -> socket:[2490]
lrwx------ 1 root root 64 Aug 26 12:49 /proc/1576/fd/5 -> socket:[3239]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/931/fd/4 -> socket:[1994]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/931/fd/5 -> socket:[1995]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/931/fd/8 -> socket:[2023]
lrwx------ 1 root root 64 Aug 26 12:44 /proc/931/fd/9 -> socket:[2024]
Find out PID of a process#ps -axl | grep 1400
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
1 0 1400 1258 20 0 2440 1208 poll_s Ss ? 0:00 dropbear
Monday, August 22, 2016
Quick note on zeromq cross complie and deployment
./configure --host=arm-linux-gnueabihf --prefix=/home/xxx/temp/arm
export LDFLAGS="-L/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/lib -L/home/xxx/temp/arm/lib"
export CPPFLAGS="-I/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/arm-linux-gnueabihf/include/c++/4.7.3/"
add path to cross compiler
export PATH=$PATH:/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/bin
$ make
$ make install
at this moment, the libzmq is available at /home/xxx/temp/arm/lib
$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuclient.c -o wuclient
$ cat wuclient.c
// Weather update client
// Connects SUB socket to tcp://localhost:5556
// Collects weather updates and finds avg temp in zipcode
#include "zhelpers.h"
int main (int argc, char *argv [])
{
// Socket to talk to server
printf ("Collecting updates from weather server...\n");
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
int rc = zmq_connect (subscriber, "tcp://127.0.0.1:5556");
assert (rc == 0);
// Subscribe to zipcode, default is NYC, 10001
char *filter = (argc > 1)? argv [1]: "10001 ";
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
filter, strlen (filter));
assert (rc == 0);
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
char *string = s_recv (subscriber);
printf("%s\n", string);
int zipcode, temperature, relhumidity;
sscanf (string, "%d %d %d",
&zipcode, &temperature, &relhumidity);
total_temp += temperature;
free (string);
}
printf ("Average temperature for zipcode '%s' was %dF\n",
filter, (int) (total_temp / update_nbr));
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}
$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuserver.c -o wuserver
$ cat wuserver.c
// Weather update server
// Binds PUB socket to tcp://*:5556
// Publishes random weather updates
#include "zhelpers.h"
int main (void)
{
// Prepare our context and publisher
void *context = zmq_ctx_new ();
void *publisher = zmq_socket (context, ZMQ_PUB);
int rc = zmq_bind (publisher, "tcp://lo:5556");
assert (rc == 0);
// Initialize random number generator
srandom ((unsigned) time (NULL));
while (1) {
// Get values that will fool the boss
int zipcode, temperature, relhumidity;
zipcode = randof (100000);
temperature = randof (215) - 80;
relhumidity = randof (50) + 10;
// Send message to all subscribers
char update [20];
sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
s_send (publisher, update);
}
zmq_close (publisher);
zmq_ctx_destroy (context);
return 0;
}
$ cat zhelpers.h
/* =====================================================================
zhelpers.h
Helper header file for example applications.
=====================================================================
*/
#ifndef __ZHELPERS_H_INCLUDED__
#define __ZHELPERS_H_INCLUDED__
// Include a bunch of headers that we will need in the examples
#include <zmq.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if (!defined (WIN32))
# include <sys/time.h>
#endif
#if (defined (WIN32))
# include <windows.h>
#endif
// Version checking, and patch up missing constants to match 2.1
#if ZMQ_VERSION_MAJOR == 2
# error "Please upgrade to ZeroMQ/3.2 for these examples"
#endif
// On some version of Windows, POSIX subsystem is not installed by default.
// So define srandom and random ourself.
#if (defined (WIN32))
# define srandom srand
# define random rand
#endif
// Provide random number from 0..(num-1)
#define randof(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
// Receive 0MQ string from socket and convert into C string
// Caller must free returned string. Returns NULL if the context
// is being terminated.
static char *
s_recv (void *socket) {
char buffer [256];
int size = zmq_recv (socket, buffer, 255, 0);
if (size == -1)
return NULL;
return strndup (buffer, sizeof(buffer) - 1);
// remember that the strdup family of functions use malloc/alloc for space for the new string. It must be manually
// freed when you are done with it. Failure to do so will allow a heap attack.
}
// Convert C string to 0MQ string and send to socket
static int
s_send (void *socket, char *string) {
int size = zmq_send (socket, string, strlen (string), 0);
return size;
}
// Sends string as 0MQ string, as multipart non-terminal
static int
s_sendmore (void *socket, char *string) {
int size = zmq_send (socket, string, strlen (string), ZMQ_SNDMORE);
return size;
}
// Receives all message parts from socket, prints neatly
//
static void
s_dump (void *socket)
{
int rc;
zmq_msg_t message;
rc = zmq_msg_init (&message);
assert (rc == 0);
puts ("----------------------------------------");
// Process all parts of the message
do {
int size = zmq_msg_recv (&message, socket, 0);
assert (size >= 0);
// Dump the message as text or binary
char *data = (char*)zmq_msg_data (&message);
assert (data != 0);
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] > 126) {
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");
} while (zmq_msg_more (&message));
rc = zmq_msg_close (&message);
assert (rc == 0);
}
#if (!defined (WIN32))
// Set simple random printable identity on socket
// Caution:
// DO NOT call this version of s_set_id from multiple threads on MS Windows
// since s_set_id will call rand() on MS Windows. rand(), however, is not
// reentrant or thread-safe. See issue #521.
static void
s_set_id (void *socket)
{
char identity [10];
sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
zmq_setsockopt (socket, ZMQ_IDENTITY, identity, strlen (identity));
}
#else
// Fix #521 for MS Windows.
static void
s_set_id(void *socket, intptr_t id)
{
char identity [10];
sprintf(identity, "%04X", (int)id);
zmq_setsockopt(socket, ZMQ_IDENTITY, identity, strlen(identity));
}
#endif
// Sleep for a number of milliseconds
static void
s_sleep (int msecs)
{
#if (defined (WIN32))
Sleep (msecs);
#else
struct timespec t;
t.tv_sec = msecs / 1000;
t.tv_nsec = (msecs % 1000) * 1000000;
nanosleep (&t, NULL);
#endif
}
// Return current system clock as milliseconds
static int64_t
s_clock (void)
{
#if (defined (WIN32))
SYSTEMTIME st;
GetSystemTime (&st);
return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}
// Print formatted string to stdout, prefixed by date/time and
// terminated with a newline.
static void
s_console (const char *format, ...)
{
time_t curtime = time (NULL);
struct tm *loctime = localtime (&curtime);
char *formatted = (char*)malloc (20);
strftime (formatted, 20, "%y-%m-%d %H:%M:%S ", loctime);
printf ("%s", formatted);
free (formatted);
va_list argptr;
va_start (argptr, format);
vprintf (format, argptr);
va_end (argptr);
printf ("\n");
}
#endif // __ZHELPERS_H_INCLUDED__
package wuclient wuserver and /home/xxx/temp/arm/*
deploy them to an arm target
after extract everything,
export LD_LIBRARY_PATH=/xxx/lib
wuserver
wuclient
export LDFLAGS="-L/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/lib -L/home/xxx/temp/arm/lib"
export CPPFLAGS="-I/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/arm-linux-gnueabihf/include/c++/4.7.3/"
add path to cross compiler
export PATH=$PATH:/opt/ti-sdk6.0/linux-devkit/sysroots/i686-arago-linux/usr/bin
$ make
$ make install
at this moment, the libzmq is available at /home/xxx/temp/arm/lib
$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuclient.c -o wuclient
$ cat wuclient.c
// Weather update client
// Connects SUB socket to tcp://localhost:5556
// Collects weather updates and finds avg temp in zipcode
#include "zhelpers.h"
int main (int argc, char *argv [])
{
// Socket to talk to server
printf ("Collecting updates from weather server...\n");
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
int rc = zmq_connect (subscriber, "tcp://127.0.0.1:5556");
assert (rc == 0);
// Subscribe to zipcode, default is NYC, 10001
char *filter = (argc > 1)? argv [1]: "10001 ";
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
filter, strlen (filter));
assert (rc == 0);
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
char *string = s_recv (subscriber);
printf("%s\n", string);
int zipcode, temperature, relhumidity;
sscanf (string, "%d %d %d",
&zipcode, &temperature, &relhumidity);
total_temp += temperature;
free (string);
}
printf ("Average temperature for zipcode '%s' was %dF\n",
filter, (int) (total_temp / update_nbr));
zmq_close (subscriber);
zmq_ctx_destroy (context);
return 0;
}
$ arm-linux-gnueabihf-gcc $LDFLAGS $CPPFLAGS -lzmq wuserver.c -o wuserver
$ cat wuserver.c
// Weather update server
// Binds PUB socket to tcp://*:5556
// Publishes random weather updates
#include "zhelpers.h"
int main (void)
{
// Prepare our context and publisher
void *context = zmq_ctx_new ();
void *publisher = zmq_socket (context, ZMQ_PUB);
int rc = zmq_bind (publisher, "tcp://lo:5556");
assert (rc == 0);
// Initialize random number generator
srandom ((unsigned) time (NULL));
while (1) {
// Get values that will fool the boss
int zipcode, temperature, relhumidity;
zipcode = randof (100000);
temperature = randof (215) - 80;
relhumidity = randof (50) + 10;
// Send message to all subscribers
char update [20];
sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
s_send (publisher, update);
}
zmq_close (publisher);
zmq_ctx_destroy (context);
return 0;
}
$ cat zhelpers.h
/* =====================================================================
zhelpers.h
Helper header file for example applications.
=====================================================================
*/
#ifndef __ZHELPERS_H_INCLUDED__
#define __ZHELPERS_H_INCLUDED__
// Include a bunch of headers that we will need in the examples
#include <zmq.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if (!defined (WIN32))
# include <sys/time.h>
#endif
#if (defined (WIN32))
# include <windows.h>
#endif
// Version checking, and patch up missing constants to match 2.1
#if ZMQ_VERSION_MAJOR == 2
# error "Please upgrade to ZeroMQ/3.2 for these examples"
#endif
// On some version of Windows, POSIX subsystem is not installed by default.
// So define srandom and random ourself.
#if (defined (WIN32))
# define srandom srand
# define random rand
#endif
// Provide random number from 0..(num-1)
#define randof(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
// Receive 0MQ string from socket and convert into C string
// Caller must free returned string. Returns NULL if the context
// is being terminated.
static char *
s_recv (void *socket) {
char buffer [256];
int size = zmq_recv (socket, buffer, 255, 0);
if (size == -1)
return NULL;
return strndup (buffer, sizeof(buffer) - 1);
// remember that the strdup family of functions use malloc/alloc for space for the new string. It must be manually
// freed when you are done with it. Failure to do so will allow a heap attack.
}
// Convert C string to 0MQ string and send to socket
static int
s_send (void *socket, char *string) {
int size = zmq_send (socket, string, strlen (string), 0);
return size;
}
// Sends string as 0MQ string, as multipart non-terminal
static int
s_sendmore (void *socket, char *string) {
int size = zmq_send (socket, string, strlen (string), ZMQ_SNDMORE);
return size;
}
// Receives all message parts from socket, prints neatly
//
static void
s_dump (void *socket)
{
int rc;
zmq_msg_t message;
rc = zmq_msg_init (&message);
assert (rc == 0);
puts ("----------------------------------------");
// Process all parts of the message
do {
int size = zmq_msg_recv (&message, socket, 0);
assert (size >= 0);
// Dump the message as text or binary
char *data = (char*)zmq_msg_data (&message);
assert (data != 0);
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] > 126) {
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");
} while (zmq_msg_more (&message));
rc = zmq_msg_close (&message);
assert (rc == 0);
}
#if (!defined (WIN32))
// Set simple random printable identity on socket
// Caution:
// DO NOT call this version of s_set_id from multiple threads on MS Windows
// since s_set_id will call rand() on MS Windows. rand(), however, is not
// reentrant or thread-safe. See issue #521.
static void
s_set_id (void *socket)
{
char identity [10];
sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
zmq_setsockopt (socket, ZMQ_IDENTITY, identity, strlen (identity));
}
#else
// Fix #521 for MS Windows.
static void
s_set_id(void *socket, intptr_t id)
{
char identity [10];
sprintf(identity, "%04X", (int)id);
zmq_setsockopt(socket, ZMQ_IDENTITY, identity, strlen(identity));
}
#endif
// Sleep for a number of milliseconds
static void
s_sleep (int msecs)
{
#if (defined (WIN32))
Sleep (msecs);
#else
struct timespec t;
t.tv_sec = msecs / 1000;
t.tv_nsec = (msecs % 1000) * 1000000;
nanosleep (&t, NULL);
#endif
}
// Return current system clock as milliseconds
static int64_t
s_clock (void)
{
#if (defined (WIN32))
SYSTEMTIME st;
GetSystemTime (&st);
return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}
// Print formatted string to stdout, prefixed by date/time and
// terminated with a newline.
static void
s_console (const char *format, ...)
{
time_t curtime = time (NULL);
struct tm *loctime = localtime (&curtime);
char *formatted = (char*)malloc (20);
strftime (formatted, 20, "%y-%m-%d %H:%M:%S ", loctime);
printf ("%s", formatted);
free (formatted);
va_list argptr;
va_start (argptr, format);
vprintf (format, argptr);
va_end (argptr);
printf ("\n");
}
#endif // __ZHELPERS_H_INCLUDED__
package wuclient wuserver and /home/xxx/temp/arm/*
deploy them to an arm target
after extract everything,
export LD_LIBRARY_PATH=/xxx/lib
wuserver
wuclient
a simple timer for linux applications
/*
* setitimer.c - simple use of the interval timer
*/
#include <sys/time.h> /* for setitimer */
#include <unistd.h> /* for pause */
#include <signal.h> /* for signal */
#define INTERVAL 500 /* number of milliseconds to go off */
/* function prototype */
void DoStuff(void);
int main(int argc, char *argv[]) {
struct itimerval it_val; /* for setting itimer */
/* Upon SIGALRM, call DoStuff().
* Set interval timer. We want frequency in ms,
* but the setitimer call needs seconds and useconds. */
if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) {
perror("Unable to catch SIGALRM");
exit(1);
}
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("error calling setitimer()");
exit(1);
}
while (1)
pause();
}
/*
* DoStuff
*/
void DoStuff(void) {
printf("Timer went off.\n");
}
* setitimer.c - simple use of the interval timer
*/
#include <sys/time.h> /* for setitimer */
#include <unistd.h> /* for pause */
#include <signal.h> /* for signal */
#define INTERVAL 500 /* number of milliseconds to go off */
/* function prototype */
void DoStuff(void);
int main(int argc, char *argv[]) {
struct itimerval it_val; /* for setting itimer */
/* Upon SIGALRM, call DoStuff().
* Set interval timer. We want frequency in ms,
* but the setitimer call needs seconds and useconds. */
if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) {
perror("Unable to catch SIGALRM");
exit(1);
}
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("error calling setitimer()");
exit(1);
}
while (1)
pause();
}
/*
* DoStuff
*/
void DoStuff(void) {
printf("Timer went off.\n");
}
Tuesday, August 16, 2016
pyzmq 15.4 and python 3.5.2
It takes a little time to get the latest pyzmq installed. So far, here is a list of things that I have done.
Note: This is on Ubuntu 16.04.
Preparation
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo python3 -m pip install cython
Download zeromq 4.1.5
http://zeromq.org/intro:get-the-software
Build zeromq 4.1.5
tar xf zeromq-4.1.5.tar.gzcd zeromq-4.1.5
./configure
make
sudo make install
Download pyzmq 15.4
https://github.com/zeromq/pyzmq
unzip pyzmq.zip
python3 setup.py configure --zmq=/usr/local
sudo python3 setup.py install
===============================
pub sub sample
python3 topics_sub.py "tcp://127.0.0.1:5556"
python3 topics_pub.py "tcp://127.0.0.1:5556" ""
===============================
topics_pub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.
Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost. More than
one subscriber can listen, and they can listen to different topics.
Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks' while listening to 'w' is enough to
catch 'weather'.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Brian Granger
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
import itertools
import sys
import time
import zmq
def main():
if len (sys.argv) != 2:
print('usage: publisher <bind-to>')
sys.exit (1)
bind_to = sys.argv[1]
all_topics = ['sports.general','sports.football','sports.basketball',
'stocks.general','stocks.GOOG','stocks.AAPL',
'weather']
ctx = zmq.Context()
s = ctx.socket(zmq.PUB)
s.bind(bind_to)
print("Starting broadcast on topics:")
print(" %s" % all_topics)
print("Hit Ctrl-C to stop broadcasting.")
print("Waiting so subscriber sockets can connect...")
print()
time.sleep(1.0)
msg_counter = itertools.count()
try:
for topic in itertools.cycle(all_topics):
msg_body = str(next(msg_counter))
print(" Topic: {}, msg:{}".format(topic, msg_body))
s.send_multipart([topic.encode(), msg_body.encode()])
# short wait so we don't hog the cpu
time.sleep(0.1)
except KeyboardInterrupt:
pass
print("Waiting for message queues to flush...")
time.sleep(0.5)
print("Done.")
if __name__ == "__main__":
main()
topics_sub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.
Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost. More than
one subscriber can listen, and they can listen to different topics.
Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks' while listening to 'w' is enough to
catch 'weather'.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Brian Granger, Fernando Perez
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
import sys
import time
import zmq
import numpy
def main():
if len (sys.argv) < 2:
print ('usage: subscriber <connect_to> [topic topic ...]')
sys.exit (1)
connect_to = sys.argv[1]
topics = sys.argv[2:]
ctx = zmq.Context()
s = ctx.socket(zmq.SUB)
print(connect_to)
s.connect(connect_to)
# manage subscriptions
if not topics:
print("Receiving messages on ALL topics...")
s.setsockopt(zmq.SUBSCRIBE,'')
else:
print("Receiving messages on topics: %s ..." % topics)
for t in topics:
s.setsockopt_string(zmq.SUBSCRIBE,t)
print()
try:
while True:
topic, msg = s.recv_multipart()
print(' Topic: %s, msg:%s' % (topic, msg))
except KeyboardInterrupt:
pass
print("Done.")
if __name__ == "__main__":
main()
Note: This is on Ubuntu 16.04.
Preparation
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo python3 -m pip install cython
Download zeromq 4.1.5
http://zeromq.org/intro:get-the-software
Build zeromq 4.1.5
tar xf zeromq-4.1.5.tar.gzcd zeromq-4.1.5
./configure
make
sudo make install
Download pyzmq 15.4
https://github.com/zeromq/pyzmq
unzip pyzmq.zip
python3 setup.py configure --zmq=/usr/local
sudo python3 setup.py install
===============================
pub sub sample
python3 topics_sub.py "tcp://127.0.0.1:5556"
python3 topics_pub.py "tcp://127.0.0.1:5556" ""
===============================
topics_pub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.
Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost. More than
one subscriber can listen, and they can listen to different topics.
Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks' while listening to 'w' is enough to
catch 'weather'.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Brian Granger
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
import itertools
import sys
import time
import zmq
def main():
if len (sys.argv) != 2:
print('usage: publisher <bind-to>')
sys.exit (1)
bind_to = sys.argv[1]
all_topics = ['sports.general','sports.football','sports.basketball',
'stocks.general','stocks.GOOG','stocks.AAPL',
'weather']
ctx = zmq.Context()
s = ctx.socket(zmq.PUB)
s.bind(bind_to)
print("Starting broadcast on topics:")
print(" %s" % all_topics)
print("Hit Ctrl-C to stop broadcasting.")
print("Waiting so subscriber sockets can connect...")
print()
time.sleep(1.0)
msg_counter = itertools.count()
try:
for topic in itertools.cycle(all_topics):
msg_body = str(next(msg_counter))
print(" Topic: {}, msg:{}".format(topic, msg_body))
s.send_multipart([topic.encode(), msg_body.encode()])
# short wait so we don't hog the cpu
time.sleep(0.1)
except KeyboardInterrupt:
pass
print("Waiting for message queues to flush...")
time.sleep(0.5)
print("Done.")
if __name__ == "__main__":
main()
topics_sub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.
Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost. More than
one subscriber can listen, and they can listen to different topics.
Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks' while listening to 'w' is enough to
catch 'weather'.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Brian Granger, Fernando Perez
#
# Distributed under the terms of the New BSD License. The full license is in
# the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------
import sys
import time
import zmq
import numpy
def main():
if len (sys.argv) < 2:
print ('usage: subscriber <connect_to> [topic topic ...]')
sys.exit (1)
connect_to = sys.argv[1]
topics = sys.argv[2:]
ctx = zmq.Context()
s = ctx.socket(zmq.SUB)
print(connect_to)
s.connect(connect_to)
# manage subscriptions
if not topics:
print("Receiving messages on ALL topics...")
s.setsockopt(zmq.SUBSCRIBE,'')
else:
print("Receiving messages on topics: %s ..." % topics)
for t in topics:
s.setsockopt_string(zmq.SUBSCRIBE,t)
print()
try:
while True:
topic, msg = s.recv_multipart()
print(' Topic: %s, msg:%s' % (topic, msg))
except KeyboardInterrupt:
pass
print("Done.")
if __name__ == "__main__":
main()
Thursday, August 11, 2016
Sunday, August 7, 2016
z50-70 hackintosh note
https://goo.gl/2bpXvt
Pretty much following the following two threads.
http://www.tonymacx86.com/threads/guide-lenovo-z50-70-using-clover-uefi-10-11.179520/
http://www.tonymacx86.com/threads/guide-booting-the-os-x-installer-on-laptops-with-clover.148093/
Note:
1) Clover version 3625 is good. Other versions may not boot. This is the main lesson learned.
2) Do not add more than needed.
3) Use MBR
Partition USB
diskutil list
diskutil partitionDisk /dev/disk1 2 MBR FAT32 "CLOVER EFI" 200Mi HFS+J "install_osx" R
Use Clover EFI 3625 install to install_osx
createinstallmedia to install_osx
https://support.apple.com/en-us/HT201372
BIOS enable UEFI first
Install and enjoy
Issues:
-WiFi
-Audio
Pretty much following the following two threads.
http://www.tonymacx86.com/threads/guide-lenovo-z50-70-using-clover-uefi-10-11.179520/
http://www.tonymacx86.com/threads/guide-booting-the-os-x-installer-on-laptops-with-clover.148093/
Note:
1) Clover version 3625 is good. Other versions may not boot. This is the main lesson learned.
2) Do not add more than needed.
3) Use MBR
Partition USB
diskutil list
diskutil partitionDisk /dev/disk1 2 MBR FAT32 "CLOVER EFI" 200Mi HFS+J "install_osx" R
Use Clover EFI 3625 install to install_osx
createinstallmedia to install_osx
https://support.apple.com/en-us/HT201372
BIOS enable UEFI first
Install and enjoy
Issues:
-WiFi
-Audio
Subscribe to:
Posts (Atom)