Posts

Showing posts from 2012

[ZeroMQ] The example of ZeroMQ via C#

Image
Recently I found these two articles that give a very good introduction of ZeroMQ and an useful use case for synchronized PUB-SUB pattern. Although it uses C# as its programming language instead of C, but I still think we can learn some important concept from these.And also, these articles provide very good images to illustrate the communication patterns.

http://www.codeproject.com/Articles/488207/ZeroMQ-via-Csharp-Introduction
There are several communication patterns described
http://www.codeproject.com/Articles/514959/ZeroMQ-via-Csharp-Multi-part-messages-JSON-and-Syn
Multi-part messagesSynchronized Pub-Sub pattern using PUB-SUB + REQ-REP




[SWIG] How to add C function in Python by using SWIG

Here is a simple example to use SWIG to automatically wrap C function and generate a wrapper and build a shared library for python./*** File : example.c ***/#include<time.h>doubleMy_variable=3.0;int fact(int n){if(n <=1)return1;elsereturn n*fact(n-1);}int my_mod(int x,int y){return(x%y);}char*get_time(){time_t ltime; time(&ltime);return ctime(&ltime);}/*************************/

/*** example.i ***/%module example %{/* Put header files here or function declarations like below */externdoubleMy_variable;externint fact(int n);externint my_mod(int x,int y);externchar*get_time();%}externdoubleMy_variable;externint fact(int n);externint my_mod(int x,int y

[Trema] The L2 isolation mechanism in sliceable switch

Image
If someone has ever seen the documents about sliceable switch as below, he/she will feel headache or sick because of a lot of contents and description.
https://github.com/trema/apps/wiki/sliceable_switch_tutorial
https://github.com/trema/apps/wiki/sliceable_switch_features

Now, I will give a flow control chart of slice function which is summarized from the source code ( slice.c ). That can give you a clear image about L2 isolation mechanism in sliceable switch, specially in Slice function. Check it out as the following chart:

 So, broadly speaking, the slice function will check mac binding first, then port_mac binding, and finally port binding. Meanwhile, some configurations will affect the result, for instance, "restrict hosts on port" enabled will force the slice function to check port_mac binding, otherwise, it won't do that.
Based on this flow chart, you can compare with the test cases in https://github.com/trema/apps/wiki/sliceable_switch_features







[MongoDB] Install MongoDB and try a simple example of mongodb_c_driver

MongoDB Installation
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux/
or
https://www.digitalocean.com/community/articles/how-to-install-mongodb-on-ubuntu-12-04
For instance in my environment:
  > sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
  > sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | tee -a /etc/apt/sources.list.d/10gen.list
  > sudo apt-get -y update
  > sudo apt-get -y install mongodb-10gen

Try command on MongoDB
>mongo
MongoDB shell version: 2.2.2
connecting to: test

> show dbs
db    (empty)
local    (empty)
test    0.203125GB

> use test
switched to db test

> show collections
foo
system.indexes

### Insert new data ###
> db.foo.save({a:1})
> doc = {
... "name" : "kristina",
... "contact info" : {
... "twitter" : "@kchodorow",
... "email" : "kristina@10gen.com"
... },
... "friends"…

[Memcached] Install memcached and try libmemcached C API

Install from package
> sudo apt-get install memcached

or Install from source code
We need to have :
libevent downloaded from : http://libevent.org/> ./configure --prefix=/usr> make> sudo make installmemcached downloaded from : http://memcached.org/> ./configure --prefix=/usr/local> make> sudo make installCheck the status of memcached
 > sudo service memcached status


Install libmemcached C API from source code

libmemcached C API ownloaded from http://libmemcached.org/libMemcached.html> ./configure --prefix=/usr> make > sudo make install
Give a simple try for libmemcached C API
> gcc -o mem_test2 mem_test2.c -lmemcached -lpthread
> ./mem_test2
Save key:key1 data:"This is c first value" success.
Fetch key:key1 data:This is c first value
Delete Key key1 success.

mem_test2.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <libmemcached/memcached.h> intmain(intargc,char*argv[]){memcached_st*memc;memcached_r…

[Presentation] OpenStack 2012 fall summit observation - Quantum/SDN

Taiwan OpenStack User Group (TWOSUG)3rd Meet Up is hold in Dec 5, 2012. I give a presentation in one of session, which is "OpenStack 2012 fall summit observation - Quantum/SDN". The topic is focused on Quantum and SDN and the slide is shared on shlideshare as follows:
http://www.slideshare.net/teyenliu/open-stack-2012-fall-summit-observation-with-quantumsdn-15493510

[iptables] some common examples of iptables rule

Read all tables without DNS lookup> iptables -L -nObtain the line number of the lines:  > iptables -L -nv --line-numbers Read NAT table in list without DNS lookup> iptables -t nat -L -nDo NAT ( SNAT )> echo "1" > /proc/sys/net/ipv4/ip_forward > iptables-t nat -A POSTROUTING -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} -o ${OUTSIDE_DEVICE} -j MASQUERADEor > iptables-t nat -A POSTROUTING -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} -o ${OUTSIDE_DEVICE} -j SNAT --to ${TARGET_IP} Do DNAT> iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.100.10:80 Drop the packet which is from 192.168.2.20 to 192.168.1.100 with TCP port 80> iptables -A POSTROUTING -t nat -s 192.168.2.20 -d 192.168.1.100 -p TCP --dport 80 -j DROPAccept the packet which is from 192.168.100.0/24 and interface eth1> iptables -A INPUT -i eth1 -s 192.168.100.0/24 -j ACCEPT Insert a logging rule between the last one which drops packet with iptables something like…

[Mongrel2] How to write a handler for mongrel2 web server

Image
Mongrel2 is an application, language, and network architecture agnostic web server that focuses on web applications. The most powerful functionality is to use Handler to deal with ZeroMQ message.
There are already some articles talking about Handler and how to get started, for instance:
http://www.ioncannon.net/programming/1384/example-mongrel2-handler-in-ruby/
http://brubeck.io/demos.html

I almost studied 2 days to understand the usage of handler and try a simple handler to respond the request, and finally I finished. So, in this article, I will give a concept about Mongrel2 web server and a simple example.


From this diagram, you will quickly realize how the mongrel2 web server works with your handler by what kind of zeromq communication type.Second, I will give the handler example in details:The conf file ==> mongrel2.conf
brubeck_handler = Handler(
    send_spec='tcp://127.0.0.1:9999',
    send_ident='34f9ceee-cd52-4b7f-b197-88bf2f0ec378',
    recv_spec='tcp://127.0…

[ZeroMQ] the study note of the ØMQ guilde -- Part II

Image
Shared Queue ( DEALER and ROUTER sockets) The only constraint is that services must be stateless, all state being in the request or in some shared storage such as a database. It then uses zmq-poll[3] to monitor these two sockets for activity and when it has some, it shuttles messages between its two sockets. It doesn't actually manage any queues explicitly — ØMQ does that automatically on each socket.
Built-in Proxy Function
Please see the examle of msgqueue.c that replaces rrbroker.c with built-in proxy function.

Transport Bridging
Handling Errors In most of the C examples we've seen so far there's been no error handling. Real code should do error handling on every single ØMQ call. void*context = zmq-ctx-new ();
assert (context);
void*socket = zmq-socket (context, ZMQ-REP);
assert (socket);
int rc = zmq-bind (socket, "tcp://*:5555");
if (rc !=0) {
printf ("E: bind failed: %s\n", strerror (errno));
return-1;
}
We'll use a publish-subscribe model to sen…

[ZeroMQ] the study note of the ØMQ guilde -- Part I

Image
I have intrudced ZeroMQ as very powerful tool to leverage your application to become a distrubuted system. If you see http://zguide.zeromq.org/page:alland take a look at the content, you will realize there are a lot of stuff that needs to study in details. Due to this reason, I will summarize what I have studied in ZeroMQ and let me give some notes about the important concepts. Request-Reply pattern: The REQ-REP socket pair is lockstep. The client does zmq_msg_send(3) and then zmq_msg_recv(3), in a loop.
They create a ØMQ context to work with, and a socket. If you kill the server (Ctrl-C) and restart it, the client won't recover properly.
Take care of string in C: When you receive string data from ØMQ, in C, you simply cannot trust that it's safely terminated. Every single time you read a string you should allocate a new buffer with space for an extra byte, copy the string, and terminate it properly with a null. So let's establish the rule that ØMQ strings are length-s…