Saturday, August 15, 2009

Google Wave Hackathon Malaysia

Today i attended the Wave hackathon. Some lucky fellas here are supposed to be the first in Malaysia to get google wave accounts (for the sandbox) .

Nazrul (the founder of MYGTUG) gave an overview and everyone quickly surfed and googled for more info . Basically for most of us, its the first time we are exposed to this Wave thing. This whole idea of blurring the line between emails and chats is boggling but opens up a whole new way of using "emails".

Due to some problems faced by other participants, somehow my "robot" was the only one demo-able at the end of the day. I guess my decision to just use the robot for fun things turns out to be easier to implement, Java programmers also complained about some issues with the api and Wave's unstability in handling certain events.

I went in with Python and i was using WingIde Pro, that must have been a real time saver since the autocompletion and module search gave lots of revelation of how some of the Wave Api works and fixes most easy errors. The problem is that you can't do a local test, so all the code must be uploaded before you can test it , simple errors will be a waste of time.

Here are some "traps" to avoid in creating a "robot" :-

1. If you add a capability to the robot AFTER you already uploaded your first robot, you will need to bump the version in your "whateverrobot.py" under the _main_ so that Google Wave will recognize that you are supporting a new event handling.
Java programmers will need to modify the capabilities.xml file to bump the version.

2. Don't just use the Tutorial example in creating Blips, you will end up modifying the blip
that the user posted instead of creating a new one since that is what the Tutorial example does.
You will get something like "User and robot" in the blip which is probably not what you want, instead just : newblip = root_wavelet.CreateBlip().GetDocument()
You can also try blip.CreateChild() if want it to appear below the original blip.
You must use the Tutorial example to learn and do the first upload though! Just don't
copy everyline when u want to do your actual one.

3. Upload the tutorial example so make sure all your other setup is already working.
You will need to setup a Google Application Engine account and a choice of either
Python or Java. (for Python i was using ver 2.5.4). Setting up Google Application Engine requires a valid and real mobile phone (it will sms a validation code).

4. IE.7 is unsupported!! Yeah, i guess Google is doing to Microsoft what Microsoft used
to do to some companies. So make sure you just stick to Chrome when participating.

Overall, it was a pleasant and enjoyable event.

Here is the robot i wrote (for those who have google wave account) :


Have Fun!

Thursday, August 6, 2009

Too much support?


I got this screen this morning when searching for something in MS.
Did MS just redirected all support calls to online search? ;-)

Monday, August 3, 2009

C# calling C DLL routines (YET ANOTHER ONE)

I am totally baffled on the examples given by sites on how to call C routines in DLL via C#.
Not only are many of them repeating one same example after another, you soon realize they are not very helpful when you want to do something that is common like passing a structure with all kinds of types inside.

So here is one example that serve as an answer to MANY of the pinvoke/calling C apis problem :- ( i wanted to say 90% but i can't justify or prove it ;-))

I got this example structure that has :

struct ToughCallDef
{
char * ptrName; // this will contain some return string values
int ptrNameLen; // normal integer
MEMBLOCK somestruct; // a structure
ANOTERBLOCK* structpointer; // structure pointer
int* ptrtoMyInt; // integer pointer
char * ptrID // this must be supplied
};

Just translate it this way :-

[StructLayout(LayoutKind.Sequential)]
public class ToughCallDef
{
IntPtr ptrName;
MEMBLOCK somestruct; // you need to define MEMBLOCK also
IntPtr structPointer;
IntPtr ptrtoMyInt;
IntPtr ptrID;
}

And if the call requires a reference to this "ToughCall" :

[DllImport("anotherc.dll")]
public static extern int Call_C_API(ref ptrToughCall);

define it this way instead :

[DllImport("anotherc.dll")]
public static extern int Call_C_API(IntPtr ptrToughCall);


Basically anything to do with Pointers, regardless of whether its integer *, char *, structure reference etc, just use IntPtr.


1 . If the member is a "char *" that you expect return values you point the IntPtr this way :

szReason = new StringBuilder(100);
zReason.Append(' ', 100); // empty string with spaces
ToughCall.PtrName = Marshal.StringToHGlobalAnsi(m_szReason.ToString());


2. If the member is a "char *" that you need to assign a value before passing in :

ToughCall.ptrID = Marshal.StringToHGlobalAnsi(StringVar);


3. If the member is a structure pointer, you assign it this way :

ANOTHERBLOCK o = new ANOTHERBLOCK();
ToughCall.structPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ANOTHERBLOCK)));
Marshal.StructureToPtr(o, ToughCall.structPointer, true);



