Font Size: A A A   Layout: Left | Right

58bits - Tech

Six bits short of sixty four...

# Sunday, June 08, 2008
Sunday, June 08, 2008 12:35:26 AM (SE Asia Standard Time, UTC+07:00) (C#)

I needed an undo stack today - but wanted it to behave like a queue when it was full, dropping the oldest items in the collection to make room for new ones. Found two posts - Undo Functionality with a Limited Stack, and also a reference to an identically named collection called LimitedStack here. 

Was initially thinking of simply writing a wrapper over an array of objects, shifting items along as new ones were added, but using a linked list is a clever solution, as it provides the queue like behavior needed without out any extra code via RemoveFirst() and RemoveLast().

using System;
using System.Collections.Generic;

namespace DotBits.Gallery.Builder.Objects
{
    [Serializable()]
    public class UndoStack<T> : LinkedList<T>
    {
        private int _maxItems;

        public int MaxItems
        {
            get 
            {
               return _maxItems; 
            }
            set
            {
                while (this.Count > value)
                {
                    this.RemoveFirst();
                }
                
                _maxItems = value;
            }
        }

        public UndoStack(int max)
        {
            _maxItems = max;
        }

        public T Peek()
        {
            return this.Last.Value;
        }

        public T Pop()
        {
            LinkedListNode<T> node = this.Last;
            this.RemoveLast();
            return node.Value;
        }

        public void Push(T value)
        {
            LinkedListNode<T> node = new LinkedListNode<T>(value);
            this.AddLast(node);

            if (this.Count > _maxItems)
            {
                this.RemoveFirst();
            }
        }
    }
}


Comments [0] | | #  
# Saturday, May 31, 2008
Saturday, May 31, 2008 6:31:49 PM (SE Asia Standard Time, UTC+07:00) (ASP.Net)

Visual Studio 2008 Professional shipped with the Visual Studio 2008 Team System unit test framework (which in Visual Studio 2005 was not available in the Professional version).

The test system is pretty cool - with excellent test code generating options and wizards. One really interesting feature is the ability to create tests for ASP.Net projects (file based, or application based). The tests created will be decorated as follows:

   1: /// <summary>
   2: ///A test for DemoTest
   3: ///</summary>
   4: [TestMethod()]
   5: [HostType("ASP.NET")]
   6: [UrlToTest("http://localhost/demo")]
   7: public void DemoTest()
   8: {
   9:     string input = "Boo";
  10:     string actual = "Boo1";
  11:     actual = input + "1";
  12:     Assert.AreEqual(input , actual);    
  13: }

 

Previously to the only way to test components in an ASP.Net application - especially one that depended on any custom configuration settings or configuration sections in the Web.config file - was to take a copy of all the required settings - and place them in an App.config file for your Unit Test project. Then using a test running framework like TestDriven.Net or NUnit - run the tests.

With the new Visual Studio 2008 test suite, you can simply use the Test Wizard to create the test, which will detect all the dependencies and create test methods that look like the one above. When run - the tests execute in the same processes as the referenced web application (and so use the same configuration settings).

If you want to debug your tests - you then have to attach the debugger to the either the development web server, or the IIS process that is hosting your local website.

Here's a link to an article that will show you how to do either - How to: Debug while Running a Test in an ASP.NET Solution.

However you have to re-attach the debugger to the process manually for every test run you want to debug, which is a drag. After googling for a while - I wasn't able to find an easy way to get F5 debugging that would automatically attach to the correct process (a command line option in the project maybe?). Still, the essence of the tests are to test assertions against your code and expected results so for a mature application test suite this isn't too big a problem. However for a TDD design approach - having to re-attach the debugger on each run is pretty inconvenient. Am I missing something?


Comments [0] | | #  
# Tuesday, May 27, 2008
Tuesday, May 27, 2008 2:31:26 PM (SE Asia Standard Time, UTC+07:00) (ASP.Net | Utilities)

Wow - Google and searchers were all over the "URI templates and hackable search engine friendly URLs" line in my short WPF post below, which means there's a lot of interest in URI templates.

I found this post on Creating a Template based URI very helpful. You DON'T have to be building a WCF service to use these. You MUST however include a reference to the System.ServiceModel.Web assembly in your project before you can use the System.UriTemplate type.

Scott Hanselman's presentation at MIX08 on MVC was also very helpful as well as it clarified many things (not to mention being hugely entertaining).

The URI Templates of .Net 3.5 don't have anything to do with REST - they're just a utility class that helps to bind and match templates and parameters to and from URIs. When combined with a URL rewriter they make it easy to present hackable URLs the world.  As Scott said in his presentation - the URLs in a browser window are effectively part of the UI - and so should be treated accordingly when possible.

So... mystore.com/clothing/shirts would be cool, as opposed to mystore.com/products.aspx?type=clothing&category=shirts


Comments [0] | | #  
# Saturday, May 24, 2008
Saturday, May 24, 2008 6:33:26 PM (SE Asia Standard Time, UTC+07:00) (Utilities)
While sprucing up my blog - I started looking for a code snippet plugin for Windows Live Writer - and Leo Vildosola's Code Snippet Plugin for WLW still appears to be a good choice. Like a lot of developers I use a 'dark theme' for Visual Studio and so I'm not sure that using the copy as HTML plugin from Colin Coller would work.
 

Here's an example of the WLW Code Snippet Plugin in action...

   1: /// <summary>
   2: /// Hash utility - pass the hash algorithm name as a string i.e. SHA256, SHA1, MD5 etc.
   3: /// </summary>
   4: /// <param name="input"></param>
   5: /// <param name="algorithm"></param>
   6: /// /// <param name="upperCase"></param>
   7: /// <returns>Hashed String</returns>
   8: public static string HashIt(string input, string algorithm, bool upperCase)
   9: {
  10:  
  11:     if (string.IsNullOrEmpty(input))
  12:         throw new ArgumentNullException("input");
  13:     
  14:     if(string.IsNullOrEmpty(algorithm))
  15:         throw new ArgumentNullException("algorithm");                        
  16:     
  17:     byte[] result = ((HashAlgorithm)CryptoConfig.CreateFromName(algorithm)).ComputeHash(Encoding.UTF8.GetBytes(input));
  18:  
  19:     StringBuilder myHexHash = new StringBuilder(64); //Maximum length required for SHA2-256, SHA1 or MD5
  20:  
  21:     string formatter = string.Empty;
  22:  
  23:     if (upperCase)
  24:         formatter = "{0:X2}";
  25:     else
  26:         formatter = "{0:x2}";
  27:  
  28:     for (int i = 0; i < result.Length; i++)
  29:     {
  30:         myHexHash.AppendFormat(formatter, result[i]);
  31:     }
  32:  
  33:     return myHexHash.ToString();
  34: }

Comments [0] | | #  
# Friday, May 23, 2008
Friday, May 23, 2008 7:40:07 PM (SE Asia Standard Time, UTC+07:00) (WPF)

Gallery.BuilderThere just aren't enough hours are there. Have just about managed to cobble together an improved looking skin for dasBlog. Will re-write my image gallery component soon with URI templates and hackable search engine friendly URLs.

The application I use to package up my images for delivery is called Gallery Builder (original) - and it's my WPF learning exercise. A hand-rolled set of control templates produced not a bad looking UI. Would love to write an effects control for generating the gallery cover image. Alas there may not be enough hours...    (click the image on the left to enlarge).


Comments [0] | | #  
# Tuesday, May 20, 2008
Tuesday, May 20, 2008 10:06:41 PM (SE Asia Standard Time, UTC+07:00) (General)

Well that's two out of four exams over with - and the end of the silent period on my blog. Successfully sat 'Information Security Management' and 'Introduction to Cryptography' over the past two days.

Both manageable papers, and the first two of six in what has turned out to be a really excellent programme. Check out the Information Security Group at RHUL for the gory details, or the syllabus posted at the London External Programme - MSc in Information Security.

Now that I have my life back... (at least for the summer), watch this space for a re-designed blog and new photo gallery over the coming weeks.


Comments [0] | | #  
# Wednesday, March 12, 2008
Wednesday, March 12, 2008 5:01:37 PM (SE Asia Standard Time, UTC+07:00) (Security)

These are interesting. And there I was feeling all warm and cosey with my trusted platform module (TPM). The RAM to USB utility is particularly cool.

http://citp.princeton.edu/memory/

http://www.cs.dartmouth.edu/~pkilab/sparks/


Comments [0] | | #  
# Saturday, January 26, 2008
Saturday, January 26, 2008 12:10:23 AM (SE Asia Standard Time, UTC+07:00) (Other Tech)

It's always great when you find a company that produces a quality product, backed up by good support.

For a while know I've been looking for a replacement to the Cisco VPN Client that I use to connect my client's VPN. Cisco have produced a version of this software for Vista - but have stated categorically that they will not be supporting the 64 Bit version of Windows. Instead they are expecting their customers to throw away perfectly good equipment - in favour of their new ASA platform of firewalls and gateways  - which in turn will use their new AnyConnect VPN software, which does of course support the 64 Bit Windows.

Along comes NCP and their NCP Secure Entry Client in both x86 and x64 flavours. A 'very' configurable VPN client with a few extras too boot. The CISCO VPN Client doesn't give much away in terms of the detailed settings required to establish an IPSec connection. And IPSec network connections operate in different modes, and have several configuration settings. That said, after a quick email to NCP support, and very helpful reply, I am now connecting just fine to my client's Cisco PIX.

ncp

Thank  you NCP.


Comments [0] | | #  
# Tuesday, December 18, 2007
Tuesday, December 18, 2007 4:42:04 PM (SE Asia Standard Time, UTC+07:00) (Other Tech | Security)

I posted here a while ago about setting up BitLocker on my PC - without a TPM. Works great.

I've recently been building up a new machine that will become my main development PC in the New Year - having decided to follow in the footsteps of others and build a decent spec Vista Ultimate 64 Bit box.

The spec:

Shuttle SP35P2 Pro
G.Skill 8GB 4-4-4-12 RAM
XFX NVidia GT 8800 Video
WD Raptor 10,000 RPM SATA for OS
SD Barracuda 300GB for Data
Vista 64 Ultimate 64 Bit

Anyway - more on this box later - which of course is going to be way better than others . :-)


