Welcome

June 9th, 2009 neo2k No comments

Welcome to the website of Dennis Hedegaard, computer entusiast and open source supporter.

This blog is the center of the webserver, which hosts gitweb repositories, some abandoned svn repositories and random public files.

Categories: My Life Tags:

A little logging framework

January 27th, 2010 neo2k No comments

One of the great things about java was the logging framework under java.util.logging, it was simple, extendable and efficient. I’ve tried to take those properties and implement a logging framework for the .net platform in c#, for now it features a logger with some logging level, these determine if a log event should get filtered, there are the usual: fine, info, warning and severe. For the actually logging you implement something called a handler with a Write method, this method takes a log event with a time, level and message.

This is very similar to java’s implementation, but why change something that works well ?

I’ll be updating it with features (new ready-made handler implementations, info about the method that made the logging etc) and tweaks from time to time, for now it works.

The DEFAULT_LOGGER on the Logger class can be used to log to stderr, without having to create a logger and implement your own handler.

Feel free to try it, feedback is welcome.

git: http://git.neo2k.dk/?p=cs/logging.git;a=summary

dll: http://pub.neo2k.dk/logging/

Categories: Languages Tags: , ,

C# and XML

January 26th, 2010 neo2k No comments

I’m used to the java way of xml, which is using StAX. In C# there are some default namespaces for xml, these are:

  • System.Xml
  • System.Xml.Schema
  • System.Xml.Serialization
  • System.Xml.XPath
  • System.Xml.Xsl

For this post I’m only going to use 2 classes from the System.Xml namespace, namely XmlTextWriter and XmlTextReader. These can be used to read and write xml in a way very close to what I’m used to in StAX.

The writer works just as you’d expect, there’s a WriteStartDocument() for witing the xml header, WriteStartElement() for writing start elements, WriteAttributeString() for writing attributes to elements. When closing elements simply used WriteEndDocument() and WriteEndElement().

For the reader there’s a bit of a iterative touch to it, just as with StAX, for instance you usually have an outer while loop, which iterates on Read(). Whenever read is true, you can switch the result with a XmlNodeType. To get elements simply use the Element type, to get an end of an element use EndElement. When inside an element with attributes you go through the attributes with a for loop, counting to the number of attributes, the number is the AttributeCount property on the reader. You move the reader through the properties with the method MoveToAttribute(int i), where i is the number of the attribute (starting from 0 of course).

The reader has a Name and Value property, these change depending on what element the reader currently is at, if the current element is an attribute, then the name and value is the name and value of that attribute. For elements the element name is Name.

Here’s a simple example using this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace xmltest {
    class Program {
        static void Main(string[] args) {
            XmlTextWriter writer = new XmlTextWriter(new StreamWriter("test.xml"));
            writer.WriteStartDocument();
            writer.WriteStartElement("users");
            writer.WriteStartElement("user");
            writer.WriteAttributeString("name", "svend");
            writer.WriteAttributeString("pass", "1234");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            System.GC.Collect();
            Console.WriteLine("all done.");
            XmlTextReader reader = new XmlTextReader(new StreamReader("test.xml"));
            while (reader.Read()) {
                switch (reader.NodeType) {
                    case XmlNodeType.Element:
                        Console.WriteLine("start: {0}", reader.Name);
                        if (reader.AttributeCount > 0)
                            for (int i = 0;i < reader.AttributeCount;i++) {
                                reader.MoveToAttribute(i);
                                Console.WriteLine("attrib: {0} - value: {1}",
                                    reader.Name, reader.Value);
                            }
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine("end:   {0}", reader.Name);
                        break;
                }
            }
            reader.Close();
            Console.WriteLine("done");
            Console.Read();
            File.Delete("test.xml");
        }
    }
}

The code makes a users element, with a user element with 2 attributes (user and pass). The reads it back, since the user element is empty, it does not contain an end element.

i’ve heard you can dump stuff from ado.net to xml, read entire xml documents with XmlDocument and XmlDataDocument. I suspect the System.Xml.Serialization namespace contains some way to serialize objects to xml, but I’ll have to test that some other time.

Categories: Languages Tags: ,

C# and lambda expressions

January 20th, 2010 neo2k No comments

Since the next semester will be spend learning some C#, I spend some time researching the language. I came across the term lambda expression, which in terms i an anonymous function or method, it is very similar to lambda calculus which is a way of expressing anonymous functions.

Some of the simpler ways to use these expressions are something like this:

first of we define a delegate, for instance:

delegate int multiply (int a, int b);

an example (static) implementation in lambda expression would be:

multiply multi_impl = (int a, int b) => a * b;

Such uses can be a bit dull, lambda expressions lack recursive behavior, much like lambda calculus is though to do, but using the generic Func class in System.Core one can create a Func object which references a method of itself thereby creating recursion. Here’s an example of a fibonacci calculation:

First the recursive part:

Func<long, object, long> fibonacci_impl = (i, func) => i == 0 ^ i == 1 ? i :
    ((Func<long, object, long>)func)(i - 2, func) +
    ((Func<long, object, long>)func)(i - 1, func);

The object in the middle is the function itself, however since it cannot reference itself explicit we do it this way and start the recursion from another expression, which can be found below:

Func<long, long> fibonacci = i => i < 0 ? -1 : fibonacci_impl(i, fibonacci_impl);

