Connect Coldfusion and Ruby via Socket

This week a read a lot about ZeroMQ(zeromq.org). It’s a very complex and elegant way to connect applications via a message queue service. What I like most is the possible connection on a single host or over multiple hosts. The reason why I was interested in this topic was the search of an easy way to connect Coldfusion to ruby.

We have lot of code running on Coldfusion and we want to migrate a lot of complex parts from it to a more performant environment. On the other hand we want to keep the easy HTML-templating system Coldfusion is providing. This means to create a ruby library to be the base of the new system and having Coldfusion on top of it.

Part 1: Connecting Coldfusion and ruby

While reading the zeroMQ documentation I got the idea of trying the same approche of using the TCP socket in a simpler way. So I was looking for different Socket examples for Coldfusion. In contrast to the most available articles, I want to use Coldfusion as a Socket Client and ruby as a Socket Server.

drawing

Creating the ruby Socket Server

At first I started with the ruby server, because later I want that server to receive and return the data. For now I’m only fine with the transfer of text based data.

require 'socket'

def incommingMessage(client)
    data = ""
    incomming_length = 56
    while( tmp = client.recv(incomming_length) )
        data += tmp
        break if tmp.length < incomming_length
    end
    return data
end

def init()
    server = TCPServer.open(2000)
    loop {
        client = server.accept
        puts incommingMessage(client)
        client.puts "Hi from ruby, cya!"
        client.close
    }
end

init()

Start the server: ruby socket.rb

Creating the Coldfusion client

After having the ruby Socket server running I was concentrating on the Coldfusion client part. For that I downloaded and startet the Express Version of Railo 4.2, which can be found here: http://www.getrailo.org/index.cfm/download/.

private string function callSocket2( required string host, required numeric port, required string message ) {
    var result = "";
    var socket = createObject("java", "java.net.Socket");
    var outputStream = "";
    var inputStream = "";
    var output = "";
    var input = "";
    var inputStreamReader = "";

    try {
        socket.init(arguments.host, arguments.port);
    } catch(java.net.ConnectException error) {
        throw message="#error.Message#: Could not connected to host #arguments.host# on port #arguments.port#";
    }

    if ( socket.isConnected() ) {
        outputStream = socket.getOutputStream();
        output = createObject("java", "java.io.PrintWriter").init( outputStream );
        inputStream = socket.getInputStream();
        inputStreamReader = createObject("java", "java.io.InputStreamReader").init(inputStream);
        input = createObject("java", "java.io.BufferedReader").init(inputStreamReader);
        output.println(arguments.message);
        output.println();
        output.flush();
        result = input.readLine();
        socket.close();
    } else {
        throw message="Not connected to host #arguments.host# via port #arguments.port#";
    }
    return result;
}

#callSocket2('localhost','2000',"Hello there, it's CF")#

When having both scripts executed, the output should look like this.

Within Terminal:
ruby
Within the browser:
cf

view on github: https://github.com/Macagare/Ruby-CF-Socket

Getting into SASS

As a developer I see CSS as a language made for designers. I don’t get comfortable with this topic.

That’s why I gave SASS a try and I must admit, it’s easy to use. One can use variables, calculate and include different files easily. Here is a small example taken from the SASS website.

$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
	font: 100% $font-stack;
	color: $primary-color;
}

In cooperation with the Koala Editor one can use about every code edtior available. Koala supports the following languages: Less,Sass, CoffeeScript and Compass Framework.

Here are the TOP6 benefits:

  1. variables like php
  2. nesting like javascript blocks
  3. imports of external scss files
  4. mixins with functions
  5. inheritance of attributes
  6. calculations

What I like most is the nesting feature. The elements are easier to find and the SASS processing is cleaning up the mess. So one can concentrate on writing readable and maintainable style code.

Example:

// avoids duplicated code
.listAppeal {
  margin: 0;
  padding: 0;
  list-style: none;
}

body {
  nav {
    ul {
      @extend .listAppeal;
    }

    li { display: inline-block; }

    a {
      display: block;
      padding: 6px 12px;
      text-decoration: none;
    }
  }
}

When you have seen and used CSS before. Getting started with SASS maybe takes about 60 minutes. With the use of Koala all processed CSS files can be generated automatically after saving the scss files.

Have fun trying…

Getting Startet with Coldfusion and mxUnit

Unit-Testing is a often used wording in the current fields of coding. Many people hesitate to use the given tools. I also didn’t use it very often.

Here a simple “getting into” for mx unit

  1. go to http://mxunit.org and get the latest project zip
  2. extract the mxunit zip into your project /yourproject/mxunit
  3. setup done

Now let’s create a simple test as warm-up.

Somewhere within your project one creates a single [name]Test.cfm with this default content.
Snippet: https://gist.github.com/Macagare/1f51ce98ec65c74fd504

When you have done everything right it should look like this:

Screenshot 2014-05-23 09.47.33

That’s all. We’re done.

Have fun playing around more.

getting String filesize in Coldfusion

Sometimes one needs to get the filesize of a string. For example when one wants to write this string into an Amazon SQS where there is a maximum limit of 256KB.

private numeric function getStringByteSize(required string text) {
 return ArrayLen( arguments.text.getBytes("UTF-8") );
}

 

Flash & Google Analytics Trackevent

Das eventtracking von Flash zu Google Analytics gibt es ja schon eine ganze Weile. Leider hatte ich seit der Umstellung der Javascript API nicht mehr so viel Kontakt mit Google Analytics.

Nun mußte ich diese Funktionalität in ein aktuelles Projekt implementieren. Hierzu fand ich eine recht gute Dokumentation. Außerdem kann ich das Google Analytics plugin für den Chrome Browser überaus empfehlen. Ohne dieses plugin kann man sich bei der Fehlersuche grau suchen.

Hier der Einfachheit mein snippet für die Javascript-seitige Implementierung:

Jetzt braucht man nur noch “trackEvent()” über das External Interface aufzurufen und die Parameter zu übergeben. Aber Achtung, der letzte Parameter “value” sollte immer eine Zahl sein. Habe den typecast extra eingebaut, da ich bei einem Fall dort unbemerkt einen String gesendet hatte. Das führte dazu, dass die events nicht aufgezeichnet wurden. Konnte man im Debugger sehr gut sehen.

Links:

Mediawiki Probleme mit der Dateisuche

Ich hatte aktuell ein Problem mit der Dateisuche innerhalb des Mediawikis unter “Special:ListFiles”. Dieses tritt hauptsächlich auf, wenn man Version 1.15.x verwendet.

Es gab zwei mögliche Lösungsansätze:

1. Anpassung der SpecialListfiles.php
Es gibt in Version 1.15.x den Fehler, dass lowercase mit caseinsensitive verglichen wird.

$this->mQueryConds = array( "LOWER(img_name) LIKE '%{$m}%'" );

Entweder man wechselt zu caseinsensitive oder macht um das LIKE auch einen LOWER().

$this->mQueryConds = array( "LOWER(img_name) LIKE LOWER('%{$m}%')" );

2. Update der Mediawiki-Version auf größer 1.15.x, da dort dieses Problem schon behoben wurde.

$this->mQueryConds = array( 'LOWER(img_name)' . $dbr->buildLike( $dbr->anyString(),
strtolower( $nt->getDBkey() ), $dbr->anyString() ) );