Erlang convert IP to integer & integer to IP.

Jut needed to convert ip to integer for Erlang project and have not found code via Google. So decided to post it here for anybody who will find it helpful.

First function converting IP tuple to integer:

ip_to_int({A,B,C,D}) -> (A*16777216)+(B*65536)+(C*256)+(D).

Code to convert integer to IP tuple:

int_to_ip(Ip)->	{Ip bsr 24, (Ip band 16711680) bsr 16, (Ip band 65280) bsr 8, Ip band 255}.

And brief dumb example:

Eshell V5.8.4  (abort with ^G)
1> Ip = {1,2,3,4}.
{1,2,3,4}
2> db_server:int_to_ip(db_server:ip_to_int(Ip)).
{1,2,3,4}
3> 

Hope this code helped you, if you have reached this page :)

What is Erlang open_port and os:cmd?

Sometimes it happens that you need to execute external command. Same happens when you are programming Erlang. Usually it is enough to use os:cmd. It executes the command and returns the result. Like the following

LsOut = os:cmd("ls"), % on unix platform

But sometimes you need to start the process and communicate with it via standard input and output. For example most of linux users know GnuChess program. And may be somebody even tried to start it from the shell without GUI. So, the following simple example will show how to start GnuChess and communicate with the process from Erlang.

-module(pipe).
-author('Sergiy Dzysyak ').

-compile(export_all).

start()->
	spawn(?MODULE, read, []).
	
read() ->
  Port = open_port({spawn,"/usr/bin/gnuchess -xe"},[binary,{line, 255}]),
  do_read(Port).

do_read(Port) ->
  receive
    {Port,{data,Data}} ->
    	io:format("Data: ~p~n",[Data]);
    {Port,eof} ->
      read();
    {go, Go} ->
    	Port ! {self(), {command, Go}};
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])
  end,
  do_read(Port). 

Example is very basic and to stat it you should do the following:

[serg@localhost erl.chess]$ erl
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> c(pipe).
{ok,pipe}
2> Pid = pipe:start().
<0.38.0>
3> Pid ! {go, <<"e2e4\n">>}.
{go,<<"e2e4\n">>}
Data: {eol,<<"Chess">>}
Data: {eol,<<"Adjusting HashSize to 1024 slots">>}
Data: {eol,<<"1. e2e4">>}
Data: {eol,<<"1. ... e7e5">>}
Data: {eol,<<"My move is: e7e5">>}
4> 

In a similar way communication via linux named pipes can be organized with external programs or even more complicated goals may be achieved. I have not tried to load drivers & communicate with them via port_open, but if I will get a minute, I will try and describe it here in my blog...

Gedit plugin: New from template

I am not sure how many people finding Gedit good enough as an editor for programming purposes, but I am using it exactly that way and quite happy with its functionality. Especially I like its integration with gnome virtual file system, that allows it to work directly on remote hosts, and ability to use and create new plugins for it.
With the release of new version of gnome environment & new version of Gedit text editor I met some problems with the compatibility of old plugins. So, I have to redo some plugins I have used before. First of all I decided to implement plugin that allows creation of new documents from templates.
Plugin is quite simple, but I have not much python programming experience and I should notice that there seems not much documentation available about new Gedit, Gtk Python API.... So, if someone has more experience in Python or have some good ideas regarding the plugin functionality you are welcome to fork Git repo or post a comment here.
Plugin on GitHub: https://github.com/dzysyak/Templates-plugin-for-GEdit3

Royalty free music search engine right in your browser (firefox, IE)

Most of web developers, especially who are dealing with flash, usually need to find some sound or loop. Personally I am not a flash developer, so had only a few flash projects or better to say projects with flash parts :)  But I felt myself uncomfortable when I had to go to some royalty free music site and only than search for what I needed. How it would be great to have it integrated into my browser search box I thought, and after a small research I have found Cool Stock Music. I am not sure about other sites yet, have just found this one. May be somebody else is providing similar thing too, but that is not important right now. Once you got to the site with your firefox or IE (as far as I know search plugins are supported from IE7)  you can open search engine selection, like on the image below and add "Cool Stock Music" to the list of search engines.


Once new engine is added to the list you can search sounds and loops right from the search box. Personally I am finding this very handy, as I do not have to go to the site, search form (do not want to remind all my disappointments). But what I like even more, are suggestions that are shown exactly like for google search, and sometimes good suggestion can save lot of time :)


Hope that someone will find this article useful and it will save somebody little bit of time and little bit of nerve cells. Personally I like this "search plugin" approach and think that guys on the right way :)

Example of Erlang crossdomain policy server for Adobe Flash

Latest version of article & code can be found on my new blog: Example of Erlang crossdomain policy server for Adobe Flash

Some time ago I had to use sockets from Adobe Flash/Flex, so needed a simple one implemented in Erlang. I do not suggest to use it exactly like it listed below, but you may find some idea for your project there. I hope that you know what I am talking about, if you are reading this article, so do not want to waist your time...