Wednesday, October 5, 2011

New Home? time to play with some automation!

So Yeah, i know i don't write half as often as i'd like but hey! probably no one is reading my blog so...meh!

[caption id="" align="alignleft" width="181" caption="Tasker Task"][/caption]

I wanted to write today about a little automation i made, which i'm rather proud of. 2 months ago, i moved in with my girlfriend and now that i have a pretty powerful laptop that i use only rarely, i decided to have some fun with it, so i set it up as a little file/sharing/downloading/multi-purpose server. I no longer have a download restriction, so i download a lot of stuff on torrent. The only problem is that i don't want these torrent to be using all my bandwidth while i'm at home (the place where i live has a really shitty internet, even slower than 1 Mbps :( ). I use a lot of automation on my phone with Tasker, and i actually really like it, i set-up all kind of stuff like
automatically muting my phone while i'm in class (it checks on my calendar to know if i'm in a class!). Well anyway i thought to use the same kind of automation software for my computer and integrate the two!(yeah i know, i'm a programmer, i could just program the damn thing, but i like to hack around)

So i started by installing Eventghost, a software that can monitor all kind of events on your computer, from mouse move to software opening and... HTTP Requests!! :D. I needed my phone to communicate with my computer, so i thought i would probably need some network component and, HTTP is pretty simple so i settled with that. At first, since i don't have a static ip address at home, i wanted Eventghost to send to my phone the renewed ip address, it however turned out impossible since Evenghost doesn't permit to receive information from the GET requests, it justs create an event. Also, it would have required to have a permanent webserver on my phone (or something like that) and that would've probably drained a lot of battery. Anyway i started by creating the action to do when the HTTP request arrived, Eventghost would just open uTorrent, select all the torrent (really just typing CTRL + A) and then start or stop all the torrent. quite simple really!(i later found a more robust way to do it, more on that later).

So, now that everything was set-up to start and stop the torrents when some event was triggered, i went on my phone and created the trigger with Tasker. My tasker already has profile to know when i'm at home or not (i use the wi-fi SSID to know) so all i had to do was trigger an HTTP get request when i got home that would stop the torrent, and one that would start it when i left, this part was really easy. I also added the same kind of rule for when i went to sleep, my torrents would start when i went to sleep(or when i plugged my phone in) and stop when i woke up (when i unplugged my phone). So, there i had it, automatically starting and stopping torrents :D.

Now, fast-forward 2 weeks after-that, my setup doesn't work because my dynamic ip changed, and in the meantime i have had the change to discover another (REALLLY NIIIICCEE) automation software (which was developed at MIT) named Sikuli, so i decided to update the way i did things a little. I started with the part where Eventghost used to Start/Stop the torrents, i wrote a really small script to do it with Sikuli:

[caption id="" align="alignleft" width="371" caption="Sikuli Script"]Sikuli Script[/caption]

So this script does the exact same thing my event ghost actions did but it's a lot simpler and it uses image recognition to navigate (instead of me specifying the coordinates) so it's a lot more stable!I also had to change the way Eventghost  handled the event so that it would instead call the script generated by Sikuli.The other modification i did was to use dyndns to map a dynamic ip address to a static domain name.

[caption id="" align="alignright" width="225" caption="Evenghost final event"]Evenghost final event[/caption]

Friday, February 11, 2011

Multi-threading and the problem of race conditions

So, this week was the week where we needed to deliver the first prototype of our third year project. Until Wednesday, everything was going smoothly, without any bug or issues. But, when came the time to actually try the light client (the part of the software that would allow us to chat using a PDA), nothing was working as intended and I started to panic. Gabriel Royer, one of my teammate helped me out by going through every issue one at a time.  I needed i was panicked, my only responsability was a failure!


After one hour, i had to leave for work but not everything was solved so, I told myself that i would stay the night if needed to solve all this. However, while i was working, 2 of my teammates continued to debug and were able to solve every problem, me panicking really helped nothing.


In the end,  the problem wasn't even entirely my fault! Imagine my reflief, when the next day they told me that the problem was actually race conditions in the network that caused the application to not behave as intended on the PDA while it worked just fine in the emulator

Tuesday, February 1, 2011

Multi-Platform development and preprocessing.

Yesterday, while working for our school's third year project, I came across a problem, the code we were going to use and compile for 2 C# platforms (Asp.net 3.5 compact and Asp.net 4.0 normal) wasn't compatible for both platform. It took a hard time to realize since our compilator was giving an error like:
"I was waiting for an argument of type System.Net.IPAdress[], but received an argument of type System.Net.IPAdress[C:\System32]"

It took a while to actually figure out what was the problem. As it turns out, it was because i was trying to include files compiled for a not-compact version of Asp.net. The fix?(thanks to Julien Gascon-Samson on this one) incorporate the source files in a new project that would compile for a compact .net version. Once we did that, we tried recompiling and realized, there is a lot of different stuff on the compact framework, and that's where the pre-processing directives came into play.

We added every part of code that caused problem in a different "Utilities" static class which would help us code different behaviors on different platforms. The part to do that was quite simple:

[code lang="csharp"]

#if !PocketPC
return (int)e.SocketErrorCode;
#else
return e.ErrorCode;
#endif
[/code]
After i found out about this, everything became much easier, i was able to continue on implementing the lightweight client. What is the lightweight client is the title for another post..maybe!

Thursday, January 27, 2011

C#, Windows mobile 5.0, delegate and multithreading

This week was the 3rd week of school and it was also the week i started to code the 3rd year project! The project goal is in fact to add internet multiplayer functionality to an already existing racing game, after spending 2 weeks refining the software requirements and filling papers and papers of documentation, we finally got to the interesting part, the part where we actually learn something.

So I started the week by beginning the development of a chat interface which will run on a Windows mobile 5.0 PDA (old stuff dating from over 3 years ago). The interface part was pretty easy to do, Winforms is great for that, you just have to drag' n drop to create the layout and then you can start programming. I got all the layout working within 1 or 2 hours but then i wondered what was the best way to keep the information between the window, and also create the communication to the server. After talking to a collegue, i decided to simply create a "Session" object which will control the displayed page as well as the messaging service, I don't know if that was the best way to do it but i'm open to comments, i'm at school to learn.

So after that i started to think of the best way to receive messages from the server, i decided to go with starting a thread which would wait for the input from the server and then would go on notifying the interface to update itself. But to do that i needed to know 2 things: what is a delegate and how to start a thread! After 1-2 hours of reading tutorials, i got the hang of delegates and decided to continue on with the implementation. I created the thread which ran on an instance of the messaging object (yeah I know, that is probably not the best way to do it) and don't worry it didn't access any data, it was actually just waiting for an answer from the server. The real problem was calling the delegate to afterward, since it resided on the main thread, the function couldn't be accessed except by quitting the thread, so i had to learn to use the .Invoke function (which blocks the incoming thread). But finally, I was able to make the 2nd thread totally separate from the first one. Now the next step is to actually communicate with the server.

Tuesday, January 25, 2011

New domain name, and trying to post more often

Hello everyone!

I know there's probably nobody reading this, but i just wanted to say that i changed my domain registrar to Hover.com and, since godaddy kept my old domain name, i had to change the domain for my blog, it is now awesoftsomeware.net. No wonder you already figured that out since you are reading this but i just wanted to say a couple of things about godaddy.

This winter, i forgot to renew my domain with GoDaddy ( i was busy at the time and never found the time to do it) so now that i was ready to renew my domain name, they were charging me 90$ to renew it, that is an enormous amount to me so i just decided to move to a better registrar, hover.com. The domain name did cost me a little more (5$ more a year which isn't THAT much).  So i don't recommend godaddy to anyone!

So now that the blog is back up, I told myself that I would try to post at least one small post every week, I don't know if i'll be able to keep up with this, but I will definitely try.