Integration of Scriptaculous slider in Zend framework

by alf

29 05 2008

For a customer need, I’ve integrated the scriptaculous slider as shown here in Zend framework.

To achieve this, I’ve created a formSlider view helper which is then used by a slider form element.

The view helper and form element code is here. To allow ZF to find the helper, don’t forget to add path to helper in your bootstrap :

$layout->getView()->addHelperPath('Mc/View/Helper','Mc_View_Helper') ;

You need of course to include scriptaculous and prototype to make it work (available here and here) :

<script src="/prototype-1.6.0.2.js" type="text/javascript"></script>
<script src="/scriptaculous.js" type="text/javascript"></script>

Then, you can create a slider like this :

require_once('Mc/Form/Element/Slider.php') ;
$my_form->addElement(new Mc_Form_Element_Slider('my_element')
    ,'my_element') ;
$my_form->my_element->setLabel('My element : ')
    ->setMin(10)
    ->setMax(30)
    ->setStep(2);

and retrieve the value as usual :

$my_form->my_element->getValue() ;

And if you want to override the default style in your css, don’t forget the !important argument :

div.slider {
    width:256px !important;
    margin:10px 0 !important;
    background-color:lightgray !important;
    height:15px !important;
    position: relative !important;
 }

div.slider div.handle {
     width:10px !important;
     height:15px !important;
     background-color:darkgray !important;
     cursor:move !important;
     position: absolute !important;
 }


Autoresize textarea

by alf

28 05 2008

I was looking for a javascript which could automatically resize a textarea but the ones I’ve found were a bit buggy or ugly, so I’ve written mine. There’s just a tiny bug with copy/paste with mouse.

function autoresize(txtbox)
{
    var cols = txtbox.cols ;
    var content = txtbox.value ;
    var lineCount = 0 ;

    var lastEOL = -1 ;
    do {
        var begin = lastEOL+1 ;
        lastEOL = content.indexOf("\n",lastEOL+1) ;
        var line = "" ;
        if(lastEOL != -1) {
            line = content.substring(begin,lastEOL) ;
        } else {
            line = content.substring(begin,content.length) ;
        }
        var rows_in_line = Math.floor(line.length/cols)+1 ;
        lineCount += rows_in_line
    } while (lastEOL != -1) ;
    txtbox.rows = lineCount ;
}

and html code :

<textarea name="my_textbox" onkeyup="autoresize(this)"
onmouseup="autoresize(this)" rows="24" cols="80"></textarea>


How to extract audio from a DVD and encode to flac

by alf

27 05 2008

To see information about the dvd :

mplayer -identify -frames 0 dvd://

To encode the 17 chapters of the first title of a dvd :

for i in $(seq 17) ; do mplayer -vo null -ao pcm -ao pcm:file=$i.wav:fast -chapter $i-$i dvd://01 ; flac $i.wav --best & done



Temps de réponse réél d’une page web avec … netcat

by regilero

25 05 2008
temps-de-reponse-reel-dune-page-web-avec-netcat

Bon, j’imagine que la plupart des gens (heu.. d’un certain type) savent qu’on peut faire du web ‘à la main’ avec telnet ou netcat. Et bien on peut. Donc. Je voulais me servir de cela pour tracer quelques temps de réponse de pages html précises. Le tout en une seule ligne histoire de se servir de l’historique et pas de mon cerveau pour la retaper.

Commencons par un telnet sur www.google.com sur le port 80 (web). On tape la requète comme si nous étions un navigateur web (très basique le navigateur). La requète HTTP va donc être:
GET / HTTP/1.0
Remarquez qu’il y a une ligne vide (donc deux fois entrée).
Cela donne en résultat une page de redirection vers le site français de google, parfait.
nii:~$ telnet www.google.com 80
Trying 209.85.129.147...
Connected to www.l.google.com.
Escape character is '^]'.
GET / HTTP/1.0
.
HTTP/1.0 302 Found
Location: http://www.google.fr/
(.. je vous passe le reste ...)
Connection closed by foreign host.
nii:~$

Bon, maintenant je veux mesurer le temps de réponse. Avec tout bètement la commande ‘time’. Pour faire cela l’idéal est de taper ma commande en une seule fois, et là telnet n’est plus très souple pour automatiser la saisie utilisateur On va commencer par tester la même chose avec netcat (nc)
nii:~$ printf 'GET / HTTP/1.0\n\n' | \
nc -w 10 www.exemple.com 80

On avance. Par contre on a de fortes chances de ne pas tomber sur la page web que l’on recherche vraiment. On a de très fortes chances de tomber sur le VirtualHost par défaut du serveur HTTP à l’autre bout. Mais pour servir le bon site il faut taper du HTTP/1.1 au lieu de 1.0 et ajouter un header à notre requète indiquant au serveur le nom du site que l’on veut (parmi ceux qu’il héberge), revoyez le protocole http version 1.1 si vous ne comprenez rien à ce que je dis.
nii:~$ printf 'GET / HTTP/1.1\nHost:www.exemple.com\n\n' | \
nc -w 10 -q 10 www.exemple.com 80

Et maintenant pour avoir le temps de la commande je rajoute time au début et je fais sauter le temps d’affichage de la réponse…
nii:~$ time printf 'GET / HTTP/1.1\nHost:www.exemple.com\n\n' | \
nc -w 10 -q 10 www.exemple.com 80 1>/dev/null
.
real 0m0.627s
user 0m0.000s
sys 0m0.004s

