opensource hardware in nz
Tuesday, May 27, 2008
Came across this site that stocks some really fun looking hardware. Of particular note is stocking only opensource-capable routers, laptops without an OS (no M$ tax!) and single-board computers that use only 5-7 watts that you could really have some fun with. Oh and who could pass up a GSM modem with built-in GPS? Oh, the gadgetry and geekyness.
nicegear.co.nz
jni - call C/C++/Assembly from Java
Friday, May 23, 2008
- Create Java class containing native method(s) (static or instance) defining interface with C code.public class Processor {
public static native double process(double x, double y);
} - Compile to Java class file.
- Run javah -jni [-o path/to/CProcessor.h] Processorto generate C header file from the Java. Eg. CProcessor.h:/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Processor */
#ifndef _Included_Processor
#define _Included_Processor
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Processor
* Method: process
* Signature: (DD)D
*/
JNIEXPORT jdouble JNICALL Java_Processor_process
(JNIEnv *, jclass, jdouble, jdouble);
#ifdef __cplusplus
}
#endif
#endif - Create a .c C source file - this is the stub to call the target code. Include header
and "Processor.h". The angle brackets mean it is registered as a library file for the compiler, whereas the speech marks denote a stand-alone header file. May need to include the path the Processor.h within the speech marks. Here is a simple example that just sums the two input numbers, rather than calling any other C: #include <jni.h>
#include <math.h>
#include "../headers/CProcessor.h"
#include <stdio.h>
JNIEXPORT jdouble JNICALL Java_Processor_process
(JNIEnv *env , jclass obj, jdouble val1, jdouble val2) {
return (val1+val2);
} - Run (eg for linux)gcc -shared -I /usr/lib/jvm/java-1.5.0-sun-1.5.0.15/include/ -Ito create the library file (indicated by 'shared' flag). Any compiler errors about missing .h headers should be solved by the inclusion (-I) of the jni and jni for linux paths.
/usr/lib/jvm/java-1.5.0-sun-1.5.0.15/include/linux/ -o CProcessor.so CProcessor.c - Either in a method inside original Java class, or in a new calling class, create a static block to either load (System.load()) the .so library via its file path, or load (System.loadLibrary()) a registered library (e.g. dll on Windows) via system-specific addressing.
- Once the static block has loaded the library, the methods are available either statically or from an object as determined in 1.public class Caller {In this case our Processor in Java calls the CProcessor in C which adds the 2 doubles we passed it and returns a double. Here it is 2 + 3 with the output 5.
static {
System.load("/home/me/_projects/JNI/C/CProcessor.so");
}
public static void main(String[] args) {
double result = Processor.process(2, 3);
System.out.println(result);
}
}
printing from Windows virtual machine on linux host
Thursday, April 03, 2008
It's great to be able to use a printer that you have defined in your linux host from within virtualised Windows guest OS (where the printer is either connected directly to your box or over a network). This post assumes you've already setup networking between your guest and host.
Firstly in your printer settings on the host, make sure access hasn't been restricted (open access by default on Ubuntu).
Find the name of the printer that you want to access (ie not the Description which is what appears in network browsing)
Then on the virtual windows machine (for XP):
Choose Add Printer
Select network printer
Select Printer on Internet
For the url, use:
Should be away laughing!
This howto is has more direct CUPS fiddling than is needed for Ubuntu now, but I got key information from there to find out the above.
run a open-source virtual machine with qemu
I use the awesome qemu open-source processor emulator, that lets you run virtual OSs (guests) on a host computer (on my Xubuntu box, in my case). Qemu has the power to have guest os's think they're on a box with entirely different processor types (eg have a PowerPC guest on a x86 box). But what I've found awesome is that with the relatively new Kernel Based Virtual Machine (aka KVM) on linux, with a supported processor, virtualisation goes right down to the cpu level - so it's fast!
If you're on linux and want to check if your processor does support it, use:
set ear file's context root in WebSphere
Thursday, March 27, 2008
WebSphere lets you set the context root of a war file directly on page 1 of the update screens. ear files are different however:
Turns out when Updating an ear, you need to select on the first screen to show all build/config options.
Then skip to step 8 'Map context roots for Web modules'. There the context can be specified.
managing networks without gnome
Wednesday, March 26, 2008
Having Xubuntu on my machine, gnome's Network Manager is an option, but I try to avoid the bloat of gnome. A great alternative (which many say is better) is wicd. It successfully scans for wireless networks, handles WEP connections, and is the only way I could find to connect to WPA/WPA2 networks. Great product. (howto)
keep grub boot switches between kernel updates
Thursday, March 06, 2008
I have been frustrated on numerous Linux systems where I have to use custom boot switches in Grub, so add them to the relevant entry in /boot/grub/menu.lst, but then a new kernel version (which adds a new boot entry) then doesn't have this new option. This has been simply annoying in the past because I've had to go back in and add the text to the entry. But now I've built a machine for a paying customer and I can't go round every time there's a kernel update!
It turns out the solution is simple:
Between the flags
### BEGIN AUTOMAGIC KERNELS LIST
and
## ## End Default Options ##
are settings that the updater will read in order to create the new boot entries.
The one we want is
While I'm on the topic, another useful setting in this set is
lock hibernate session to avoid lazy loading exceptions in tests
Tuesday, March 04, 2008
//Lock this hibernate session so we don't get lazy loading exceptions
SessionFactory sessionFactory = (SessionFactory) appContext.getBean("sessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
ssh tunnels
Thursday, January 17, 2008
Using ssh we can create tunnels to route bi-directional traffic. Depending on our selection of destination and server computers to connect to, the complete tunnel is made of a fully secure ssh tunnel, and possibly an additional tunnel continuing on that is not secure. The examples below should help explain this.
Here's the command:
ssh -NL localport:targethost:hostport server [-p serverport]
Where
- -N means don't start a remote ssh session (useful if just port fowarding)
- -L indicates a tunnel is to be set up with the following params:
- localport: tunnel 'entry'. Port number to connect to locally (ie at localhost)
- targethost: tunnel 'destination'. Which computer (name or ip) to connect to
- hostport: tunnel 'exit'. Port number to appear as on the other side of the tunnel.
- server: computer to validate ssh connection on (needs to be running ssh server). This forms part of the tunnel and three variations of this are explained below. You can make this 'user@server' in order to specify a username for the authentication if required.
- serverport: this is optional, but if you want to create the ssh connection over a non-standard port (ie not 22) then use this. You'll need to get the sshd listening on the new port in this case.
Example 1, target computer is running ssh server:
ssh -NL 8081:192.168.1.4:8090 192.168.1.4
Create a tunnel from 8081 locally to 192.168.1.4, and appear there at port 8090. Validate at 192.168.1.4. This is a short tunnel, between just the client and host, but completely secure. All traffic in this case will be over port 22 (ssh default).
Example 2, host computer is running ssh server:
ssh -NL 8081:192.168.1.4:8090 localhost
Exactly as in example 1, only this time the local machine is both ssh server and client. There is effectively no secure tunnel here (ie it's only between ports on the local machine), so unsecure over port 8090 to 192.168.1.4.
Example 3, 3rd computer is running ssh server:
ssh -NL 8081:192.168.1.4:8090 192.168.1.5 -p 3389
In this case, locally we are the ssh client, connecting securely (over port 3389) to the ssh server, 192.168.1.5. From there traffic travels over port 8090 to the destination box, 192.168.1.4. This is very powerful as traffic can be securely routed into a limited-access network. For example in this case if Microsoft's Remote Desktop Protocol (RDP) was the only traffic allowed in and out (eg through VPN), then the client externally can run this command, connect through on the RDP port (3389), connecting to a box inside the network (192.168.1.5). From there the destination IP is resolved, so this needn't be visible from the client computer, and traffic flows freely inside the network over 8090.
Another option is remote tunnels (instead of local tunnels). These use essentially the same commands, but with -R in place of -L. With a remote tunnel it is possible to declare a port on a foreign machine as the tunnel entry.
A nice way to test your tunnel is to use netcat. For the above examples this test would be:
On 192.168.1.4:
nc -l -p 8090
listen (-l) on port (-p) 8090 - end of the tunnel
On localhost:
nc localhost 8081
connect to localhost on 8081 - start of the tunnel
The localhost connection should get tunneled to the remote box and you'll be able to type at either end and see it appear in the terminals.
gtk dialogs in linux
Wednesday, December 05, 2007
Bash scripts are fantastic. I could end the post there, but they can be made even better for end users by using one of the simple GUI applications out there that can be run from bash. Here's a great little example (using gtkdialog)that pretties-up the interface for my infrared phone script from this post.
OUTPUT=$(/usr/local/bin/phone-connect $1 | sed -e 's/\(.*\)$/
Beautiful