BitLocker and EFS are now standard on my PCs and any new notebook I buy will have a TPM in it for sure.

That said - I'll be darned if I could get BitLocker to find the USB flash drive on this new PC to load the BitLocker keys at startup.

There are plenty of threads out there on the topic. Search for 'Bitlocker unable to read USB drive', or 'Bitlocker cannot find keys on USB drive'.

None of these helped me in this case.

Here's the solution (at least for this machine - with a Phoenix Award BIOS - V6.00PG - on an Intel P35 Express Chipset + ICH9R).

1. Be sure to put your USB keyring or flash drive in first!

2. Restart and enter your PC's BIOS (DEL at startup)

3. Go to the Integrated Peripherals menu item

4. Go to your USB Device Settings menu item

5. Set the USB controller to 'Enabled', 'High Speed' and the USB Storage function to 'Enabled'.

6. Here's the trick.... You should see a line like the one below with your USB thumbdrive listed.

*** USB Mass Storage Device Boot Settings***

[Yourdrive MFG name here]   [Auto]

The default is 'Auto'. Change it to 'HDD' and presto - Bitlocker will find the USB drive.

 

Took me ages to figure this one out...


Comments [0] | | #  
# Wednesday, October 03, 2007
Wednesday, October 03, 2007 2:26:09 PM (SE Asia Standard Time, UTC+07:00) ()

It's my data and I'll open it if I want to... right? Errr.. not quite. If you've EFS encrypted files on Windows Vista you will not be able to open them under Windows XP. I'd thought I had most of my recovery scenarios covered. Was about to prep my XP Pro notebook for a trip and wanted to take some EFS secured data with me as well...

The following KB article applies...

Error message when you try to open an EFS-encrypted file in Windows XP or in Windows Server 2003 after the file has been opened in Windows Vista: "Access is denied"


Comments [0] | | #