It is important to notice that the fibonacci method only takes one argument and starts the function above that one with the integer and the function itself, moreover the casts from object to fibonacci_impl is necessary since fibonacci_impl is unable to reference itself in the expression.

I some benchmarking comparing it to a normal recursive function, and the normal way is faster. I realize that lambda expressions aren’t meant for recursion just yet, but is another way to minimize code when implementing anonymous methods. It still fun to mess with however =)

For more on lambda calculus: http://en.wikipedia.org/wiki/Lambda_calculus

Under the “recursion and fixed points” section there is the same way of doing recursion, but in lambda calculus. And as is written:

“In lambda calculus, one cannot define a function which includes itself. To get around this, one may start by defining a function, here called g, which takes a function f as an argument.”

Benchmark of datastructures in c++

October 30th, 2009 neo2k No comments

Here are some test results from a simple benchmark using vector, list, queue and stack in the c++ stl:

The benchmarks are done on 40.000.000 structs with a string and an int.

With aggressive optimizations (-march=native -funroll-all-loops -fomit-frame-pointer -finline -O9 -ffast-math):

$ ./stacktest
5.59 seconds
$ ./linkedtest
8.05 seconds
$ ./queuetest
5.51 seconds
$ ./vectortest
10 seconds

Without aggressive optimizations:

$ ./stacktest
8.3 seconds
$ ./linkedtest
12.84 seconds
$ ./queuetest
7.82 seconds
$ ./vectortest
12.89 seconds

What’s noteworthy is that a queue and a stack is pretty close in performance, compared to a vector and list.

The code for the project is on git for now:

http://git.neo2k.dk/?p=sum_benchmark.git;a=summary

I’m using cmake for makefiles :>

Categories: C/C++, Languages Tags: , , ,

HTTP server continued..

September 30th, 2009 neo2k No comments

The http server now features around 10-15 classes, with a nice structure.

The logging system is done and is rather flexible. It can write to streams and files, using observer pattern. The newline has been changed to <CR><LN> instead and MIME is now provided from 3rd party (with a few bugs).

The new big thing is a configuration file in xml, it is parsed through StAX, which is a “Streaming API for XML”, since I’m not too farmiliar with it at the moment, the code for that will probably be optimized alot later.

The parsing of the xml can be found on my git, this is the Config class from a few minutes ago, look in the parse method:

http://git.neo2k.dk/?p=http.git;a=blob;f=src/http/config/Config.java;h=eaa9608e5691e65c7ff1ad7eacd9ed4674b88214;hb=HEAD

Categories: Java Tags: , ,

HTTP server in java

September 24th, 2009 neo2k No comments

It’s been a while since my last post, but finally I felt the urge to post again. I’ve started implementing parts of the http protocol in java using the java tcp/ip stack.

For now it can handle 200, 404 and Content-Type in headers. What I’d like to see in the future is thing like Encoding the header, dynamic Content-Type from a config file, Date in header and a config file for public_html, error_html, server port and so on.

Anyways, here’s a link to the git repo if you want to mess around:

http://git.neo2k.dk/?p=http.git

Categories: Java Tags: , , ,

Disabling the power off button via ACPI

September 8th, 2009 neo2k No comments

This is how to disable the power off button on debian lenny, when the power off button is pressed, a script is run in: /etc/acpi/powerbtn-acpi-support.sh

To disable, simply comment out the shutdown line in that bash script.

Categories: Debian Tags: ,

Sierpinski javabean

August 28th, 2009 neo2k No comments

JavaBeans are use to modify classes in a builder application, like netbeans, jbuilder og VE for eclipse. I decided to try something a bit interesting with an old project which can be found here: http://java.neo2k.dk/sierpinski/

The interesting part was, it was extremely easy to do, all I had to do was add some propertychange support and code a listener interface and event class. The library can be found here: http://java.neo2k.dk/sierpinski-bean/

This library is now used in the sierpinski app, moreover a git repo for the library can be found here: http://git.neo2k.dk/?p=sierpinski-bean.git

Categories: Java Tags: ,

RMI on linux

August 25th, 2009 neo2k No comments

RMI is a java specific technology, where you can execute a method across processes, and even across networks. It works in a way, where a stub (as sort of a proxy) is generated from an object. The methods in this object in defined in an interface, which extends java.rmi.Remote. The methods which are able to execute remotely, all throws java.rmi.RemoteException.

RMI can be nice for quiet a few things, however if you constantly get a Marshalling exception, it’s probably because you forgot to define the property called java.rmi.server.codebase in the jvm, men starting the server part of the RMI.

It is usually defined in the following way:

$ java -cp bin -Djava.rmi.server.codebase=file:`pwd`/bin/ server

Or that’s the way I do it, keep in mind that the codebase can point to a jar and may use other protocols such as http, ftp etc. It is important to notice, that the codebase url for the file protocol has to be the complete path. This is why I pwd here.

I have yet to find a nice way to do this in ant, however there’s a jvmargs tag and a sysargs tag, these can be used but it’s a problem to get the workdir from within ant.

Categories: Java Tags: ,

Setting default username in eclipse

August 25th, 2009 neo2k No comments

This is how you set the default user name in eclipse, it’s annoying to change your username to your real name:

$ ./eclipse -vmargs -Duser.name="Mr awesome"
Categories: Uncategorized Tags: