/usr/portage

New languages to broaden my horizon 6

I’ve read a bit and played around with Scala and Arc recently. I always recommend to learn other languages. Nobody is forced to get productive tomorrow in a language one learns but it always makes you a better programmer. This sounds trivial but I often met programmers who did only Java or only PHP or … you name it. There is nothing wrong with focusing on a language and getting really good at it, but ignorance is wrong. A good programmer always wants to learn, he basically defines himself through his fast ability to adopt new stuff. Everytime I look at different languages outside of my daily scope (which is mostly PHP and C, Ruby and Python for fun), especially on those who look exotic from the first view, I get something better. When I’ve tried to learn Erlang – even I totally failed at really getting it – I looked completely different afterwards at PHP constructs like array_filter(), array_walk() and the general concept of callbacks. When you halfway know both worlds, the functional as well as the object oriented, you just get more effective in applying the adequate methodology to solve an issue.

Back to Scala. Scala comes with a really feature rich object model. It provides classes, a concept well known to those who do Java or C++. Classes and methods may even be abstract. So far nothing new. But Scala provides a language feature so called traits. It addresses the issue that single inheritance is often too limited when it comes to stable (and convenient) compositions while mixins as in Ruby or classical multiple inheritance as in Python induce fragile, hardly maintanable hierarchies. The answer are traits. Traits allow stable compositions. A trait is basically a container for functionality. A class may extend from multiple traits, rename or overrride methods. Think on a debugging functionality – in this case implemented as a debugging trait – which should be used in a number of classes while each class has its own hierarchy. The debugger is than easily composited with the debugging consumer class while the class hierarchy is kept and the composition is stable itself. In PHP we do big wrenches to do composition. We introduce broker components to handle composited objects and what not. Traits are an elegant solution to the problem, a really impressive language construct.
Scala also provides generics. Generics are datatypes that can take other types while keeping type safety. Think you have an array and can enforce it to only accept instances of the class “Foo”. C#, Java and C++ implement that concept for a long time but nevertheless it is really handy.
I’m not really sure, how handy that would be in practice, but Scala enforces to explicitly overrride methods. I have the feeling that after a while one would do “override def method()” with the same implicitness as we do it today. Maybe this feature will be good for teaching object oriented programming.
The general syntax is a best of both worlds, the Java and the Ruby world. It is also possible to reuse existing Java libraries in Ruby, which makes it pretty attractive and lowers the entry barrier as a full stack of libraries are already present.

Arc is the newest baby by Paul Graham, the inventor of the bayesian spamfilter which mildens the pain for everyone of us dealing with email. Graham loves Lisp and it relatives. This is why Arc is very lisp’ish including polish notation for mathematical expressions. Actually the code examples are so incredible short to do a lot of stuff I guess I will try some sort of prototyping with it in the future. The plan is to design Arc to be useful in 100 years. This means time for adoption.

Filed under , , , , , , , & six comments & no trackbacks

A naive approach to mixins in PHP 3

Mixins is a known multiple inheritance concept from languages like Python or Ruby or (but in a different way in JavaScript). In Ruby you can include another class definition, in Python you extend from multiple classes and in JavaScript you use prototype to copy methods from one class to another. In PHP there is no default strategy how to reach that, but nevertheless multiple inheritance leads often to the advanced usage of the mudclump pattern, sometimes it is practically. Think on PHPUnit and its assert*()-methods. They are defined in PHPUnit_Framework_Assert. PHPUnit_Framework_TestCase is derived from the last but it would be much nicer to have the possibility here to mixin my custom assertions. For example I have a custom assertion to ensure a certain object implements a valid singleton. Currently I need to patch PHPUnit, but why should I need to?
I am currently working on a half-automized storage component, which provides helper functions for database queries (auto-generate a WHERE-clause from a filter object, create a field list out of a dependency list and stuff like that). I do not want to pollute my class hierarchy but I want to have them pluggable and I want my collegues to add their own. So here is my naive approach (only works for methods).


Continue reading "A naive approach to mixins in PHP"

Filed under , , , , & three comments & no trackbacks

»sleep« for special purposes 2

