Deep-blue

Sunday, December 13, 2009

My First Lisp Program

I wrote a simple timecode generator for testing of the Video Annotation and Reference System in Groovy. The program listens for requests on a UDP port and responds with a String formated as a timecode. For grins, I converted the program to Clojure

Groovy

#!/usr/bin/env groovy

/**
 * Script for simulation of a udp VCR
 */

def port = (args.size() > 0) ? Integer.valueOf(args[0]) : 9000

println("Localtime timecode generator attached to port ${port}")

 
def socket = new DatagramSocket(port)
def buffer = (' ' * 4096) as byte[]
while(true) {
    def incoming = new DatagramPacket(buffer, buffer.length)
    socket.receive(incoming)
    def c = new GregorianCalendar()
    def f = c.get(Calendar.MILLISECOND) / 1000.0 * 29.97;
    String reply = String.format('%tT:%02d', c, Math.round(f))
    outgoing = new DatagramPacket(reply.bytes, reply.size(),
            incoming.address, incoming.port);
    socket.send(outgoing)
}

Clojure

(import 
  '(java.net DatagramPacket DatagramSocket)
  '(java.util GregorianCalendar Calendar))
  
(defn fmt-time 
  "Format a java Calendar as a timecode"
  [cal] 
  (let [frames (* (/ (. cal get (Calendar/MILLISECOND)) 1000) 29.97)]
    (format "%tT:%02d" cal (Math/round frames))))
  
(defn run-generator 
  "Function for simulation of a udp VCR"
  [port] 
  (println (str "Localtime timecode generator attached to port " port))
  (let [socket (new DatagramSocket port)
        buffer (make-array (Byte/TYPE) 4096)]
    (while true 
      (let [incoming (new DatagramPacket buffer 4096) 
            reply (fmt-time (new GregorianCalendar))]
         (. socket receive incoming)
         (let [outgoing (new DatagramPacket (. reply getBytes) (. reply length) (. incoming getAddress) (. incoming getPort))]
           (. socket send outgoing))))))
  
 (run-generator (Integer/parseInt (first *command-line-args*)))

Thursday, December 03, 2009

Eclipse template for quickly inserting a SLF4J logging statement

${imp:import(org.slf4j.LoggerFactory)}
private final ${type:newType(org.slf4j.Logger)} ${log:newName(org.slf4j.Logger)} = LoggerFactory.getLogger(getClass());${cursor}

Deleteing all the files Memeo scatters across my hard drive using Groovy

(new File('.')).eachFileRecurse { file -> if (file.isFile() && file.name.startsWith('.Memeo ')) {file.delete()} }

Monday, June 01, 2009

Maven and Google Code

I have a few projects I keep on Google Code. Since I do quite a bit of work using Maven and Java, it's nice to be able to use the subversion repo on Google Code to host my maven repo and my javadocs. Notes for hosting a Maven repo can be found at http://www.jroller.com/mrdon/entry/find_of_the_day_wagon and Javadoc details are at http://stuffthathappens.com/blog/2007/11/09/howto-publish-javadoc-on-google-code/

Blog Archive