Tuesday, September 04, 2007

Ruby Java Bridge

Against better judgement.... I just wrote some not so portable ruby using RJB, ruby java bridge, rjb.rubyforge.org, first I made a jar that contained little java class that I wrote for some LDAP testing(I needed to use java because of some SSL certificates contained in JVM), then I wrote following ruby in small rails app that displays the mess in a web app, pretty cool:

require 'rjb'
Rjb::load('/Users/villem/rails/ldaptester/script/ldaptester.jar', [])
lr = Rjb::import('LDAPreader')
fetcher = lr.new
result = fetcher.goForIt(params[:ldapstring], params[:server])
render_text result

Both on Mac OS X and on CentOS I had some trouble install RJB gem, error kept saying:
checking for jni.h... no
*** ext/extconf.rb failed ***
there were 2 problems, JAVA_HOME needs to be set, on OS X JAVA_HOME is set by default, however there is no include dir under the default Java dir, so I created a symlinked folder called include that has jni.h etc in it. Second problem had to due with the fact that I didn't have gcc installed and error message about jni.h really led me astray.
Anyway on centOS I simply did
sudo yum install gcc
and this fixed the problem.

Friday, June 22, 2007

Compiling and Running Windows programs on Ubuntu

For a while I had been wanting to get my company's products to compile and run on linux and since they are written in QT and M$ VS C 6 it was somewhat of a challenge. Here are few tips that might help somebody else on the same road. I did this on ubuntu 7.04 but probably most of this is not very linux distro specific, so give it a shot. Most of my help came from this slightly old doc, so I am not going to repeat everything contained in this doc by J. Grant:
http://codingstyle.com/articles/using-ms-vcpp-with-gnu-wine.html

1.First make sure you got latest latest wine, what threw me off was that wine version doesn't use correct decimal numbering i.e. 0.9.09 is called 0.9.9 so it came before for example 0.9.10. Anyway you can get latest 0.9.38 that I used from here:
http://wine.budgetdedicated.com/apt/pool/main/w/wine/

2.Now copy all the MS VC6 files as well as QT files if you need them from you windows machine to your linux box, folders you should need are called Common, VC98 and something like qtdll/3.0.5(or whatever QT you have). Puth them under .wine/drive_c where ever you like but
remember the path for step 5.

3. From your windows XP machine copy MSVCRT.DLL to the ubuntu box default wine system32 folder at:
~/.wine/drive_c/windows/system32

4. On ubuntu command line run
winecfg
and go to libraries. Add MSVCRT.DLL as native override.
ALSO CHANGE OS EMULATION TO WINDOWS XP

5. PATH IS VERY IMPORTANT TO GET RIGHT
From ubuntu command line run
wine regedit
navigate to
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
add the PATHs from step 3 following, for example in my case path is as follows:
c:\windows\system32;c:\windows;d:\nexaclient\qtdll\3.0.5\bin;d:\vs\common\msdev98\bin;d:\vs\vc98\bin

6. now try running
wine nmake
and make sure you get something like
NMAKE : fatal error U1064: MAKEFILE not found and no target specified
Stop.
so you know nmake is running and in the PATH. Check step above it's not working.

7. For some reason wine DOS shell called from nmake .mak script could not run the QT moc command, so I did via following little RUBY script to be run inside the directory with the QT headers in it:
headers=`ls *h`
headers.each do |header|
puts `wine moc #{header.chop!} -o moc_#{header.sub!('.h','.cpp')}`
end

8. Export ENV variables and call wine nmake, so for example in my case from ubuntu shell I issued:
export INCLUDE="z:\root\vs\vc98\include;z:\root\vs\vc98\mfc\include;z:\root\vs\vc98\mfc\src;z:\root\vs\vc98\atl\include;z:\root\nexaclient\qtdll\3.0.5\include;z:\root\nexaclient\qt\viewer\Common"
export LIB="z:\root\vs\vc98\lib;z:\root\vs\vc98\mfc\lib;z:\root\vs\vc98\mfc\src"
export QTDIR="z:\root\nexaclient\qtdll\3.0.5"
You can also put these vars into a script that also calls the nmake so you don't have to keep retyping them.
I also changed my .mak script to default to RELEASE and now when I call
wine nmake mymak.mak
I will get MYEXE.EXE in Release folder under the directory with the code

9. After this running the EXE was simple as in the correct DIR calling:
wine myexe.exe
and Viva La FSM! it works!

10. Next I also the great free installer generating program, Innosetup
http://www.jrsoftware.org/isinfo.php
this one was as simple as running the installer, after downloading to ubuntu box simply called:
wine setup.exe
and remarkably, it all just works! (or at least it did after a long night....)

Hopefully this helps somebody to get going a little quicker.
GLUCK!

Wednesday, April 04, 2007

emailcases - project management ajax web app


