Rosetta Code

I’ve been brooding lately that I haven’t been doing anything interesting as far as programing lately. Mostly I’ve been doing pretty run of the mill stuff, maintaining a large infrastructure that has been built up over time. Adding some new features here or there but nothing truly that interesting.

Instead of whining about how uninteresting my programing is right now, I figured I would do something about it. My solution come in the form of Rosetta Code. I really want to say I’ll do at least 3 projects from it a week but even that feels a little too ambitious. Lets start with 1 or 2 a week and see how things go.

Hopefully my stuff doesn’t suck too much :)

First up is an erlang chat server. It’s not very secure but hopefully no one would really use it for anything real.

http://rosettacode.org/wiki/Chat_server#Erlang

Posted in Uncategorized | Leave a comment

Try blogging

A while back my server got hit. Some script kiddie came around and basically defaced all the wp installations that happened to be on the server. I fixed the few that actually get traffic. Mine, I didn’t care so much about it. So it was just swept it under the rug and the traffic was redirected elsewhere. Looks like I lost all the content too, but that’s alright. Life got busy. Work picked up, got a house and a second cat. Not that I have thousands of readers to apologize to about not posting, but I do want to post more.

For some time I’ve been working on an erlang web frame work. Using mochiweb as a base to handle the actual talking. I want something in between mochiweb and nitrogen. It won’t make the hard decisions for you but just a simple way for an erlang app to talk to the world via http in a RESTful manner.

The project started as a twitter choose your own adventure game, and worked well as that, but I wanted to make it more generic. Something that could be reused by me, and hopefully someone else would see some value and use it as well because honestly that’s the real reward for writing and releasing something like this.

Problem is I constantly start and stop on the project. I get just deep enough where I want to start over thinking things and wonder how much better things would if I just did something different. Things cascade and it gets restarted. I think the problem as finally been found, no clear design or direction.

What needs to be done, is to start from the top define what the app needs to do, break it down to it’s logical parts. Figure out what processes need to be running, and define the communication and data formats to exchange between them ahead of time. No more, oh this could be a record or this could be an array, stuff. Less by the seat of my pants. I think it’s the fancy thing to call it is, scope and discovery.

It would be extremely naive to think I can see everything ahead of time, so things will evolve but hopefully in a manageable way this time. A more clear direction is what I’m aiming for.

Also need to finish that tiny url tutorial :P

Posted in Programming | Leave a comment

I played the odds

I played the odds with a 54% failure rate it was bound to happen. 4 friends two broken, I lost the dice roll, at least the other two are safe now. Damn red ring of death.

Posted in Uncategorized | Leave a comment

Getting the Alchemy memory ByteArray

To move data back and forth between alchemy and acitonscript you can use the normal functions to shuffle the data between the two but when you want to move a lot of data using those functions cam be really slow so what is one to do?
Well there’s the GLEByteArrayProvider but I can’t figure out how to get ahold of it.
Then of course according to the alchemy documentation you can use the gstate (which is basically the alchemy fsm) Since it’s a global variable you should just be able to import it via.

import cmodule.MY_MODULE.gstate

But alas it doesn’t work, gstate is not public so you’re stuck, well not really….
Continue reading

Posted in Alchemy, AS3, Programming | Tagged , | 1 Comment

Adobe Alchemy: Anatomy of a Simple C Program

Adobe alchemy is one of those that I found totally overwhelming, at least at first. It took me two tries many months aport to really grasp a hold of things and while I don’t quite have a handle on everything I can at least go through the motions to get things working and I suppose the rest will come along as I go. The setup docs are here follow them closely if you are going to attempt this, I don’t think you can royally screw things up permanently but there’s a lot of stuff going on, one miss and the whole house of cards falls. The most important thing to remember once it’s all installed is alc-on alc-off. Since Alchemy needs to take over gcc for things to work you use those two commands to turn things on and off.

First off we are going to check echotest example it’s in the $ALCHEMY_HOME/samples/echotest directory. It’s simple but it shows a few very important things that can really get you on the ground and running. Stringecho.c is where the fun stuff happens. A simple a string is passed from your action script code to the alchemy cmodule where it just sends it right back, pretty worthless overall functional wise but a great place to learn.

Stringecho.c is pretty well commented so really I just wanted to add some points on how to extend things. In the main() function you have to define what functions are exposed to your actionscript code. Each function you want to be exposed is typed as an actionscript function instance.

AS3_Val echoMethod = AS3_Function( NULL, echo );

Then a AS3_Object is built that holds a reference to these functions.
Continue reading

Posted in Alchemy, AS3, Programming, tutorial | Tagged , , , | Leave a comment

Encode any file as a png

I drew some inspiration for doing this from a recent service that offers to encode a torrent as an image. Personally I don’t see the point of doing this but also I saw and thought it looks like a fun and easy exercise to kill a little time. I’m not sure if you can consider this stenography sure it’s hiding the data in an image but it’s not really hiding it that well considering if you ever got this picture you would think it’s just junk. What would be more fun if it stored it in a perfectly good looking image but alas that sounds a lot harder maybe for the next iteration. In the mean time here is a picture of guido van rossem encoded in a png using this method. How worthless is that? Source code later when I clean it up. I used python of course. I’m not sure what is in Guido’s hand but there’s a chance he is drunk in this picture.

