Friday, September 14, 2007

Skype is sniffing ? You must be kidding me!

Guess what! The little tricks of skype does not have an end ... The Linux users of Skype recently discovered that it is reading the /etc/passwd and firefox profile files. COME ON? Do you continue using Skype?

The original post is here: http://forum.skype.com/index.php?showtopic=95261
And some explanations can be found here: http://www.oreillynet.com/sysadmin/blog/2007/08/a_close_look_at_questionable_a.html

But I seriously consider using Skype ...

Do you?

Tuesday, September 11, 2007

FileSystemWatcher problem

Did you have a task for monitoring files?
The FileSystemWatcher class has a great features, but also some outages. The problem is that creating/copying/moving files arround is hard taks and the watcher raises couple of events.
The most important events in my case were "created" and "changed". The problem is that the "Created" event is fired when the file is started being created, not when finished being created. After the created, there would several "changed" events depending on the file size. So how could we possibly know when the file is realy created, full and ready to work with it?

There are several posts and examples arround the net like:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2123580&SiteID=1&mode=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659823&SiteID=1

What is my approach?
After catching any event - you just stop monitoring for FSWEvents and start a timer. On every tick you try to open the file for reading. If you cannot - increase the timer interval. You can also add a code to monitor the total time elapsed since the first try to open the file, but the provided code does not do that.

So here it is - one of the approached to more correctly monitor for files:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Timers;

namespace MonitorTest
{
    public class Program
    {
        private FileSystemWatcher _watch;
        private string _watchPath = @"d:\temp\";
        private string _watchFile = "SomeFile.txt"
        private Timer _timer;
        private DateTime _started;
        private TimeSpan _totalTime;
        private void StartTrying()
        {
            _started = DateTime.Now;
            _totalTime = TimeSpan.Zero; 
            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Enabled = true;
        } 
        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            string FullPath = string.Format("{0}{1}", _watchPath, _watchFile);
            FileInfo fi = new FileInfo(FullPath);
            StreamReader r = null;
            _totalTime = DateTime.Now - _started; 
            Console.WriteLine("Trying? {0}", _totalTime);
            try
            {
                r = fi.OpenText();
                r.Close();
                r.Dispose();
                _timer.Enabled = false;
                Console.WriteLine("File is free for reading after: {0} timespan?",_totalTime);
            }
            catch (IOException ioex)
            {
                Console.WriteLine("Cannot open ? {0}", ioex.Message);
                _timer.Enabled = true;
                _timer.Interval += 1000;
            }
            finally
            {
                if (r != null)
                {
                    r.Close();
                    r.Dispose();
                }
            }
        } 
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or eleted.
            FileSystemWatcher wo = source as FileSystemWatcher;
            FileInfo fi = new FileInfo(e.FullPath); 
            if (wo != null)
            {
                switch (e.ChangeType)
                {
                    case WatcherChangeTypes.Changed:
                        Console.WriteLine("File {0}{1} was changed? {2} 3}", _watchPath, _watchFile, fi.LastAccessTime ,wo.NotifyFilter);
                        StartTrying();
                        _watch.EnableRaisingEvents = false;
                        break;
                    case WatcherChangeTypes.Renamed:
                        Console.WriteLine("File {0}{1} was renamed? {2}",
_watchPath, _watchFile, source);
                        StartTrying();
                        _watch.EnableRaisingEvents = false;
                        break;
                    case WatcherChangeTypes.Deleted:
                        Console.WriteLine("File {0}{1} was deleted? {2}",
_watchPath, _watchFile, source);
                        StartTrying();
                        _watch.EnableRaisingEvents = false;
                        break;
                    case WatcherChangeTypes.Created:
                        Console.WriteLine("File {0}{1} was created? {2}", _watchPath, _watchFile, source);
                        StartTrying();
                        _watch.EnableRaisingEvents = false;
                        break;
                }
            }
        } 
        public Program()
        {
            _watch = new FileSystemWatcher(_watchPath);
            _watch.Filter = _watchFile;
            _watch.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
            _watch.Changed += new FileSystemEventHandler(OnChanged);
            _watch.Created += new FileSystemEventHandler(OnChanged);
            _watch.Deleted += new FileSystemEventHandler(OnChanged);
            _watch.EnableRaisingEvents = true; 
        } 
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.ReadLine();
        }
    }
}

Wednesday, September 5, 2007

Another SKYPE issue ...

Do you know the Debug View ?
It is a very usefull application by Microsoft. You can watch for all debug info sent to the "Debug output".

So, I was looking for debug output from some of my projects and I noticed some strange "PING/PONG" messages. The next thing I saw was some names from my Skype's contact list. Amazing ...

Shouldn't they build Skype end product in "Release" configuration and suppress all these debug output messages ?

skype

Saturday, August 11, 2007

ActionScript 3.0 / Flash 9

Great surprise for all of us who in the years got used with ActionScript 2.0. First of all - we cannot apply actions directly to the movie clip (so forget about the really simple on(release){...} ).

If you click on a movie clip and you have the "Actions" window opened you will notice "Current selection cannot have actions applied to it"! At first it is irritating, but it seems to be a good point.

So how to make a "RollOver", "RollOut" etc. effects in AS3 ?

The first what we do - we have to import the Events package:

import flash.events.Event;

Now lets assume we have the movie clip on the stage and we named it "mymc". What we have to to is to add event handlers to it to listen for events:

mymc.addEventListener(MouseEvent.ROLL_OVER, RollOverHandler);
mymc.addEventListener(MouseEvent.CLICK, ClickHandler);
mymc.addEventListener(MouseEvent.MOUSE_OUT, RollOutHandler);

There is a set of predefined constants in the MouseEvent object, which we can use.

The next step is to implement the event handlers. Here is the simplest implementation of an event handler:

function RollOverHandler(me:MouseEvent):void {
    me.target.alpha = 1.0
}

I would rather call the type of "me" MouseEventArgs, but it's another story. What do we have in the MouseEvent variable is the "target". The "target" is the object that has escalated the event. Now we can say to that target what to do. We can say for example: me.target.gotoAndPlay(2), or me.target.gotoAndStop(1);

OK, that's it so far. Hope to have more to write soon ;)