How To Hack Twitter With a Few Lines Of Lua Code

Nowadays twitter is very popular for the web world, and so the lua for me. Below i write down a small lua snippet grabbing tweets from your twitter home. Enjoy it!

-- twitter.lua

http = require"socket.http"
r = http.request(string.format("http://%s:%s@twitter.com/statuses/friends_timeline.json", arg[1], arg[2]))
for i,v in ipairs(json.decode(r)) do print(v.user.name .. ":", v.text, "(" .. v.created_at .. ")") end

Run like this with ‘watch’

$ watch -n 60 lua -lluarocks.require -ljson -lsocket twitter.lua <username> <password>

This snippent requires luasocket and json4lua libraries. You should get’em via luarocks

tweets on terminal

How To Configure Hunchentoot Behind Nginx

Although i’m a newby on common lisp world, i decided to implement a simplistic mvc framework with common lisp. So i configured my recently owned linode vps with nginx, sbcl, hunchentoot, cl-sql, also installed emacs and slime for a convenient development environment which nicely supports remote connection to a lisp interpreter instance using slime’s swank server. Since i succeed with my own configuration, i want to share my experiments from here to lighten another lispers. But please pay attention that all instructions i share here is in the responsibility of your own risk of usage. The operating system i use is Etch release of Debian. Note that the version of applications that i use for examples should change in the future.

First of all let’s get nginx installed on our server, so go and get nginx tarball from nginx homepage. Download and unzip it to your home directory like this:

$ cd
$ wget http://sysoev.ru/nginx/nginx-0.6.34.tar.gz
$ tar -xzvf nginx-0.6.34.tar.gz

Enter to the unzipped directory and perform a user-wide installation with its prerequisites.

$ cd
$ apt-get install libpcre3-dev
$ apt-get install zlib1g-dev
$ mkdir nginx
$ cd nginx-0.6.34
$ ./configure --prefix=$HOME/nginx
$ make
$ make install
$ cd
$ rm nginx-0.6.34 -rf

In the end you should have a nginx directory with a structure like this

nginx/client_body_temp
nginx/conf
nginx/fastcgi_temp
nginx/html
nginx/logs
nginx/proxy_temp
nginx/sbin

