Monday, July 25, 2011

grails - export plugin, at dynamically more attributes

What is this about?

currently I'm writing on a little tech study application, called times. Which can be accessed here

This application is a very simplistic timetracker, but allows me to play with some api's I normally don't use or to simply test some grails functions.

Right now I had a simple idea.

I want to export all my data, using the grails export plugin, without cluttering my database model with dozens of attributes, which will never be queried. I also did not want to create pojo's, just for the export. Instead I decided to play a bit with the EMC MetaClass again and see if, we can do this on the fly.

Example:



def exports = []

def fields = ["name","organization","project","beginDate","endDate","timeSpend"]
def labels = ["name":"Name","organization":"Organization","project":"Project","beginDate":"Start","endDate":"Finished","timeSpend":"Durations (s)"]
Task.list().each {Task t ->
if (t.endDate != null) {
t.metaClass.timeSpend = (t.endDate.time - t.beginDate.time)/1000

exports.add(t)
}
}
exportService.export(params.format, response.outputStream, exports,fields,labels, [:], [:])


this registers the attribute 'timeSpend' to the lifespan of the Task instance 't'. Which is only be required during the export.

The important line is



t.metaClass.timeSpend = (t.endDate.time - t.beginDate.time)/1000



Short, it can simplify your life a bit and reduces the need of introducing a pojo, just to export data or to permanently add this object to your database. Specially since it's a calculated value and can be regenerated on the fly.

Monday, July 18, 2011

openCl - not recognizing GPU, during remote logins

in the last couple of days I had some issues with my Ubuntu/Ati GPU based calculation units. They refused to execute any calculations, whenever I tried to use SSH to run them.

So a bit of googleling later I found the following informations in a hidden pdf:



a. Add the following lines at the end of /etc/gdm/Init/Default,
before the exit 0, to modify the security settings, allowing remote
sessions to access the X server and ensuring that remote sessions have
access to the necessary device files when communicating with the GPU:
xhost +
chmod uog+rw /dev/ati/card*
b. If you normally use bash, add the following line to the end of
/etc/bashrc file to ensure remote sessions know which X server to
access.
case $DISPLAY in ’’) export DISPLAY=:0;; *) ;; esac
NOTE: ’ ’ are two single quotes, not a single double-quot



Taken from:

http://developer.amd.com/sdks/AMDAPPSDK/assets/App_Note-Running_AMD_APP_Apps_Remotely.pdf

Now everything works fine and all is dandy again.

Sunday, July 10, 2011

Groovy HTTP Builder- fetching JSON data over post and supply parameters

Right now I'm playing around with the MtGox api to issue online transactions to buy and sell bitcoins. Yes I'm still obsessed with these and don't think this is going to change anytime soon, since It's starting to actually generate money.

But the big pain right now is that you have togo to so many different websites todo a simple transaction and I really want to have this all combined in one single tool.

So the api exspects from you to be queried over post arguments and the first thing which came to my mind was to use the fantastic HttpBuilder in groovy.

so let's define a simple method to simplify life a bit for us


private executeQuery(Map parameter, String path) {

def http = new HTTPBuilder("https://mtgox.com/")

def result = ""
http.post(body: parameter, path: path, requestContentType: URLENC) { resp, json ->

if (resp.statusLine.statusCode == 200) {
result = json
}
else {
result = false
}

}

return result


and a second method to actually call this


def getCurrentBalance() {

if (MtGoxAccessHandler.isConfigured()) {
def values = executeQuery([

name: MtGoxAccessHandler.getUserName(),
pass: MtGoxAccessHandler.getPassword()],
"/code/getFunds.php")

return [usd: values.usds, coins: values.btcs]
}
else {
return false
}
}



The class MtGoxAccessHandler is just a little helper, which stores the username and password and allows me to simplify the code a bit and easy testing.

This post is also related to the google code project DeepBitView

Monday, July 4, 2011

JSON and SSL in groovy, how to ignore certificates

In the last couple of days I became more and more interested in bitmining and the first thing I noticed was the utter lack of integrated tools and hence statistics I would like to associated with my miners.

This resulted in me creating a tiny grails application to synchronzie my deepbit statistics with the current market data from mtgox to calculate at which point it becomes pointless to mine these thing. I basically hope that this pays for the 700$ for my 2x6950 radeons and associated cooling the 100+F heat we have here in California.

Now originally you could assume that this is rather straightforward. After all you got json data, which just need to be parsed.


Map jsonArray = JSON.parse(new InputStreamReader(new URL("https://mtgox.com/code/data/ticker.php").openStream()))

println jsonArray



but sadly the java ssl security manager does not agree here with us and tosses a fit..



javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)



Now java is kinda smart, if want's to force you install a security certificate from the server you access and authorize yourself. But sometimes you don't want this. In this case it's overkill, since we are having a readonly transaction.

So what has to be done?

you need to create your own security manager implementation. Which basically is a 3 step procedure


  1. implement an interface


    class TrustManager implements X509TrustManager {

    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    return null;
    }

    public void checkClientTrusted(
    java.security.cert.X509Certificate[] certs, String authType) {
    }

    public void checkServerTrusted(
    java.security.cert.X509Certificate[] certs, String authType) {
    }

    }




  2. register the interface and update the context configuration

    TrustManager[] trustAllCerts = new TrustManager[1]

    trustAllCerts[0] = new TrustManager()
    try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }

  3. and continue like always with your json script

    Map jsonArray = JSON.parse(new InputStreamReader(new URL("https://mtgox.com/code/data/ticker.php").openStream()))


and the result should be now the current ticker for the USD to BitCoin exchange rate.


    {"ticker":{"high":15.4989,"low":13.31415,"avg":14.726503862,"vol":42862,"last":13.31415,"buy":13.331,"sell":13.35999}}