Wednesday, August 26, 2009

PHP Multithreading for beginner

This entry is inspired by Iman "Sapi". Thank you sapi.

PHP has become not just web scripting language. Now PHP also widely use for simple server side shell scripting. PHP is proven a strong platform do perform database and www operation, such as querying, CURL-ing, http tunnelling and API's function.


The differences is at output and how long the script allowed to runs. Output of web-based PHP mostly markup languages bounded with fancy images and media files. But shell scripting only output only-God-knows log messages, and has no time limit.


Supported by enormous of active developer, PHP server scripting are very easy to those actively build applications under web. PHP also powerful to build heavy-duty task, such as file backup, backend server-to-server processes. Like Java, PHP5 now has the capability for multithreading, to increase processing speed. The only limit is memory and processor. But I found for doing certain task (my case) it's much lighter than using Java.

To enable, first make sure you've installed pcntl extension. On Ubuntu machine, install the extension files :
$ sudo apt-get install php5-dev
$ cd ~
$ mkdir php-temp
$ cd php-temp
$ sudo apt-get source php5
$ cd php5-xx.xx/ext/pcntl (xx.xx is your PHP5 subversion, pressing tab would reveal it)
$ sudo phpize
$ sudo ./configure
$ sudo make

Copy the compiled extension into modules library.
$ cp modules/* /usr/lib/php5/xxxxxx/ (xxxxx is php library date)

Restart apache
$ sudo /etc/init.d/apache2 restart


Next is, try copy and paste this simple script simulate the multithreading.
Further reading, please visit PHP official manual page. Happy multi-threading.



$children = 5;

for ($i=1; $i<=$children; $i++) {
$pid = pcntl_fork();
if ($pid == -1) {
SaveLog("THREADING","could not fork thread $i");
die("could not fork\n");
}
else if ($pid) {
# If we are the parent, we did our job of giving birth,
# now lets finish our job and die!
SaveLog("THREADING","Parent thread exiting..");
exit(0);
}
else {
# Since we are the child, fork so that init will become our parent. Init
# is good at wait()ing on children - ie: reaping.
$cpid = pcntl_fork();
if ($cpid == -1) {
SaveLog("THREADING","could not fork in child process $i");
die("could not fork in child process\n");
}
if (!$cpid) {
# Put your real task here, dont forget they're run as separate process, make sure no dependency each thread
SaveLog("THREADING","Thread $i started");
$result = StupidTask(10000000);
SaveLog("TASK","Result on thread $i : $result");
SaveLog("THREADING","Thread $i finished");
# Don't forget to exit after the child processing is done. Certainly
# change this exit code if you need to however.
exit(0);
}
}
}

function StupidTask($count) {
for ($i=1;$i<$count;$i++) {
}
return "I'm done doing stupid thing for $count times";
}

function SaveLog($type,$msg) {
global $logfile;
file_put_contents($logfile,date('Y-m-d H:i:s') . ' ' . $type . ' ' . $msg . "\n",FILE_APPEND);
return true;
}

Friday, July 10, 2009

Install fancy fonts for your Ubuntu Java apps

Java is well-known development platform, not for enterprise system, but also to small application like rendering picture and processing video. One of the application I involve it, requires so many fancy fonts to make the output more beautiful.

It's not common that Java requires such fonts, but this really matters as it related to graphic and colour. Under Ubuntu box, Font that Java loads different from the system has. So we might need to copy all needed fonts into folder that reads by JRE.

For Ubuntu, Java fonts is available under folder /usr/lib/jvm/{your-java-version-folder}/jre/libs/fonts/. To find out what java version your working, see configuration of your Java apps. Refer to it, browse into /usr/lib/jvm, and match any possible version that's closest match.

By default no configuration file needed to edit if all your required fonts is satisfied. Once copied, reload your Java Application.

Tuesday, June 30, 2009

The longest "BETA" products

In software development cycle, we oftenly use the term ALPHA and BETA term to indicate a progressing software, but somehow ready to make a test hit. In my own experience, we usually use BETA only for stating a software meet its basic function but yet to test througly. There's no exact rule on how long a software should be put on beta before its final release.

For a s
oftware company, when it meet all initial designed functionality and performance, then it's OK to be released and leave the beta stage. I'm not sure what are GMAIL guys think of, but the product never officially release.

When it first announce on April fool's day 2004, this phenomenal free mail really changes the paradigm of free mail services. Yahoo previously only provide max 10MB of mailbox, now even no limit. Others are previously closed the pop access now consider to reenable. And the Ajax-interface inspire other web application to follow so.


What's now ?

It's been BETA for more than 5 years. It doesn't make any sense that a product still in beta for such long time, even I start to think maybe "beta" is the product name. I'm not really sure what is their initial design and plan, but for me, it's surpased any free mail service that makes Gmail more ready than any other similar service to get out of the Beta stage.


Do you know any software that labeled Beta longer than Gmail does ?

Wednesday, May 6, 2009

Manage MySQL datadir folder

Mysql no-doubtly is the most popular database server among web user for it available on most hosting and SMEs. It's distribution also available on most linux distro, including the popular Ubuntu.

Mysql data file is the base of mysql database storage. Relocating folder usually for harddrive partition storage management issue. Managing MySQL data file is kinda tricky, because since Ubuntu 8.04, MySQL data file is protected by apparmor by default. Meaning, we cant simply move around files related to MySQL, as all location need to be registered.

Basic step is to edit mysql config file, in /etc/mysql/my.cnf.
Make sure the service is stopped.


$ sudo /etc/init.d/mysql stop
$ sudo vi /etc/mysql/my.cnf

Look for datadir directive, change it to your new location. Datadir location has to be actual location, not a symbolic link. Save and quit the editor. Dont forget to move old database location into the new folder, most importantly "mysql", as the folder contains user authentication info. Otherwise you'll find unable to log in to the database.

Apparmor Part

Apparmor is a security software for Linux. Apparmor protects unauthorised changes to system critical files. To open apparmor protection for MySQL, open a shell terminal, and type :

$ sudo vi /etc/apparmor.d/usr.sbin.mysqld

Add the new location:


/var/www/mysql_datadir/ r,

/var/www/mysql_datadir/** rwk,


Restart apparmor by :


$ sudo /etc/init.d/apparmor restart

If everything went right, mysql start status should showing [ok], otherwise, consulting syslog somehow could help. Type this before restarting mysql service

$ tail -f /var/log/syslog


In other terminal,

$ sudo /etc/init.d/mysql start

Now your database is located in folder you prefer to.

Wednesday, April 22, 2009

Affordable internet but with fair usage policy

Affordable internet finally available !

With a series of new regulation from Ministry of Communication and Information (Menkominfo), now internet subcription is getting affordable. Compare to my last two (article 1 & article 2) article related to internet rates, currently unlimited internet is available with cheaper monthly fee.

Started of by Indosat with its Broom Prabayar Unlimited, price start from Rp. 100.000 (US$ 9) per month, you can enjoy unlimited internet. Even nothing much to be proud of beside the cheap price, but after launched several months, Indosat is overwhelming with increasing of subscriber, exceed their capasity. As demand increases, it's getting hard to the get the Broom's starter pack.

Then followed by XL with XL internet unlimited prabayar, at approximately same rates, XL plot the service with only 250K subscriber. Not really sure if the quota has been surpased, but same thing happen, no where we can get the starter pack. Telkomsel recently lauched their first unlimited prepaid internet. As they claim have suprerior QoS, monthly fee slightly higher than other competitor.

All these service above carying same one term : Fair Usage Policy. As now limiting access no longer acceptable for subcriber, Fair Usage policy seems to be best fit for current situation. Fair usage policy is limiting connection speed when a sucbriber reach certain quota. Most GSM internet provider lock the speed at 64kbps when quota reach 2-3GB. This is quite tricky because they mostly use term of Broadband 3.5G speed in their promotion, although the maximum speed only at 256kbps.

My personal choice for home internet is Firstmedia. Although some said the customer service isnt nice (in solving problem) but as long as you have no problem with your access, I almost can say its beyond expectation. With Rp. 135.000 (+tax) (US$ 12), speed we can get up to 384kpbs flat no fair usage policy. A real download could reach almost 50KBps from Rapidshare.
So far, I'm quite happy with this service. Hope this can help you decide what the personal internet connection that suits you.

Wednesday, March 11, 2009

Setting up gammu with smsd

Gammu is the most popular program to build customized sms  server. It can connect to various of handset, from the oldest model until recently launched model.  Also it support populars os(es) such as Microsoft Windows and Linux Ubuntu. 


To setup gammu under windows simply download win32 binary from gammu site. For Ubuntu, even easier.

$ sudo apt-install gammu

That's all.

Once installed try edit gammurc (gammu config files). Windows version, config file will be in same folder as gammu.exe. For Ubuntu version, the config file is in /etc. Edit where needed.
My working setting is :


[gammu]

port = /dev/ttyS1
;model = 6110
connection = at19200
;synchronizetime = yes
logfile = /home/says/gammulog
logformat = textall
;use_locking = yes
;gammuloc = locfile
;startinfo = yes
;gammucoding = utf8
;rsslevel = teststable
;usephonedb = yes


To test out :

$ gammu --identify


On success connection it should display our gsm modem properties.

Manufacturer : Nokia
Model : 6150 (NSM-1)
Firmware : 4.12 E (02-12-98)
Hardware : 2320
IMEI : 493002107068660
Original IMEI : 493002107068660
Manufactured : 1298
Product code : 0502690
Simlock 1 : MCC+MNC 00101, opened, factory, counter 0
Simlock 2 : GID1 0000, opened, factory, counter 0
Simlock 3 : GID2 0000, opened, factory, counter 0
Simlock 4 : MSIN 0000000001, opened, factory, counter 0
MSID : 823c21bb67cc5d6aab0f3caae6
MCU checksum : 4057
DSP ROM : 4


Next is configure smsd engine. Create a smsd config file. Ussually named smsdrc. Edit where needed. Put the file in same folder as we running Gammu. I strongly recommend using files style, as you can easily to customize the system.

[smsd]
PIN = 327426
logfile = /home/says/sms/logs/smsdlog
commtimeout = 10
sendtimeout = 20
#receivefrequency = 0
#resetfrequency = 0
deliveryreport = log
phoneid = mdsms

inboxpath = /home/says/sms/inbox/
outboxpath = /home/says/sms/outbox/
sentsmspath = /home/says/sms/sent/
errorsmspath = /home/says/sms/error/
inboxformat = standard
transmitformat = auto

The main part of the system just to read incoming sms save it to /inbox, send any message that we put in /outbox. Next gammu smsd able to do the rest.

Next, create an agent just to read message source to be send out, and retrieve incoming msg in inbox to store in your database. last thing is create nice fancy interface, of course dont forget the user authentication. Then hola, your own sms server is ready to serve.


Download : php agent to read & delete files

Wednesday, March 4, 2009

Looks profesional, write profesional email too

Email is must have communication for today business activity. Some people may not aware of that email have ethics code, so your email looks profesional and not just email. I write this because I've seen a lot of people just doesn't know (or doesn't care) about this.

I just encourage people to pay more attention on good email manner

1. Use company domain for business purposes. This rule is very simple, never join a mass mail mailing list using your company email, simply signup a free mail service. And oppositely, never response to a company project using free mail.

2. Dont forget to include nice signature in every mail you sent.
It could be your contact detail, such as full name, position, company address, company phone, and company website. It's a very simple, as so many people might forget to set it. For me, receiving email without signature like the person trying to hide these detail, that not good.

3. Do not reply when you're not in the mood..
We seen one-word, one-liner reply like 'ok', 'me too','i agree'. For me it represent the sender not in mood to response. delete unused thread below.

4. Write what you mean, mean what you write
Don't compose an email in hurry, especially that's a weekly report. We can easily knows if an email is composed to catch up the target or it really composed from getting data from lot of sources.
In slank Jakarta word, "gak niat". No, never sent that kind of weekly report, better wait until you really have enough useful data and information.


Happy email-ing !

Friday, February 20, 2009

SMS spam, regulate it or block it?

First of all, I need to declare, SMS Spam here is refer to those free promotional message, not those unethical premium SMS(es) services, that charge user per receive. Also, as long as the content does not violate ethical and law regulation, e.g terorrist, race issues, raise conflict, etc.

Flashing back in time,where sms just a new-comer service, sending an sms cost about one meal, people found it very effective way to communicate. As it getting popular, telco company along with some creative people start to making business based on this.

And anomaly, the more telco lower the sms cost the more telco got it revenue from. And now, it like almost email, we can send an sms at almost zero cost.

GSM network design to interconnected each other. Like mail server to deliver email, sms delivered by an alien called SMSC. So far, only telco have the SMSC. The best part for spammer is, due to the regulation, sender keep all the revenue, while by right, destination network has to deliver the message due to the regulation.

The world is too big to implement single anti spam policy. So some country start regulate kind of sms. Some even with government law that could bring spammer to court.

But is the regulation effective to combat spam, especially by those well-organized company? Company may change its name. As long as it has the benefit to its marketing, there will always people to send these message in any way. Some company might tear to ground, some can flee away, but sure they will find a way to survive...

So is it effective to set punishment ? I said NO!!
The most effetive way is just block message that telco 'thinks' a spam. Same as my opinion for site blocking. There could by several way of detection like blaclisted Smsc (learning the lesson from email's anti spam softwares). So then just let user to choose which are the best telco for them, especially some country has implemented MNP. Let user decide, whether they prefer protective telco, or they would rather loyal to "more liberal" telco.

Somehow we might found some promo are quite informative. For me, as long as it not costing us, why bother! Just delete the message. Case close and thank you!

Saturday, February 14, 2009

Unleash your E71



Nokia E71 is the most complete and most premium E series to date ever produce by Nokia. As the highest rating (and one of the most expensive), user like me will always seek for any possible feature has is not published by Nokia.

Thanks to its stable and secure platform of S60, now installing application can not just simply send the SIS / SISX file to the device then install it. S60 require developer signature and valid certificate for the device can accept the application.


But it will not stops creative people to pass thru it. First, download HelloOX.Unsigned.sis and MapDrives.Unsigned.sis. Sign the application. To sign an application, go to a unofficial signature site at http://s60.vgooo.com/. Symbian signing site is closing down due to law suit from symbiansigned. Last time I found the only valid site is OPDA. Using google translate tool will help you to understand these chinese words. But the point is, first register your account (email, user name, and password) and your device IMEI. It take 24 hours for approval. Visit the site following day and login. Upload the HelloOX.Unsigned.sis and MapDrives.Unsigned.sis to be signed.

After signed, download the application and send to your E71. Make sure your device is accept all application before installing. Set it at
Menu -> Installation -> App Manager -> Option -> Setting Set "Software Installation" -> All. Online certificate check -> Off Install MapDrives before HelloOx. Right after HelloOx installed, go to Menu -> Installation -> HelloOX. Just confirm several simple question, then after everything is set, you can uninstall the HelloOx.

Now, your device is ready to accept any application, no more restriction. Just make sure play safe, beware of virus. And one last word as a software developer : IF YOU LIKE THE APPLICATION, PLEASE CONSIDER TO BUY IT.

Note.
If you just desperate cannot found the HeeloOX and MapDrives, drop me an comment. I'll send you these apps.

Referrence.
KasKus

Friday, January 9, 2009

Download KOMPAS Daily to your own server

Kompas starts today 2 July 2009, implements Microsoft's Silver Light framework, this download script temporary unable to fetch any edition




As most popular daily newspaper in indonesia, KOMPAS's content always been a compass most people to look to, from marketing to adv publisher, formal and informmal worker. As the biggest newspaper in country, everything in it counts, the news and advertorial. Some people may need it to be archived at their own for future reference.

As now, KOMPAS has their edition published online, you read it via internet browser. It uses djvu format, small image as thumbnail, combination of png (for text layer) and jpeg (for color photos layer). To download it, kind of hassle, because you might need to combine these two layers to get complete paper layout. Also, they just implement a sign up system to gather user information. Though it's free for now, but sure will consider a paid-scheme in the future.

If you want to keep ones as your archive, you install this script to your server, or you can try the working demo version. The interface isn't beautiful tough, but it's functional. Enjoy.

Demo version
Facebook version of KOMPAS
KOMPAS Script