4. After calling the C routine you will need to do the following to get the values :-
  1. If its a string, use :
    string s = Marshal.PtrToStringAnsi(ToughCall.PtrName)
  2. If its a structure use :
    Marshal.PtrToStructure(ToughCall.structPointer, o);
  3. Free any allocated strings/struct pointers via :
    Marshal.FreeHGlobal

That's it.

Wednesday, July 22, 2009

Twitter (python api) on Nokia S60

I wanted a system whereby people can sms to a phone that will log it to twitter.
With this system an organization can keep track of what its support/sales staff are at any given point of time.
After tinkering around with the twitter python api and Nokia s60 python (1.9.6), i finally
got it working...(duh...that took me more than an hour...)

There is however ONE big catch (read end of the section)

Here is the code :


Gotchas :-

1 - Ah yes you also need to change that getusername routine in twitter, else it wont work

2- Pay attention to that pesky pyS60 Application Packager (READ the README file) , this is not py2exe. Basically, you will need to rename your main file to some fix default.py.

3- It will keep asking you for connection (access point) on each msg , unless your using some newer phones like nokia 5800 XM. If you are using N82, ALL the popular examples shown in the internet like these :

http://discussion.forum.nokia.com/forum/showthread.php?t=163939

or

http://snippets.dzone.com/tag/pys60

or

http://croozeus.com/blogs/?p=836

DOESN'T Work. The reason is due to some "compatibility problem" between socket and btsocket.

"Twitter" uses urllib2 not urllib, and these people probably have never tested it on that api. Someone from Silicon Valley once said that Nokia know nuts about promoting development, and thus he rejected their offer to develop for symbian and instead move the whole team to iphone.

In some ways, i agree....but i still love my nokias ;-) I reckon the same thing would have worked in Iphone much earlier and with less blood on my desk.

** Update :

Marcelo Barros from Croozeus :

Yes, that is the problem. I reported it at maemo some time ago.
I suggest you to use my twitter api. Extend it if necessary.
http://code.google.com/p/wordmobi/source/browse/trunk/wordmobi/src/s60twitter.py
It uses urllib and simplejson (I ported it to S60, code in the same dir).

Note : If you still want to use the twitterapi as it is and not the ported version, you will

need to hardcode the "getusername" and stick to "socket" only calls, avoid using btsocket.

Sunday, July 12, 2009

C# calling Python scripts and processing output

Occasionally there would be a situation where the C# needs to call a python script and work with its output. While this might sounds like a piece of cake, it does have its gotcha, that is when
you want to process each line one by one and you happened to have somekind of timing (time.sleep) or some other python codes that works correctly under console but unable to be processed by C#.

Below is how you do it : (read until the end, there is a gotcha)

private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (outLine.Data != null)
Console.Out.WriteLine(outLine.Data.ToString());
}


static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "python.exe";
p.StartInfo.Arguments = "c:\\test\\test.py";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
p.Close();
}



Assuming ur python script is the following (test.py) :

import time
import sys

for x in range(100) :
print x
time.sleep(1)


You will be surprised that you dont get anything. For some reason, time.sleep(1) and possibly
some other python library calls would result in the output not being flushed. The solution
would be to "flush" it :

for x in range(100) :
print x
sys.stdout.flush()



Vmware and the Virtualization Gotcha's

There is this lab that has 4 different Win2k servers, each running a different application.
Server A - Mailserver
Server B - Intranet application with MS SQL db
Server C - Vpn server and backup
Server D - Test server

Along the way, the admin decides to put all these dated servers into a VM and slot it in to a high end Server from Dell. The whole porting process took around 2 weeks and when it was over,
everyone was happy with the new setup. No more additional switching cables and multiple monitors lyring around and the perceived energy savings cost was a bonus.

However one day, the mailserver began to feel very slow, Procexp (sysinternals) itself was myteriously taking up 45% cpu and more and even with the mailserver service stopped the pc still feels awkwardly slow. Rebooting didnt that mail server VM doesn't help either.

When i came in to help out in this scenario, the first thing i went thru was the list of VMs running in the server. One particular VM is taking up 26% of the CPU of the main server, however that should not be the reason why it would affect the mail server VM. Upon closer inspection however, i notice some native apps running on the test machine VM (26%) that is using up the VM's tcp/ip port very quickly.
It then became logically clear that this was the problem, pausing that VM immediately restored the other server's performance and that was like a 100% improvement.