If you wonder why i preferred nginx instead of httpd (Apache Http Server) or lighttpd, my answer will not be so long. Our primary goal to use a http server in the frontend of hunchentoot is to serve static files (images, videos, javascript or css files etc.) efficiently so nginx has small memory footprint compared to httpd and very stable memory management compared to lighttpd. Yes i know lighttpd is also a nice web server with very well known modules handled on an easy configuration file but it has a very annoying memory leak problem that has not been solved yet (#758). Also you should read a post about nginx vs .lighttpd comparison here. For more information please refer to the survey of web server usage statistics at netcraft because i’ll not continue to talk about web server challanges anymore. There is a probably more adequate reason for me is that i loved nginx :)

Anyway, you can test your nginx installation:

$ cd
$ cd nginx
$ sudo sbin/nginx
$ curl localhost

If you see the nginx response you’re so lucky that you jump over the nginx installation flawlessly

Now let’s put hunchentoot on work starting with installation of sbcl first. Go to sbcl platform download page, choose an appropriate architecture on Linux row and get it with wget.

$ cd
$ wget http://puzzle.dl.sourceforge.net/sourceforge/sbcl/sbcl-1.0.23-x86-linux-binary.tar.bz2
$ tar -xjvf sbcl-1.0.23-x86-linux-binary.tar.bz2

Installation is quite easy:

$ cd
$ mkdir sbcl
$ cd sbcl-1.0.23-x86-linux-binary
$ INSTALL_ROOT=$HOME/sbcl sh install.sh
$ cd
$ rm sbcl-1.0.23-x86-linux-binary -rf

And now set ‘SBCL_HOME’ environment variable and add ‘$HOME/sbcl/bin’ to the ‘$PATH’ environment variable to get sbcl work anywhere

$ cd
$ echo "export SBCL_HOME=$HOME/sbcl/lib/sbcl" >> .bashrc
$ echo "PATH=$PATH:$HOME/sbcl/bin" >> .bashrc
$ . .bashrc

Great! Now see the sbcl works

$ sbcl

If you entered to the sbcl interpreter you got it.

The funniest part is when you are in a lisp interpreter, now we have to install hunchentoot to the system. Here we’ll require the help of ASDF (Another System Definition Facility) and ASDF-INSTALL (Remote or tarball interface tool for ASDF). ASDF is library dependency resolver system for common lisp world. Fortunately sbcl comes with ASDF and ASDF-INSTALL.
Hunchentoot is full blown pure common lisp web server. The structure of hunchentoot is fun to digg further and plays very well while designing web frameworks. I plan to write another post to give a hunchentoot tutorial in detail. Anyway..

Now you are ready to install hunchentoot but first we should better to define a variable that skips GPG signatures of lisp libraries globally.

$ cd
$ echo "(defparameter ASDF-INSTALL-CUSTOMIZE::*VERIFY-GPG-SIGNATURES* nil)" > $HOME/.asdf-install

Install the hunchentoot by ASDF_INSTALL. This tool resolves library names from cliki, the community common lisp wiki and library repository.

* (require 'asdf-install)
* (asdf-install:install 'hunchentoot)
* (quit)

In the end you’ll got the funniest pure lisp web server in the world. Start server by typing

$ cd
$ cat > hunch.lisp
(require 'asdf)
(asdf:operate 'asdf:load-op 'hunchentoot)
(hunchentoot:start-server :port 8080)
(read)
[CTRL-D]
$ sbcl --script hunch.lisp&

Yep!!, let’s checkout hunchentoot wheter it’s working

$ curl localhost:8080

If you see the hunchentoot’s response you’re ready to configure nginx to proxy web page requests to hunchentoot’s listening port. We’ll create a virtual host and proxy the requests that has paths end with ‘.html’ to the hunchentoot. First create a virtual server home directory.

$ cd
$ mkdir -p nginx/html/example.org/htdocs

Now open nginx configuration file and create a virtaul host configuration within the http node of the configuration file like below.

    server {
        listen       80;
        server_name  example.org;
        location / {
            root   html/example.org/htdocs;
        }
        rewrite ^(.*)/$ $1/index.html;
        location ~ .html$ {
            proxy_pass      http://127.0.0.1:8080;
        }
    }

You must now restart your nginx process to reload it’s configuration file

$ cd
$ sudo pkill nginx
$ cd nginx
$ sudo sbin/nginx

Yes! From now you have got a working lisp web server behind a powerful static content server. You should try it out by putting some static content to virtual host root directory and pointing your browser address to it. Please pay attention that we have proxied all html requests to hunchentoot, you should configure the nginx to handle any other regex.

Please if any command fails do not be upset cause i didn’t tried the commands line by line. I assume that you’re familiar unix/linux envirmenment and should correct if any instruction booms.

Hope this post help anyone interested…

Read From Config To Override Default Value With a Type Free Method On DotNet

Here is a little code snippet i have written recently for type free variable assignment from config’s appSettings node using c#

public static void ReadFromConfig<T>(string key, ref T obj)
{
    string value = System.Configuration.ConfigurationManager.AppSettings[key];
    if (!string.IsNullOrEmpty(value)) obj = (T)System.Convert.ChangeType(value, typeof(T));
}

The usage is simple as itself.

// initialize a variable with a default value
int foo = 9;

// change the value of foo if any value assigned to it from config
// otherwise do nothing

ReadFromConfig<int>("foosetting", ref foo); 

Hope, this helps…

Simple Dependency Resolution Under a Directory Structure with GNU Make

make it

Since i decided to begin a C/C++ project, i thought that i must firstly prepare a development environment for my own convenience. So i began to dig about make tool for building an environment for myself. The most important feature i wish to implement was a proper directory structure. After hours reading GNU make manual, i found myself writing one that resolves header and source dependency between object files under a directory structure.

Here i share well parametrized and documented simplistic make file that supplies all my needs. The directory structure i use is:

./src
./src/greeter.cpp
./src/printer.cpp
./src/main.cpp
./bin
./makefile
./obj
./inc
./inc/printer.h
./inc/greeter.h

Since i’m not an make tool expert, please feel free to share if there is an absurdity or if you want extend it somehow

makefile:

# ------------------------------------------------------------------------------
# Copyright [2008] [Kadir PEKEL]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# This simple GNU make file automates your build processes with simple
# dependency resolution under a DIRECTORY STRUCTURE.
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Name your own project
# ------------------------------------------------------------------------------

PROJECT = testProject

# ------------------------------------------------------------------------------
# Compiler parameters
# ------------------------------------------------------------------------------

CC = g++
CFLAGS = -Wall -g
LIBS = -lm

# ------------------------------------------------------------------------------
# Directory structure
# ------------------------------------------------------------------------------

SRC_DIR = src
OUT_DIR = bin
OBJ_DIR = obj
INC_DIR = inc

# ------------------------------------------------------------------------------
# Source file sufix
# ------------------------------------------------------------------------------

SUFFIX = .cpp

# ------------------------------------------------------------------------------
# You shouldn't modify below unless you do know what you are doing.
# -
# The thing that is going on here is that any source file under the source
# directory is compiled into its own object file under the objects directory.
# Dependency declaration of the object file to its corresponding source file
# is done automatically. Also, if your source file has got a header file with
# the same name in the include directory, it's also declared as a dependency
# for the related object file.
# ------------------------------------------------------------------------------

ALL_CFLAGS = -I. -I./$(INC_DIR)
OUTPUT = $(OUT_DIR)/$(PROJECT)

SRC_FILES = $(wildcard $(SRC_DIR)/*$(SUFFIX))

OBJ_FILES = $(patsubst $(SRC_DIR)/%$(SUFFIX), $(OBJ_DIR)/%.o, $(SRC_FILES))

INC_FILES = $(wildcard $(INC_DIR)/*.h)
INC_SRC_FILES = $(patsubst $(INC_DIR)/%.h, $(SRC_DIR)/%$(SUFFIX), $(INC_FILES))
INC_SRC_OBJ_FILES = $(patsubst $(SRC_DIR)/%$(SUFFIX), $(OBJ_DIR)/%.o, \
							$(INC_SRC_FILES))

_main: $(OBJ_FILES)
	$(CC) $(CFLAGS) $(ALL_CFLAGS) $(OBJ_FILES) -o $(OUTPUT) $(LIBS)

$(INC_SRC_OBJ_FILES): $(OBJ_DIR)/%.o: $(INC_DIR)/%.h

$(OBJ_DIR)/%.o: $(SRC_DIR)/%$(SUFFIX)
	$(CC) $(CFLAGS) $(ALL_CFLAGS) -c $< -o $@

# ------------------------------------------------------------------------------
# Default target that does the work when you type 'make' without pointing to
# any target
# ------------------------------------------------------------------------------

.DEFAULT : all
all: _main

# ------------------------------------------------------------------------------
# Put your cross dependiences here
# ------------------------------------------------------------------------------

$(OBJ_DIR)/greeter.o: $(INC_DIR)/printer.h
$(OBJ_DIR)/main.o: $(INC_DIR)/greeter.h

# ------------------------------------------------------------------------------
# Here you can write down custom targets and if you wish, you can make the
# default target dependent to yours. For ex: 'all: _main <your_target>'
# ------------------------------------------------------------------------------

.PHONY : clean
clean:
	-rm $(OBJ_DIR)/* $(OUT_DIR)/*

your_target:
	@echo "your target"

Faster Eclipse On Slower Machine

If your computer goes slower while working in a big project, you recognize that you need to do some tweaks to work more fluidly. Here i share some of my tricks that meaningfully speeds up my eclipse work flow.

eclipse

Disable mark occurrences

Open preferences (choose of project wide or global wide preferences is up to you) window and type “mark occurrences” in filter text box. Select the “Mark Occurrences” from the list box and remove the tick from “Mark occurrences of the selected element in the current file” check box.

Remove structured text validation

This time type “validation” into the filter text box in preferences window and select “Validation” from the list box. You’ ll see some of file types that are promised to be validated. Deselect validation ticks from all of the file types in the list (you can do it manually later if you want to). You’ll see a really very big difference in eclipse performance if you have big xml and wsdl files. For a example in my last J2EE project, my web.xml files contain 1400 and wsdl files contain thousands lines of text so eclipse couldn’t handle all the validations while computer memory is avarage

Do not use subclipse plug-in

Subclipse consumes so much system resources and effects eclipse performance greedily in big projects. If you could, consider not to use subclipse especially in projects that contain thousands of code kept in subversion source repository. It’s really become a very heavy-weight plug-in with heavy-weight code. You should feel better using subversion from the command line or from a seperate client

Consider converting your static code to a jar library

This advise can be possible more likely when you have static code automatically generated from static wsdl belongs to a web service. By this way you reduce the raw code size in project and use the code functionality from compiled classes to force eclipse to use fewer system resource.

Configure java virtual machine memory management start up arguments

In your eclipse.ini file, set -Xms40m and -Xmx256m args as your needs. This options define minimum and maximum memory usage bounds which passed to java virtual machine to manage eclipse application domain’s memory allocation tolerance. You can tweak this values and experiment your optimum eclipse speed.

Also if you have problems in eclipse’s memory management in Linux os environments like having lots of out of memory errors, you should define permgen space argument in eclipse.ini file. Setting this arguments as needed, you will have very few (or not) sudden memory exceptions. Try these -XX:PermSize=128m -XX:MaxPermSize=128m values if you got for about 1GB ram in your machine.

Finally the Eclipse version i currently have is 3.3.1.1.

« Previous Entries  
Valid XHTML 1.0 Strict Valid CSS!