So much for good intentions. I get myself all geared up to
the problem I need to solve today? Well, I want to find out about how emacs networking works.
So first, I need a simple server to play with. Now normally when I think server I automatically think Apache but this time I want something a bit more basic. And if I was thinking enterprise1 I might reach for C++/ACE. However, for something basic, Perl is ideal.
I’ve just upgraded to Ubuntu 9.04 on this box and Perl is unused so let’s see if it has what I need.
06.52 Ubuntu finishes booting
I waste a few minutes on the internet.
06.57 I start Emacs
and remind myself just how gorgeous emacs-23 looks.
06.59 I check for the Net::Server package
$ perldoc Net::Server
You need to install the perl-doc package to use this program.
$ sudo apt-get install perl-doc
$ perldoc Net::Server
No documentation found for "Net::Server".
It is not installed. I could install it using apt (it is called libnet-server-perl) but I’ve got in the habit of using Perl’s CPAN module which provides package management facilities too. The advantage is that it is somewhat consistent across platforms.
$ sudo perl -MCPAN -e shell cpan[1]> install YAML cpan[2]> install CPAN cpan[3]> reload CPAN cpan[4]> install Bundle::CPAN
The CPAN bundle installs quite a bit so I go for breakfast.
07.15 Back from breakfast
cpan[5]> install Net::Server
I took this code pretty much straight from perldoc Net::Server.
#!/usr/bin/perl use strict; use warnings; package SimpleServer; use base qw(Net::Server); sub process_request { while (<STDIN>) { s/\r?\n$//; print STDERR "Received [$_]\n"; last if /quit/i; } } SimpleServer->run(port => 8080);
I tested it using telnet localhost 8080 to confirm it does what I need.
07.20 All done
Presumably Python and Ruby have similar incantations that will get a server up and running quickly.
And unfortunately at this point I have to go to work. But later on, I can begin experimenting with emacs networking.
1. i.e. some thing that grows humongous over a period of two years and then no-one wants to work with it anymore.
For networking there’s url.el package or using the functions:
(open-network-stream) which is synchronous and
(make-network-process) which can be asynchronous if the parameter is set. I haven’t been able to make the last one work, but check it out yourself.
If you make it work, please let me know the syntax. I’m trying to make emacs identica-mode ( http://blog.nethazard.net/identica-mode-for-emacs/ ) asynchronous, so that it won’t freeze emacs when internet connection is crappy.
Speaking of which there is (make-network-process :server t …) now and at least one “simple server to play with” can be had without leaving emacs:
http://repo.or.cz/w/muse-el.git?a=blob;f=contrib/httpd.el;h=083dcb6575fdddfccc4fb4cb7ea4db6476f4b174;hb=HEAD
As for a simple server…hate to say it after all the work you did, but have you tried netcat? It looks pretty darned close to what you did in Perl.
@Gabriel Saldana – thanks for the pointers. For making (make-network-process) asynchronous, how far did you get?
@phayes – that is pretty cool, and gives me the idea to make a FastCGI version of emacs. (just kidding!)
@Saint Aardvark – I didn’t think of netcat (and by all the work you did you’re being sarcastic right?
) But I think that the Perl has the advantage as I’ll be able to emulate a crappy internet connection by adding a sleep line to my server when I’m looking at the asynchronous stuff.