Wednesday, 13 May 2015

Rust: network byte order

Languages on different architectures might serialize the 32-bit unsigned int 15 as the byte sequences 00 00 00 0F (big-endian) or 0F 00 00 00 (little-endian) but network protocols generally prefer the former.

pub fn to_network_byte_order(n: u32) -> u32 {
  n.to_be()
}

pub fn from_network_byte_order(n: u32) -> u32 {
  u32::from_be(n)
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn test_to_network_byte_order() {
    let expected: u32 = 0xFFAABBCC;
    let nbo = to_network_byte_order(expected);
    if cfg!(target_endian = "big") {
      assert_eq!(expected, nbo);
    } else {
      assert_eq!(expected.swap_bytes(), nbo);
    }
    let actual = from_network_byte_order(nbo);
    assert_eq!(expected, actual);
  }
}

u32 is a 32-bit unsigned integer; i32 is the signed version.

This post is just a code snippet written by someone getting started. No promises are made about code quality. Version: rustc 1.0.0-beta.4 (850151a75 2015-04-30) (built 2015-04-30)

Rust: UTF-8 byte array to String

Rust has a string type that mandates UTF-8 for strings but at some point you need to turn raw octets into structures you can treat as character data.

pub fn utf8_to_string(bytes: &[u8]) -> String {
  let vector: Vec<u8> = Vec::from(bytes);
  String::from_utf8(vector).unwrap()
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn test_to_string() {
    let bytes: [u8; 7] = [0x55, 0x54, 0x46, 0x38, 0, 0, 0];
    let len: usize = 4;
    let actual = utf8_to_string(&bytes[0..len]);
    assert_eq!("UTF8", actual);
  }
}

The utf8_to_string function turns some octets into a String. Note the lack of semicolon on the last line - this indicates that the line returns a value. The ampersands indicate that the function is borrowing the variable. Rust has strong opinions on ownership - expect compile errors if you do not understand this concept.

Rust comes packages with the Cargo build system. I've in-lined my unit tests to exercise the functions I write. assert_eq! is a macro. Note that I've tried out a slice in my test to truncate the trailing zeroes.

This post is just a code snippet written by someone getting started. No promises are made about code quality. Version: rustc 1.0.0-beta.4 (850151a75 2015-04-30) (built 2015-04-30)

Sunday, 10 May 2015

Rust: first look

Rust is approaching its 1.0 release.

Rust is a systems programming language that runs blazingly fast, prevents almost all crashes*, and eliminates data races.

* In theory. Rust is a work-in-progress and may do anything it likes up to and including eating your laundry.

Rust has been under public development for a number of years. Many search engine results will lead to obsolete code. That might include this post. I have tried to stick to APIs marked stable but verify against the official documentation. The documentation on www.rust-lang.org is currently incomplete but it is the best starting point.

Version info:

$ rustc --version
rustc 1.0.0-beta.4 (850151a75 2015-04-30) (built 2015-04-30)

How I approached getting started with Rust:

  • Decide on an application to write (a simple UDP test client)
  • Decompose it into a list of parts
  • Figure out how to do each one in turn

A laundry list of disjointed code samples follow in subsequent posts.

2015-05-13: post has been split up into multiple posts for easier reference; use the rust-lang tag to filter.

Wednesday, 6 May 2015

UML: authoring simple sequence diagrams

I had specific criteria for a UML sequence diagram generator:

  • Text-sourced
  • Simple
  • Off-line
  • Linux

seqdiag fits the bill. There is an interactive shell if you want to try it out before installing.

Monday, 27 April 2015

CentOS 6.6: redirecting SNMP traps from low ports to high ports with iptables

I have a Java application that processes SNMP traps. Traps arrive on port 162. Normally Java cannot listen on the low ports. You can use setcap to permit it or run as root but these introduce potential security issues.

I opted to have the Java process listen on port 1162. Changes will have to be performed as root.

Thursday, 12 March 2015

Java: handling null in getter method chains

Null is just something you need to deal with in Java. This post looks at the mechanism for handling chained method calls without causing a NullPointerException.

This post uses Java 8.

Sunday, 16 November 2014

Java: equals and hashCode performance

I wanted to benchmark KλudJe's equals/hashCode implementation against common alternatives. I used JMH 1.2 to measure performance.

Sunday, 10 August 2014

Java: easy equals/hashCode/toString (less boilerplate; no magic)

This post describes a way to implement the equals hashCode and toString methods with relative brevity using Java 8 and KλudJe.

