Archive for the 'erlang' Category

30
Jul
09

using concurrency primitives

Everything we’ve learned about sequential programming is still true
for concurrent programming. All we have to do is to add the following
primitives:

Pid = spawn(Fun)
Pid ! Message
receiveend

Pid = spawn(Fun)

Creates a new concurrent process that evaluates Fun. The new
process runs in parallel with the caller. spawn returns a Pid (short
for process identifier). You can use Pid to send messages to the
process.

Pid ! Message

Sends Message to the process with identifier Pid. Message sending
is asynchronous. The sender does not wait but continues with
what it was doing. ! is called the send operator.
Pid ! M is defined to be M—the message sending primitive ! returns
the message itself. Because of this, Pid1 ! Pid2 ! … ! M means send
the message M to all the processes Pid1, Pid2, and so on.

receiveend

Receives a message that has been sent to a process. It has the following skeleton :

receive
_ ->
ok
end.

To be continue with simple example …

24
Jul
09

Concurrent Programming

• Creating and destroying processes is very fast.
• Sending messages between processes is very fast.
• Processes behave the same way on all operating systems.
• We can have very large numbers of processes.
• Processes share no memory and are completely independent.
• The only way for processes to interact is through message passing.

Erlang and so much more !

06
Jun
09

CouchDB

Sự ra đời của Apache-CouchDB là một tất yếu với nhu cầu khai thác/lưu trữ các dạng web resources như : images/css/javascript/…. Kiến trúc của Apache-CouchDB cho thấy khả năng chịu tải cao (nền vm erlang), khả năng tải các transaction đa chiều, hỗ trợ partitioning/clustering … Với mô hình FT và SFDOD, khai thác dựa trên RESTful HTTP/JSON API.

Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API. Among other features, it provides robust, incremental replication with bi-directional conflict detection and resolution, and is queryable and indexable using a table-oriented view engine with JavaScript acting as the default view definition language.

CouchDB is written in Erlang, but can be easily accessed from any environment that provides means to make HTTP requests. There are a multitude of third-party client libraries that make this even easier for a variety of programming languages and environments.

See the introduction and the technical overview for more information.

06
Jun
09

Variable trong erlang

Để chứa giá trị đương nhiên ta phải cần dùng đến variable, trong erlang để định nghĩa 1 variable rất đơn giản như  sau :

1> X = 2009.
2009
2>

Như  vậy ta đã khai báo 1 variable là X có giá trị được gán là 1 số nguyên = 2009.

+ Variable trong erlang khác hoàn toàn so với variable trong một số ngôn ngữ cấp cao khác như : java hay C. Vì thế nó không hẳn là 1 variable đúng nghĩa !?.
+ 1 variable trong erlang được định nghĩ là 1 single assignment variables (bound/unbound). 1 dạng name suggests, tức là không thể thay đổi được giá trị khác (giá trị duy nhất), nếu cố gắng thay đổi  bạn sẽ nhận được 1 brutal error message:

3> X = 1234.
=ERROR REPORT==== 11-May-2008::20:32:49 ===
Error in process <0.31.0> with exit value:
{{badmatch,1234},[{erl_eval,expr,3}]}

** exited: {{badmatch,1234},[{erl_eval,expr,3}]} **

+ 1 variable đã được gán cho 1 giá trị cụ thể thì được gọi là bound variable.
+ 1 variable trước khi được gán giá trị thì được gọi là unbound variable.

Lưu ý : variables trong erlang luôn bắt đầu bằng 1 ký tự viết Hoa.

05
Jun
09

RabbitMQ

Lại chiến RabbitMQ, nhưng lần này là Erlang/Python …

http://barryp.org/software/py-amqplib/

03
Jun
09

ejabberd module (gen_mod behavior).

Như ta đã biết với kiến trúc nền mở của ejabberd im server + vm erlang và sức mạnh của ngôn ngữ lập trình erlang, cho phép chúng ta implement/deploy ejabberd modules (ở đây interface là gen_mod behavior) vào hệ thống.

Cơ bản của một ejabberd module (gen_mod behavior) có skeleton như sau :
- Ở đây tôi đặt tên module này là : mod_test.
- Skeleton bắt buộc khi viết ejabberd module gen_mod behavior là phải có 2 export api (2 hàm callback) :
+ start với 2 arguments signal (_Host, _Opts).
+ stop với 1 arguments signal (_Host).
- Lưu ý underscores trước mỗi argument signal.
- 2 hàm callback trên sẽ được gọi sau khi start/stop ejabberd server.

-module(mod_test).
-behavior(gen_mod).

%%Export API
-export([start/2, stop/1]).

%% External APIs implement
start(_Host, _Opts) ->
ok.

stop(_Host) ->
ok.
%% Internal APIs implement

- Tiến hành compile ejabberd module trên bằng erlc, chúng ta có được mod_erlang.beam (erlang bye code) file.
(erlc -I /path/to/include -pz /path/to/ebin mod_test.erl).
- Deploy module này vào hệ thống thông qua file cấu hình (/path/to/ejabberd-x.x.x/conf/ejabberd.cfg) :

{modules,[
...
{mod_test, []},

]}.

* Stop/Start ejabberd server.




 

May 2012
M T W T F S S
« Apr    
 123456
78910111213
14151617181920
21222324252627
28293031  

del.icio.us

Con gái yêu.

CIMG0003

16/01/2010

Đầu tóc rối tung ...

More Photos

jabber

cuonglb@jabber.mobi
cuonglb@jabber.org
cuonglb@gmail.com
cuonglb@facemain.com
cuong@livemarkets.com
cuonglb@moo.im
cuonglb@jabber.snc.ru
cuonglb@alpha-labs.net
cuonglb@jaim.at
cuonglb@jabber.bluendo.com

Twitter Updates


Follow

Get every new post delivered to your Inbox.