I have a problem which occurs every evening: often I started a video using realplayer before going to bed. Realplayer does not close itself at the end of the video, which is correct in general but in my case not the best practice. Realplayer is a proprietary tool and uses Open Sound System (OSS), which is a bit antiquated nowadays. Also OSS was blocking the sound device when playing sounds. The crap Enlightment Sound Daemon (ESD) was a result of that issue. Back to my topic. Often when I go to bed I want to watch a video, but always want my Banshee to wake me up in the morning. But Banshee cannot play any sound, if the device is blocked by realplayer. So I need to kill realplayer once the video is done or a bit later but before the next morning. So I do something like sleep 3600 ; kill <pid> which works pretty fine. If I’m drunken, which takes place naturally rarely, I have the problem that I do not remember how big the integer after my sleep must be. And, especially when I’m drunken, I hate it to have my video quitting before it is done or I felt asleep. So I need a sleep-replacement which is more convenient and usable when being drunken. In fact I need a sleep replacement which can handle strings like »2m«, »2h«, »60*60*2« etc. pp. Download it! And now I can go to bed.

Filed under , , , & two comments & no trackbacks

Release early, release often 0

I’ve written a replacement for the well-known locate on Unix-plattforms. It is designed to act in a client/server model (currently not implemented) and should do auto-updating via inotify (currently also not implemented, first of all I need to fix the Ruby-bindings for inotify) but it does indexing and searching and this works relatively well. Some speed improvements are needed but I’ve just spent something around two hours from the idea to the implementation. If you want to give it a try, just check out http://svn.usrportage.de/rblocate/trunk/ and create a SQLite3-database in the same directory where you script is.

$ sqlite3 files.db
SQLite version 3.3.6
Enter ".help" for instructions
sqlite> CREATE TABLE files (id INTEGER PRIMARY KEY AUTOINCREMENT, path VARCHAR(1024) UNIQUE);
sqlite> .quit

Now run ./search.rb --index <dir> and after that just ./search.rb --search <term>. Have fun!

Filed under , , & no comments & no trackbacks

Multithreaded TCP-server in Ruby 0

Hopefully on Wednesday Blogmonitor, a new project of Interdings will be open for the public. In the last days and hours I was working on the API-components. An XML/RPC-interface, an REST-service and as a nice goodie a multithreaded TCP-server where you can watch all the incoming pings when they occur. The concept is the following: when an incoming ping occurs, the encapsulated class Blogmonitor::Pingservice::Ping sends a notification to the TCP-component, which waits for incoming connections on a custom port and serves the snippet to all of the connected clients. This is invented as really simple interface and has nothing to do with more standartized technologies like »Publish & Subscribe«, also the logic would be similiar.


What does Ruby already provide?


Ruby ships an example of a multihreaded server implementation which is called GServer. It provides the possibility to create a child-class of it, overload the serve()-method and be happy. That’s really a good beginning and the concept of having patterns seems to succeed again: don’t code things twice.


Example I, first working snippet


<ol><li>require 'gserver'</li><li>class MyServer < GServer</li><li>  def serve( io )</li><li>    io.puts( "Hello world" )</li><li>  end</li><li>end</li><li></li><li>s = MyServer.new 1234</li><li>s.start</li><li>s.join</li></ol>

The code should be more or less self-explaining: load the pattern, extend the class GServer and overload the method server(), which provides an IO-object. The method puts() of the IO-object writes »Hello world« to connected client.
Just create an instance, start() the server, join().
Connect with telnet to localhost and port 1234:

telnet localhost 1234
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
<strong>Hello World</strong>
Connection closed by foreign host.

Now you have a simple TCP-Server. But we miss a few things: on the one-hand the backend notification is not implemented and the connection is not persistent. If a client connects, it should not be disconnected just to receive a stream of incoming weblogs. First of all: implement the streaming thingy. Have a look at the lines 3-5 of example I: the serve()-method is executed on a new connection and disconnects the client when it method is called. But we want to have it persistant. A dirty solution would be to write the io-code in an endless loop, which works pretty fine.


Example II, persistant server