Tuesday, 29 July 2014

I18N: dead links; Michael Kaplan's blog

Many of the links on this blog useful for Windows internationalization are dead as Microsoft has ceased hosting them. Some of the information may still be available here.

Tuesday, 22 July 2014

Java: arbitrary functional interfaces and checked exceptions in Java 8

The last couple of posts have covered how to handle exceptions with Java 8 functional interfaces. This post discusses how to handle arbitrary functional interfaces as of KλudJe 0.2.

Saturday, 19 July 2014

Java: lambdas, streams and IOException

The last post discussed exception handling with KλudJe in broad terms. This post presents a more practical example with the Stream type and I/O.

Sunday, 15 June 2014

Java: lambdas and easy checked exception handling with KλudJe

This post describes an approach to handling checked exceptions using Java 8 lambdas without adding try/catch blocks everywhere.

Wednesday, 5 March 2014

Sunday, 26 January 2014

Java: exceptions, error stories, and plot holes

Software should tell a good story about its errors. Failure to do this in complex applications can lead to hours or days of trying to puzzle out the cause of some seemingly random exception in the production logs.

Here are some tips for writing Java code that generates informative stack traces.

Wednesday, 8 January 2014

Java: taking jsonunicode to Maven central

Sonatype provide the means and instructions to promote open source software to Maven central. I opted to refactor a small library from a previous post on JSON and publish it as a library called jsonunicode.

Wednesday, 4 December 2013

Java: JSON to XML with JAXB and Jackson

Here's a small example demonstrating how to convert JSON to XML using JAXB data binding. You might want to use this over streaming where you want to operate on objects in an intermediary step. It is possible to perform the reverse operation but that is not demonstrated here.

  • Java 7
  • Jackson 2.2
    • jackson-core-2.2.0.jar
    • jackson-databind-2.2.0.jar
    • jackson-annotations-2.2.0.jar
    • jackson-module-jaxb-annotations-2.2.0.jar

Wednesday, 20 November 2013

Java: notes on IBM's multitenant v8 beta JVM

I thought I'd take a look at IBM's Java 8 beta multitenancy support. This is a mechanism for sharing runtime resources across Java virtual machine instances. See developerWorks' "Introduction to Java multitenancy."

I installed ibm-java-sdk-ea-8.0-0.0-x86_64-archive3.bin on Linux using a CentOS-6.4-x86_64-LiveCD.iso based host.

Saturday, 9 November 2013

HOWTO: create WebSphere AS 8.5.5 development workspace in Eclipse

This post is a follow-up to HOWTO: script WebSphere AS 8.5.5 internet install and describes the post-install steps to create a development workspace.

The clean install described in the previous post requires further configuration before you can start the server and deploy applications.

Wednesday, 9 October 2013

HOWTO: script WebSphere AS 8.5.5 internet install

This post describes how to perform a scripted install of a WebSphere Application Server (WAS) development environment from the internet. This is adequate if you want automated installation that will be repeated infrequently. Prior knowledge of WAS, Eclipse and Linux is assumed.

IBM has at least three offerings for basic Java EE development:

  • WAS for Developers - the full-fat commercial WAS server
  • Liberty Profile - a lightweight, developer-friendly server
  • Community Edition - a blue-washed Apache Geronimo

The scripts described here install the first two options with IDE tooling and Java 7. Eclipse Juno is the latest release with this level of WDT support.

Formally, the following are installed:

  • IBM WebSphere Application Server for Developers 8.5.5.0
  • IBM WebSphere SDK Java Technology Edition 7.0.4.1
  • IBM WebSphere Application Server Liberty for Developers 8.5.5.0
  • IBM WebSphere SDK Java Technology Edition Version 7.0 for Liberty 7.0.4.1
  • Eclipse IDE for Java EE Developers (Juno Sr2; Eclipse 4.2)
  • WebSphere Application Server V8.5 Liberty Profile Tools
  • WebSphere Application Server V8.5 Tools

Friday, 4 October 2013

Java: writing a MongoDB driver

I didn't set out to write a MongoDB driver; it came about while I was trying to write proof-of-concept code for my Java binary protocol API. The MongoDB wire protocol just happened to be well documented and easy to implement.

Caveats:

  • No binaries have been published; sources are in a git repository.
  • The code presented here is not a substitute for the official driver.
  • Information pertains to MongoDB 2.2.4.
  • APIs may change without warning.

This post is informational; not a tutorial. Everything presented here is pre-alpha code.