I suspect the problem is because the main server is still just an OS with the normal limitation of the 65535 ports on a single ip and single network card. Since all the VMs runs on this machine , that test application was just blasting away the network resource, this turns the allocation of the real main server NDIS packets resources into an ugly situation where each of the VMs are queueing up to get its allocation.

Another case solve. Another Hamster Huey and the GooiKablooi award for virtualization.

Thursday, June 4, 2009

Linux Desktop And Windows 7

I must admit, Ubuntu has reconfigured my view towards the Linux desktop distro. Yes, its not something new, but from version 8.1 onwards since last year, the refinement is just slightly short of a usable desktop for common users.

Most of the stuffs are similar to windows in that you have hardware detection app running in the background with an icon in the system tray. The wifi detector works its best in trying to minimise "geeky" configurations and in my test notebook DELL inspiron 1520, it works straight out without the need for me to configure it manually and tweaking the config files.

With the common host of free apps like openoffice and email clients (Evolution - the email client is still lacking in refinement compared to Outlook Express but Thunderbird isn't), heck, for most Janes and Johns who just want to have email, internet surfing and office documents, this would feel right at home. The system works faster and you even feel "safer" without having any anti-virus, anti-malware, anti-adware, malicious removal tools , Windows Defender and all those hooking apps that layers itself one on top of the other.

Nine months ago, when i tried OpenSuse on my other notebook , it was hell, i had to configure and download specific wifi patch just to make it work. If a common user would have to choose between OpenSuse and Windows OS, even Win 98 would be a better option (speaking strictly in terms of usability for users who want things to just work and not do any command lines, scavaging forums and pulling hairs)

Ubuntu, rightly one of the most popular distro now, deserves an applause from the Linux community. Instead of sticking to the unspoken rule of "we can't copy microsoft, if you can't configure some simple scripts you are lame", it went straight on to make Linux a much friendlier distro for desktop. By providing an option without the need to repartition the HD and installing it side by side with Windows XP/Vista, it became an attractive option for users to try it.

The introduction of Version 8.1 was nothing short of perfect timing too. Vista, being an infamous slow OS - thanks to the "dev team" that insists on working on the new approach and forsake previous compatibility.

The fact is, Vista is slow. There is just something wrong with its design.
Personally i do not use any anti-virus, instead i even ran defrag religiously and PerfectDisk periodically to make sure i have optimum speed for my HD files. Still it was slow, i made sure my services are configured correctly and disabled all that i can lay my hands on and it is still slow. The frighthening thing was the fact that my Vista ran 3 times faster than any other Vista around my office on similar hardware, yet i find the response unacceptable.

Ubuntu on the other hand, ran seemingly faster even when it was installed using the side by side installation. Thanks to Vista, I believe tons of users flock to try Linux distro and many who happened to try Ubuntu would be positively surprised. Just this 2 weeks, i notice that there are 2 more collegues who installed Ubuntu and was excited to show it around.

However, there are still many hurdles for Ubuntu to overcome before it can become a replacement for Windows. For e.g, try connecting it to a projector, and good luck.
Try getting Compiz to work, chances are that you won't get it working correctly in the first 30 minutes and oh, pls update your Nvidia drivers else be prepared to do some file editing.

To date, there are around 200 Linux distros, some says 450. This is actually good news to Microsoft. As long as these jokers cannot unite, Windows will continue to dominate. Every aspiring Linux techie with enough willpower will want to produce his/her own Distro with some lousy excuse for yet another distro.

I would bet that if the community would drop all the similar distros and say just concentrate on 3 :

1. CentOS or RedHat for Server
2. Ubuntu for Desktop / Workstation
3. Knoppix for small storage / misc applications

Or better :
Ubuntu Desktop, Ubuntu Server, Ubuntu Mini.

Then all the big hardware manufacturers and the gaming industry would ensure that they will provide support for this alternative OS. Ms.Window would have to taste its own medicine in pitting against FREE OS. Yup, taste its own medicine. The number of companies that MS brought down just by including the said product/feature FREE are calling out for judgement day.

Netscape, RIP.

The only other area that MS still have not managed to kill are the Anti-Virus companies. Free Anti-virus seems to be playing along MS agenda all the time and yet failed to be executed. Hmm, before this happens, i should seriously fold up my sleeves and start working on this project i have in mind for some time now, the Corporate-Anti-Malware-End-Of-Anti-Virus project. A solution that will guarantee a corporate environment to be clean of viruses using a filter driver and a concept that involves a model pc, central sandbox and whitelisting. No more antivirus scanning and signature subscriptions!


Yawn....signing off.