<ol><li>require 'gserver'</li><li>class MyServer < GServer</li><li>  def serve( io )</li><li>    loop do</li><li>      io.puts( "Hello World" )</li><li>    end</li><li>  end</li><li>end</li><li></li><li>s = MyServer.new 1234</li><li>s.start</li><li>s.join</li></ol>


Let’s implement the backend thingy. During my implementation I ran into a number of problems: I had a working example on my Linux-box there and after putting it on our development-system, which is a Solaris-driven T1000 from Sun I saw that from strange reason the implementation of sockets in Ruby seems not to work on Solaris, which is pretty annoying but in the end I’m happy it did not work with sockets. The next idea was to use just TCP with non-blocking connections to send packages from the ping-interface to the Live-monitor-component, but sadly Ruby on Solaris does sadly also not support non-blocking connections. The thing I’d stuck with was UDP. Good old UDP but I think it is pretty ok for this uncritical task. To send some message, you also have a pattern called UDPSocket which can be utilized to do this task.


Example III, UDP-server


<ol><li>require 'socket'</li><li>u = UDPSocket.new</li><li>u.bind( "127.0.0.1", 1357 )</li><li>message = u.recvfrom( 512 )[0]</li><li>puts message</li></ol>

Example IV, UDP-client

<ol><li>require 'socket'</li><li>s = UDPSocket.new</li><li>s.connect( "localhost", 1357 )</li><li>s.send( "Hello world", 0 )</li><li>s.close</li></ol>


The next problem is to make sure all listening TCP-clients get notified. So we need to have something like an IO-heap, where we store all the existing IO-objects and iterate through them to notify everyone. So we modify the serve()-method as it follows:

  1. def serve( io )
  2. io_heap << io</li><li> loop do; end</li><li>end</li></ol></pre> <p>But who sends the snippet to all the objects? We also overload the constructor of our MyServer class to start a notification thread which does the following tasks:<ul><li>Start the UDP-server</li> <li>Iterate to our heap of IO-objects to notify them all</li></ul></p> <h3>Example V, the complete component</h3> <pre><ol><li>require 'gserver'</li><li></li><li>class MyServer < GServer</li><li> def initialize( port, *args )</li><li> io_heap = []
  3. udp = Thread.fork do</li><li> udp_socket = UDPSocket.new
  4. udp_socket.bind( "localhost", 1357 )</li><li> loop do</li><li> payload = udp_socket.recvfrom( 512 )[0]
  5. unless payload.empty?
  6. io_heap.each do |io|</li><li> begin</li><li> io.puts payload</li><li> rescue Errno::EPIPE => error</li><li> io_heap.delete io
  7. end
  8. end
  9. end
  10. end
  11. end
  12. super(port, *args)
  13. end
  14. def serve( io )
  15. @io_heap << io
  16. loop do; end
  17. end
  18. end
  19. server = MyServer.new( 1234, host=“localhost” )
  20. server.start
  21. server.join

Isn’t it really, really short?

Filed under , , , , , , & no comments & no trackbacks

Sprücheklopperei 2

Den mitlesenden Computeraffinen gilt es die letzte Einlassung des Kollegen Ohlig nicht vorzuenthalten:

»Python ist eine Strebersprache. Ruby ist dagegen irgendwie mehr Rock ’n Roll«
Schöner lässt es sich kaum zusammenfassen, auch wenn dem Streben manchmal ein gewisser Reiz anhaftet. Letztmalig festgestellt beim Schreiben von mod_jabber.py letztes Wochenende. Der Zwang zur ordentlichen Einrückung ist beim Debuggen schon eher anstrengend.

Filed under , , , , , & two comments & no trackbacks

Symbole in PHP emulieren 0

PHP bietet von Haus aus keine Symbole. Ist es in Sprachen wie Ruby möglich, Symbole als Methoden-Argumente zu verwenden, so entfällt diese Möglichkeit in PHP. Dabei bieten Symbole im Gegensatz zu Werten oftmals große Vorteile: sie ermöglichen API-Stabilität, auch wenn sich die Funktionalität einer Klasse beständig ändert, denn das Zuweisen eines Wertes kann in der Klasse bzw. Methode geschehen. Auch wenn sie PHP nicht mitbringt, gibt es eine Möglichkeit, solche Symbole zu emulieren.
Erst einmal ein Beispiel in Ruby:

class Bla
        def self.foobar( param )
                print “Here we are!\n” if param == :sym
        end
end

bla = Bla;
bla.foobar( :sym )

Nun bietet PHP seit der Version 5.0 bekanntermaßen ja Klassen-Konstanten, die sich für diesen Zweck recht praktisch missbrauchen lassen. Das bekannte Beispiel in PHP:

class Bla
{ const SYM = “bla”; public function foobar( $param ) { if( $param == Bla::SYM ) print “Here we are!\n”; }
}

$bla = new Bla;
$bla->foobar( Bla::SYM );


Nachteile hat das Verfahren kaum, nur dass die Parameterliste oftmals sehr lang wird, wenn mehrere Klassen-Konstanten als Argumente übergeben werden und sprechende Klassen- bzw. Parameternamen verwendet werden.

Filed under , , , , , , , & no comments & no trackbacks

Mails von kaputtem procmail wiederherstellen 0

Ein Problem von procmail ist die unstrikte Syntax. Wenn man aus Versehen bösartige Direktiven in der .procmailrc platziert, so landen die Mails alle in einer großen Datei, die keine MBox ist. Ein Beispiel:

:0
br />^X-BeenThere: list@youfavouritehost\.com$
.Path.To.Your.Imap.Box/
Nun landen alle Mails in einer großen Datei »^X-BeenThere«, kurz gesagt: Schmerz im Hintern. Heute wunderte ich mich über die leer bleibende Inbox. Ein ganzer Tag ohne komische Linux-User die komische Dinge fragen, Leute ohne Freunde, die welche suchen und andere Spammer? Kann nicht sein. In das Logfile von procmail geschaut und obiges Problem entdeckt. Ruby sollte mein Freund sein, das wieder zu beheben:
#! /usr/bin/ruby
a = 0
File.new( “heap_of_mails” ).read.split( /\n\nReceived: / ).each |m| {
a = a.next
File.new( “outputdir/” + a + “_mail.txt”, File::CREAT|File::TRUNC|File::RDWR, 0640 ).write( “Received: “ + m )
}
Nachdem die Mails aus der Datei gebastelt wurden, kann man sie nun wieder über procmail ganz normal in die Mailsammlung einpflegen:
for file in outputdir/_mail.txt ; do cat $file | procmail ; done

Filed under , & no comments & no trackbacks

Rails 1.1.0 1

Ich bin ja nun wirklich nicht so der Rails-Experte finde es aber trotzdem nicht nur cool (meistverwendetes Buzzword im RoR-Vortrag auf dem Webmontag gestern), sondern arbeite schlichtweg ab und an recht gerne mit diesem Framework. Mehr nicht. Einige der Dinge in Version 1.1 hören sich sehr praktisch an, so dass das integrierte Scaffolding nun endlich keine Injections mehr zulässt (nicht probiert, steht nur im Changelog), auch die Updates der JS-Bibliotheken scheinen mir sinnig und dass ich nun Unit-Tests auch direkt in den Source schreiben kann, erleichtert einiges. Auch wenn mir letzteres aus der Perspektive guten Software-Designs nur halb gefällt, ist es doch praktisch das für »ich mach ma flott was« schon in Ordnung. Für mehr taugt Rails wahrscheinlich eh nicht.
Version 1.1.0 gibt es jetzt alternativ zu 1.0 auf Schokokeks.

Filed under , , , , & one comment & no trackbacks

Diamantenjagd 7

Ruby on Rails LogoAußer dass ich endlich die PHP 5.1er Reihe PHP 5.1.2 inklusive hardened-Patch auf Schokokeks eingeführt habe, habe ich mir endlich mal Ruby noch ein bisschen genauer angesehen. Das ist schon eine furchtbar geniale Sprache.


Continue reading "Diamantenjagd"

Filed under , , , & seven comments & no trackbacks