Mais…
Il y a une grosse erreur. On se prends le temps d’une requète DNS de la machine sur laquelle on est qui cherche l’adresse IP de l’hôte que l’on a donné à netcat (après le nc -w 10) pour aller ouvrir une connexion tcp sur le port 80 de cet hôte. Ce temps DNS ne sert à rien, il fausse notre résultat (et c’est souvent très long le DNS). Il faut utiliser l’adresse IP du serveur web directement. De toutes façons c’est à l’intérieur du protocole HTTP, avec notre entête Host: que l’on indique le site web demandé au serveur, et pas du tout avec le nom DNS utilisé pour résoudre l’adresse IP du serveur web.
nii:~$ nslookup ww.exemple.com
66.116.125.121
nii:~$ time printf 'GET / HTTP/1.1\nHost:www.exemple.com\n\n' | \
nc -w 10 -q 10 66.116.125.121 80 1>/dev/null
.
real 0m0.408s
user 0m0.004s
sys 0m0.000s

0.408s est déjà plus proche du temps de réponse réèl du site (depuis le point du réseau où on se trouve, il y a des pertes dues au réseau, forcément).

PS: vive les sites Best Viewed with telnet to port 80



Afficher des documents Plomino dans GoogleMaps

by ebr

22 05 2008

… rien de plus simple.

Quelques lignes de javascript dans un view template font l’affaire. Tous les détails dans ce how to.



libmodbus 2.0.0 “Slaves to Our Machines” is out!

by Stéphane

18 05 2008

libmodbus is library to send/receive data to/from ModBus devices which respect the protocol established by Modicon.

This new release contains major enhancements like the slave component.
A special thanks to Todd Denniston for this release.

The Ubuntu users can obtain the packages of this project here:
https://launchpad.net/~sra/+archive

libmodbus 2.0.0 (2008-05-18)
============================
- Slave API
https://blueprints.launchpad.net/libmodbus/+spec/slave-api
- No more glib dependency
https://blueprints.launchpad.net/libmodbus/+spec/glib-dependency
- Unit testing and many test programs
- Waf build support
https://blueprints.launchpad.net/libmodbus/+spec/waf-support
- MacOS X support by Matthew Butch
https://blueprints.launchpad.net/libmodbus/+spec/macosx-support
- No more glib dependency
https://blueprints.launchpad.net/libmodbus/+spec/glib-dependency
- Unit testing (unit-test-slave and unit-test-master)
- Port number is now defined at initialisation by Dirk Reusch
- Better memory management (uint8_t *data and packing of
modbus_param_t)
- Better error management
- Declare many static functions and const arrays
- Enhance an integer division
- The GNU licences LGPL and GPL are in version 3
- Debian and RPM packages (#224496)
- Many cleanups
- Fix #159443 reported by Stefan Bisanz
Index of incoming data in force multiple coils function
- Fix #161989 reported by Konstantinos Togias
Serial device paths more than 10 chars long (eg. /dev/ttyUSB0) don’t
fit to modbus_param_t -> device char[11] var.
- Fix #188189 reported by Chris Hellyar
Compute_response_size() no entry for read_input_status()
- Fix #191039 reported by Todd Denniston
modbus.h is not installed at prefix.
- Fix #211460 reported by Todd Denniston
With TCP, automatic reconnect on error may not be desired.
- Fix #224485 reported by Todd Denniston
libmodbus does not link with c++ code.
- Fix #224496 reported by Todd Denniston
It is easier to install on rpm based systems with a spec file.



Reduce open calls on Ubuntu

by Stéphane

13 05 2008

You’ve installed a fresh Ubuntu and you’re not English. Just try:

$ strace gnome-terminal

and you’ll see this long list of calls only to open gtk20.mo (12 calls on Hardy Heron):

open("/usr/share/locale/fr_FR.UTF-8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/fr_FR.utf8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/fr_FR/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/fr.UTF-8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/fr.utf8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/fr/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr_FR.UTF-8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr_FR.utf8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr_FR/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr.UTF-8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr.utf8/LC_MESSAGES/gtk20.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale-langpack/fr/LC_MESSAGES/gtk20.mo", O_RDONLY) = 3

There are many solutions to workaround this performance problem, either you use my little script reduce-mo-open-calls.sh which detect your current locale and create only some symlinks, or you can try someting more adventurous (I don’t know the result after a upgrade of your langpack), move /usr/share/locale-langpack/fr/LC_MESSAGES/* to /usr/share/locale/fr/LC_MESSAGES/ and create the following symlinks after deleting of /usr/share/locale-langpack/fr:

ln -s /usr/share/locale/fr /usr/share/locale-langpack/fr

then from /usr/share/locale/, /usr/share/locale-langpack and /usr/lib/locale:

ln -sfvT fr fr_FR.UTF-8
ln -sfvT fr fr_FR.utf8
ln -sfvT fr fr.UTF-8
ln -sfvT fr fr.utf8
ln -sfvT fr fr_FR

If someone knows a better solution to configure the paths order, I’m interested! I’m intend to post a bug report if I don’t see any strange systems calls in the next weeks.

This hack removes 89 open calls at the launch of gnome-terminal (the result is better with bigger applications), it’s pretty nice, isn’t it?