Emailcases is a new web based project management application that will allow emailing of new cases/projects that I just started working on, its still definetely a work in progress but I uploaded the code to google project hosting and GPLd it, here it is:
http://code.google.com/p/emailcases/

Sometime in near future I'll have version running at:
http://www.emailcases.com


Friday, March 23, 2007

JMS in Ruby using Stomp

I have been looking for ways to build asynchronous message driven systems in Ruby for a while, rinda works well, but it's nice to tap into the whole JMS universe, so I just gave a try to stomp,
http://stomp.codehaus.org/Ruby+Client
IMO last missing piece in the ruby ecosystem. Sending text messages via Apache Activemq works great, here are the steps:

1. download activemq
http://activemq.apache.org/activemq-411-release.html
and run
C:\java\apache-activemq-4.1.0-incubator\bin>activemq.bat

2. go to new command line and type
gem install stomp

3. type up following 2 apps from http://svn.codehaus.org/stomp/ruby/trunk/bin/ :
send.rb:
# Copyright 2006 LogicBlaze Inc.
begin; require 'rubygems'; rescue; end
require 'stomp
begin
@port = 61613
@host = "localhost"
@user = ENV["STOMP_USER"];
@password = ENV["STOMP_PASSWORD"]
@host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != nil
@port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != nil
@destination = "/topic/default"
@destination = $*[0] if $*[0] != nil
$stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
@conn = Stomp::Connection.open @user, @password, @host, @port, true
$stderr.print "Sending input to #{@destination}\n"
@headers = {'persistent'=>'false'}
@headers['reply-to'] = $*[1] if $*[1] != nil
STDIN.each_line { |line|
@conn.send @destination, line, @headers
}
rescue
end


cat.rb:
# Copyright 2006 LogicBlaze Inc.
begin; require 'rubygems'; rescue; end
require 'stomp'
begin

@port = 61613
@host = "localhost"
@user = ENV["STOMP_USER"];
@password = ENV["STOMP_PASSWORD"]

@host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != nil
@port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != nil

@destination = "/topic/default"
@destination = $*[0] if $*[0] != nil

$stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
@conn = Stomp::Connection.open @user, @password, @host, @port, true
$stderr.print "Getting output from #{@destination}\n"

@conn.subscribe @destination, { :ack =>"client" }
while true
@msg = @conn.receive
$stdout.print @msg.body
$stdout.flush
@conn.ack @msg.headers["message-id"]
end

rescue
end


4. Open second console and run
ruby cat.rb

5. ruby send.rb

6. Type up messages and see them arrive, that was easy!!!

Wednesday, February 07, 2007

Ruby Rails Cyclomatic Complexity analysis

Cyclomatic Complexity analysis is very useful in figuring out where to refactor code; what parts of the code need most clearing up, what code should be broken into new classes and methods etc.
Analyzing complexity of ruby applications is very easy with the cool app by Zev Blut, Saikuro, it uses ruby-lex to analyze the complexity, mainly by counting IF/AND etc statements per method.

I ran it for my ajax stock trading rails demo app, nexavibes, interestinly due to the rails MVC architecture and other rails default use of lot of patterns without having spent much effort on this rails application in refactoring, it comes out pretty well by these metrics. Since this app includes lot of javascript code that I wrote from scratch, that javascript would have some higher complexity numbers, however saikuro only analyzes ruby code for now.
If you know of a free open source complexity analysis tool for javascipt or C/C++, leave a comment.
But, anyway, here's the results that I got:

http://nexavibes.maanpaa.com/saikuro/index_cyclo.html
http://nexavibes.maanpaa.com/saikuro/index_token.html

Thursday, January 11, 2007

Modifying author property in subversion using svn propset

In case you use multiple user names in subversion and you make a mistake and hence have to modify author property,here's the command:
svn propset svn:author 'vmaanpaa' -r53626 --revprop
but first, make sure that in you repository, i.e.
/var/repository/hooks
you have something like:
[root@svn hooks]# cat pre-revprop-change
#!/bin/sh
REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"

also, this file needs to be executable by apache.

Thursday, November 09, 2006

Ruby Starfish - Easy Distributed Computing

Today gave a try to starfish, (relatively) easy set up of distributed client and server, so if you are planning no taking on Google with the motto of "Do Only Evil", this is the easiest way to distribute the computation on your redundant array of commodity hardware.
Anyway, on ubuntu ran no problem right out of box(as long as you have active-record gem installed), but some problems on windows as usual.
Simple fix to win32-process.rb 0.5.1 fixes the problem, just find the line
Win32::Process
and change it to just
Process
on my machine that file could be found at
c:/ruby/lib/ruby/gems/1.8/gems/starfish-1.2.0/lib/starfish.rb
now sample starfish foo.rb app should run fine.
If other gems are missing etc, just install them of course.