• Building an AIM Bot in .Net using boo

    I’ve been working on a AIM Bot using the aimcc sdk. After signing up and downloading the sdk from developer.aim.com I found the documentation for .net lacking and found just getting up and running to be a long process. Below are some of the issues I ran into.

    **Application.Run()</p>

    </b>To start with I needed to create a class which extends Control. Then add a method to start the application, at the beginning of the method call CreateControl() at the end of the method you need to call Application.Run() to setup the message loop. I’m working in boo but this should work fine in c#.

    class Bot(Control):
    def Start():
    CreateControl()
    #AccSession setup to go here
    Application.Run()

    PInvoke

    Next pinvoke is required to creat the AccSession instance. This is in some of the SDK samples, but it wasn’t obvious looking at the java and c++ sample bots. It only calls acccore.dll, but other dll’s in the accsdk/dist/release/ are needed I’m just using the full path, but should copy the required dll’s to my project file.

    
    
    
        [DllImport(“C:\accsdk\dist\release\acccore.dll”, EntryPoint:“#111”, PreserveSig:false, SetLastError:true)] 
        private static def AccCreateSession(
        [MarshalAs(UnmanagedType.LPStruct)] riid as Guid,
        [MarshalAs(UnmanagedType.IDispatch)] ref session as object) as IntPtr:
            pass

    Calling the method is pretty strait forward, just pass in an object and then cast the object to a AccSession.


    CreateControl()
    o as object
    AccCreateSession(typeof(IAccSession).GUID, o)
    session = o as AccSession

    </pre>

    Login

    The next step was to wire up the events I wanted to get catch, I choose to wire up all the events for logging. You need to provide your API key and then login.


    handler = AccSessionEventHandler(BoxesImResponder())
    handler.WireSession(</em>session)
    session.ClientInfo.Property[AccClientInfoProp.AccClientInfoProp_Description] = key
    session.Identity = username
    session.SignOn(password)
    Application.Run()

    That’s finally all I needed to get the bot running. I fought with setting preferences for a long while like the java example shows, but I ended up following the c++ example to filter which messages get accepted. More to follow in later posts. In addition, I plan on posting the full source code when the application is working.

    </p>
  • Implied generic parameter types

    I’m not sure of the exact name of this feature, but I stumbled across it today. If a generic method uses the type for a parameter, the complier assumes the generic type, which makes for cleaner code.

    using System;

    class Program
    {
    static void Main()
    {
    MyClass.WriteInputStatic("test");
    MyClass obj = new MyClass();
    obj.WriteInput(12);
    }
    }

    public class MyClass
    {
    public static void WriteInputStatic<T>(T input) { Console.WriteLine(input); }
    public void WriteInput<T>(T input) { Console.WriteLine(input); }
    }

    I can’t think of many use cases, but kind of a cool feature anyway. I was using it to serialize and deserialize a type for testing the correct serialization/deserialization.

    internal static T Serialize<T>(T worker) where T : class
    {
        MemoryStream serializationStream = new MemoryStream();
    new BinaryFormatter().Serialize(serializationStream, worker);
    byte[] bytes = serializationStream.ToArray();

    MemoryStream deserializationStream = new MemoryStream(bytes);
    object obj = new BinaryFormatter().Deserialize(deserializationStream);
    return obj as T;
    }
    </p>
  • Instant wiki with rBuilder MediaWiki Appliance

    The MediaWiki Appliance is

    An appliance that bundles up PHP, MySQL, Apache and MediaWiki to provide a self-contained, turn-key Wiki appliance.</p>

    http://www.rpath.org/rbuilder/project/vehera-base/

    Setup was easy and quick and it runs in Virtual Server and Virtual PC 2007.

  • Overriding component parameters when using Castle.MicroKernel

    Overriding component parameters when using the Castle.MicorKernel turned out to be kinda weird. Anyway, here’s one way to do it.

    IoC.Container.Kernel.RemoveComponent("ComponentA");
    IoC.Container.Kernel.AddComponentInstance(
    "ComponentA",
    typeof (ComponentA),
    new ComponentA("Customer parameter for testing."));

    I needed to override the parameter value of ComponentA for some unit tests. The parameter is normally set in the configuration file. After trying a few different approaches, I ended up with the code above. It’s more of an end to end unit test, and as such ComponentA will be resolved as a result of another Component being resolved.

  • Unit Test Live Templates for ReSharper

    Attached are Live templates that can be imported into ReSharper to help with writing unit tests. There are templates for Test, Setup, TearDown, TestFixtureSetUp and TestFixtureTearDown methods.

    I find that failing test cases are really good reminders of things that need to get done, so often I will add test methods with Assert.Fail(); as the body, usually the method name is enough detail for me to remember what needs to be done.

    Lastly, I’ve started using the ConditionalAttribute on my test class methods so they won’t be compiled in release mode. If this is not your style you may want to modify the templates.

    ReSharperUnitTestLiveTemplates.xml (2.63 KB)

    **Update:
    ** I’ve decided against using the ConditionalAttribute in favor of adding compiler directives to the test files. I quickly found that not using the attribute became a source for failing tests that shouldn’t have been run and/or simple code leaks. I know that many people prefer a different class library for tests, but I think that only makes sense for non unit tests. Anyway, here’s the updated file.

    ReSharperUnitTestLiveTemplates1.xml (2.48 KB)

  • Setting up VS 2005 for nant build files

    To setup nant intellisense in VS 2005 you can copy the schema from the install to the Visual Studio Install\xml\schema directory, however this won’t get all available tasks. As such, I prefer to create a nant file which calls the nantschema task (http://nant.sourceforge.net/release/0.85/help/tasks/nantschema.html). This task requires an output attribute be set, which I just set to my Visual Studio Install\xml\schema directory. Below is the build script I use:

    <?xml version="1.0" encoding="utf-8"?>
    <project name="Build nant schema to VS 2005" default="build" basedir=".">
        <target name="build">
            <nantschema 
    output="C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\NAnt.xsd"
    target-ns="http://tempuri.org/nant-donotuse.xsd" /> </target> </project>

    You can run this after updating Nant or adding contrib projects to your installation and get the latest tasks.

    Finally, in your other build files add the xmlns attribute to the project element pointing to the name space you used as the target-ns, http://tempuri.org/nant-donotuse.xsd in the above example. You may also need to tell VS to open .build files with the xml editor, you can do this by right clicking on the .build file and choosing ‘open with’.

  • Sum a column in a tab-delimited file using Powershell

    Assuming you have a bunch of tab delimited files w/out headers and the second column as a number, you could use the following powershell command to sum the values and output the result.

    PS > $sum=0; ls | cat | %{ $.split(“`t”)[1] } | %{ $sum+=[int]$ }; $sum

    Sample File:

    a-count    7
    b-count 233
    c-count 32
    </p>