Category Archives: Java Programming

Articles on Java programming.

jyn: Simple Native Code Access in Jython

I had been wanting to mess around with native code access in Java for awhile. I’ve also been delving fairly deep into Jython, mostly out of necessity for RingMUD. So, I decided to combine these two ideas, and started work on a simple library similar to ctypes. Jython does not have a working ctypes implementation yet, but it is coming along. This was mostly for my own experimentation to learn a bit more Python as well as experiment with JNA (Java Native Access).

JNA is the much simpler alternative to JNI. In normal Java, you define an interface that corresponds to the function prototypes of the native library you want to access. You then create a new “instance” of the library by calling JNA’s special gateway method. From that point on, you can call the defined functions of the library.

The nice thing about Python is that it is a dynamic language. By overriding __getattr__ and friends, and using JNA’s reflection API, it becomes possible to have Jython lazily load the function references and call them in a much more intuitive way. The name of this simple library is jyn (No, no alcohol was involved in the creation of this library).

An example:

import jyn
lib = jyn.libc() #Load libc for Windows or Unix
printf = lib.printf
printf("Hello world! I am printing from Jython!\n")

jyn is not really suitable for production environments yet. The most glaring problem at the moment is that there is no argument checking. This means that passing wrong arguments to a native function will usually crash the JVM. I am definitely open to more experienced Python developers willing to improve the library.

jyn is intended mainly as a demonstration of what is possible with Java and Jython. The Java platform brings with it a large selection of powerful libraries, and Jython brings the dynamic power of Python. Combined, they are an unbeatable team.

Grab jyn from GitHub.

Google Wave Development

I recently got ahold of a Google Wave account and started experimenting with development. My first foray into Wave development was with a bot. I made a simple group management bot (it’s not actually complete yet). Currently, Wave does not have any functionality for managing groups of users. While that will change in the near future, the immediate situation required a simple group management system. Users can add themselves to the community bot, and then they will immediately be subscribed to the community’s waves. Yes, there are bots that do this and are a bit more genericized to allow for multiple groups in one bot. I did it for the learning experience. The bot itself was not that hard to code. It’s not very big–having only four source files. It was learning the API, the way the Wave robot client library works, and Google’s App Engine that took up a little bit of time.

This bot will probably become useless within a few weeks after Google implements its groups functionality, but this will work for now.

RingMUD Being Worked on Once More

I’ve started working on RingMUD once more. This time, I’ve hammered out a to-do list that will finally put the MUD from alpha into beta. Beta, in this case, means that the base engine system is done and that the content needs to be created. Code work would continue of course during the beta phase, but the larger focus would be on developing the XML files that describe items, player classes, and the like.

RingMUD is a MUD engine, rather than a specific MUD coded in Java. That means the content should be specified outside of the code itself. The goal is to create a D&D 3.5-based system that can be used by people to design any world they want.

Here’s a general idea of the to-do list for the MUD:
1. Modify movement code to support different zones.
2. Implement serialization/saving of player characters and the world state. (IN PROGRESS)
3. Implement defining player classes, items, etc in XML files. (IN PROGRESS)
4. Convert all code documentation to Javadoc.
5. Move many options outside the hard code into a config file.
6. Implement equals methods for various classes such as Race, Body, and BodyPart.
7. Investigate display problems that some MUD clients seem to be having.

Here’s some things that have already been done in a day:
1. Standardize client-server communication code instead of having methods scattered everywhere.
2. Rewrite and simplify code that parses outgoing data.
3. Clean up the logon session class. It was extremely hard to read previously.