Original Picture:
guido

Encoded Picture:
guido

Decoded output:
out

Tada! That’s how we do it.

Posted in Programming, python | Tagged , , , , | Leave a comment

bsdiff Binary file recreation in Python

Edit: There’s a slight problem with how I was extracting the numbers. I just assumed it was a simple long long int, but it’s not it’s as far as I can a custom way of storing things. I corrected the problem in my alchemy version, so once I update the python version I’ll make a note here.

For a reason that will still remain secret I want to get familiar with binary diff files and recreating files from those diffs and what better than language to do this in but Python. I’m basing all this on bsdiff.

The jist of this thing is you have an application that is out in the wild and you just made an update to it. So now all the masses of people using it out there want the upate. Really all you did was changed the background color but people love it and want it. It’s shame though because that application is 10 megs and in reality very little has changed. That is where bsdiff comes in. Instead of sending over the whole file, bsdiff takes a look at the files and tries to figure out what has changed and saves those to a file. So in theory that diff file should be a lot smaller. That diff file gets sent over and some magic happens and hopefully the file is now identical to the new one. Yea!

So you have a newfile.zip and oldfile.zip. Then use bsdiff to create a diff file

bsdiff oldfile.zip newfile.zip patch.diff

Continue reading

Posted in Programming, python, Uncategorized | Tagged , , , , | 2 Comments

How to make your own Tiny URL service in Erlang. Part 2 say hello to erltl.

Erltl is part of the erlang web framework called yaws, it simple and fast and is great at allowing you embed some erlang logic in large endless streams of binary, which happens to be great for writing html templates.

Since erltl is part of the erlyweb package so you’ll have to grab it from here
http://code.google.com/p/erlyweb/source/browse/trunk/src/erltl/erltl.erl

You already know some erlang you’re in luck there isn’t much more new to learn.
Make your templates as you make any other html template but where you need a little erlang magic just enclose them in the <% %> tags.

The basics are

<%! [comment] %>
<%? [top-level expressions] %>
<%@ [function declaration] %>
<%~ [TopForms] %>

Before you can use your template you have to compile it. By convention templates have the extension .et. Use erltl:compile(file_name) to compile then file_name:render(Data) to output the results. The Data parameter is passed in and is usable as Data in the template. Pass the output of the render command to Mochi and away you go.

% you don't want to do this every time unless you are debugging
erltl:compile("my_template.et"),

% some data to render in the template
Data = {["one", "two", "three"], "something else"},

% output the template
Body = my_template:render(Data),

% send the output to mochiweb
Req:respond({200, [{"Content-Type", "text/html"}], Body})



my_template.et

<%? {Numbers, Text} = Data %>

<ul>
<% [display(Number) || Number <- Numbers] %>
</ul>

<em><% Text %></em>

<%@ display(Number) %>
<li><% integer_to_list(Number) %></li>
Posted in erlang, Programming, Uncategorized | Tagged , , , | Leave a comment

FizzBuzz

FizzBuzz is a simple game
have your program count from 1 to 50
if the number is a multiple of 3 instead of the number print out “Fizz”
if the number is a multiple of 5 instead of the number print out “Buzz”
if the number is a multiple of 3 and 5 instead of the number print out “FizzBuzz”
else just print the number.

I want to do it in a few languages but mostly got stuck in erlang.
Here we go, I hope I read what FizzBuzz was suppose to do carefully enough.

Start with the shortest one
Python FizzBuzz

for i in xrange(50):
    print (i, 'Fizz', 'Buzz', 'FizzBuzz')[(not n % 3) | (not n % 5) < < 1]

Most of these erlang ones can be mixed an match for more permutations but I didn’t want to get to crazy, I could have gone with this for a long time.

Erlang FizzBuzz1
Continue reading

Posted in erlang, Programming, Uncategorized | Tagged , , | Leave a comment

A* Path finding

I’m not going to spend much time talking about a* because there are so many other great articles out there. Such as this one that was a basis for my actionscript implementation. Good news is that it works really well. Bad news? It’s slow, I mean really slow. I even spent a whole day devoted to spending this that sucker up, I’m talking in-lining functions and converting arrays to vectors, I think I’ve seriously come to end with doing this in actionscript. Next up is implementing this in alchemy which should be interesting, time to brush up on my C!

I’m looking at upwards of 2ms per path finding operation which doesn’t sound like much but consider 30 fps which means I only get about about 33ms to do a complete update. To get around this I decided that I could get away with only doing one path finding operation per frame update and the results aren’t too bad.

Click on the swf then use the arrow keys to move the bigger block around which the little blocks come after you. If you stick around the red areas the little blocks can’t see you. Have fun.

astar

Posted in AS3, Programming | Tagged , , , , | Leave a comment