Visual Studio 2012 and Microsoft XNA Game Studio

Ryan Lange has provided a handy guide how to get XNA studio projects to run in Visual Studio 2012. But this is on the proviso you already have Microsoft XNA Game Studio already installed on the machine.

I had a clean machine with a VS 2012 install, and as I soon found out, installing XNA game studio is no trivial matter. The 43 MB XNA Game Studio has a dependency on Visual C# 2010 Express, which in turn installs SQL Server Express and a load of junk that threatens to use 898 Mb of disk space.

Luckily, I already had XNA Game Studio installed on another machine, and the process was reasonably straightforward.

  1. Copy the files from C:\Program Files (x86)\Microsoft XNA\XNA Game Studio\v4.0
  2. Make the following Registry Entry (required for BuildXAct task):Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\XNA\Game Studio\v4.0]
    “InstallPath”=”C:\\Program Files (x86)\\Microsoft XNA\\XNA Game Studio\\v4.0″

  3. Copy the files from C:\Program Files (x86)\MSBuild\Microsoft\XNA Game Studio
  4. Register the XNA assemblies in the GAC using gacutil
    cd C:\Program Files (x86)\Microsoft XNA\XNA Game Studio\v4.0\References\Windows\x86
    gacutil /i Microsoft.Xna.Framework.Avatar.dll
    REM repeat for each of the DLL in this directory
    
  5. I’m not sure if this is entirely necessary – Run the Redistributable MSI in
    C:\Program Files (x86)\Microsoft XNA\XNA Game Studio\v4.0\Redist\XNA FX Redist\xna_fxredist.msi

And that was about it. I was able to compile StarWarrior from Artermis Entity Component Framework.

Let me know if you find anything else.

Banning spammers on a low traffic site

I’ve a low/zero traffic site that attracts more bots than human visitors. Here’s a simple Python script that looks through the logs and bans visitors who try to spam the comments page.

import os, time
banned_ips  = []
while True:
    fd = os.popen("tail access_log | grep commentStart")
    txt = fd.read()
    fd.close()
    for line in txt.split('\n'):
        ip = line.split(' ')[0]
        if ip and not ip in banned_ips:
            banned_ips.append(ip)
            print "Banning", ip
            os.system('sudo iptables -A INPUT -s %s/24 -j DROP' % ip)
    time.sleep(1)

In addition, project honeypot is interesting as well.

Shopping Cart Schema

My recent freelance work involved writing a shopping cart and integration with a hosted payment gateway. How hard could it be? Well, as it turns out, there were quite a few elements that I missed in my initial design.

Shopping Cart

A shopping cart has the following attributes

  • Cart Id
  • Date created
  • Date modified – used to identify cart abandonment
  • Customer reference – customer can include their notes/comments
  • Status – Open, locked, completed
  • Transaction Type – You might wonder what the transaction type is doing here, but in the case of hosted payment pages, such as paypal or gumtree, we need to lock the cart. When the payment processor acknowledges payment, we can mark the entry here. In my case, some customers have accounts which get billed at the end of the month.
  • LockedTotalCostAfterTax – Just like Amazon, while the cart is open, the prices of items in the cart can be changed by the store. When the cart is locked, the prices are locked in.
  • Currency – Some items are priced differently depending on the currency displayed. e.g. 3.99 Euro, 8.99 USD.
  • TransactionReference – Used for integration with payment provider
  • OnBehalfOfUserId – A cart may be initiated by a staff member, and in this case, the shopping cart is actually created for a particular user.

When payment is processed, these details are copied over to the General Ledger subsystem.

Postscript

In hindsight, starting off with the PayPal schema would have been a lot easier.

PayPal Shopping Cart Schema

Enterprise Library – CustomTraceListener that logs to a Ring Buffer

Configuring Enterprise Library is actually harder than writing a custom trace listener. There was one catch though. A CustomTraceListener doesn’t inherit from FormattedTextWriterTraceListener. The format specifier of a log entry is not respected. I had to get the EntLib sources to figure out what was going on. In the end it was a matter of overriding TraceData and calling the Formatter.Format().

namespace Cognoware
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Cognoware.Collections.Generic;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
    using System.Diagnostics;
    using Microsoft.Practices.EnterpriseLibrary.Logging;

    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class CircularTraceListener: CustomTraceListener
    {
        private static RingBuffer<string> _ringBuffer = new RingBuffer<string>(200);
        private string tmp = String.Empty;
        public override void Write(string message)
        {
            tmp += message;
        }

        public override void WriteLine(string message)
        {
            _ringBuffer.Add(tmp + message);
            tmp = String.Empty;
        }

        public IEnumerable<string> Messages
        {
            get
            {
                return _ringBuffer.AsEnumerable<string>();
            }
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
            {
                if (data is LogEntry)
                {
                    if (this.Formatter != null)
                    {
                        WriteLine(this.Formatter.Format(data as LogEntry));
                    }
                    else
                    {
                        base.TraceData(eventCache, source, eventType, id, data);
                    }
                }
                else
                {
                    base.TraceData(eventCache, source, eventType, id, data);
                }
            }
        }
    }
}

Configuring the CustomTraceListener:

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            type="Sample.CircularTraceListener, CircularTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            name="CircularTraceListener" formatter="Brief Formatter" />       
    </listeners>
    <formatters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="{timestamp}  {message} {title} {dictionary({key} - {value}{newline})}"
            name="Brief Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="CircularTraceListener" />
            </listeners>
        </add>
    </categorySources>
</loggingConfiguration>

The RingBuffer code came from Florian Reischl. I’ve posted the code in here for completeness.

/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
*/


/// <summary>
/// Represents a fixted length ring buffer to store a specified maximal count of items within.
/// </summary>
/// <typeparam name="T">The generic type of the items stored within the ring buffer.</typeparam>
[DebuggerDisplay("Count = {Count}")]
public class RingBuffer<T> : IList<T>, ICollection<T>, 
                             IEnumerable<T>, IEnumerable {
   /// <summary>
   /// Creates a new instance of a <see cref="RingBuffer<T>"/> with a 
   /// specified cache size.
   /// </summary>
   /// <param name="capacity">The maximal count of items to be stored within 
   /// the ring buffer.</param>
   public RingBuffer(int capacity) {
      // validate capacity
      if (capacity <= 0)
         throw new ArgumentException("Must be greater than zero", "capacity");
      // set capacity and init the cache
      Capacity = capacity;
      _buffer = new T[capacity];
   }

   /// <summary>
   /// the internal buffer
   /// </summary>
   T[] _buffer;
   /// <summary>
   /// The all-over position within the ring buffer. The position 
   /// increases continously by adding new items to the buffer. This 
   /// value is needed to calculate the current relative position within the 
   /// buffer.
   /// </summary>
   int _position;
   /// <summary>
   /// The current version of the buffer, this is required for a correct 
   /// exception handling while enumerating over the items of the buffer.
   /// </summary>
   long _version;

   /// <summary>
   /// Gets or sets an item for a specified position within the ring buffer.
   /// </summary>
   /// <param name="index">The position to get or set an item.</param>
   /// <returns>The fond item at the specified position within the ring buffer.
   /// </returns>
   /// <exception cref="IndexOutOfRangeException"></exception>
   public T this[int index] {
      get {
         // validate the index
         if (index < 0 || index >= Count)
            throw new IndexOutOfRangeException();
         // calculate the relative position within the rolling base array
         int index2 = (_position - Count + index) % Capacity;
         return _buffer[index2]; 
      }
      set { Insert(index, value); }
   }

   /// <summary>
   /// Gets the maximal count of items within the ring buffer.
   /// </summary>
   public int Capacity { get; private set; }
   /// <summary>
   /// Get the current count of items within the ring buffer.
   /// </summary>
   public int Count { get; private set; }
   
   /// <summary>
   /// Adds a new item to the buffer.
   /// </summary>
   /// <param name="item">The item to be added to the buffer.</param>
   public void Add(T item)
   {
       // avoid an arithmetic overflow
       if (_position == int.MaxValue)
           _position = _position%Capacity;
       // add a new item to the current relative position within the
       // buffer and increase the position
       _buffer[_position++ % Capacity] = item;
       // increase the count if capacity is not yet reached
       if (Count < Capacity) Count++;
       // buffer changed; next version
       _version++;
   }

   /// <summary>
   /// Clears the whole buffer and releases all referenced objects 
   /// currently stored within the buffer.
   /// </summary>
   public void Clear() {
      for (int i = 0; i < Count; i++)
         _buffer[i] = default(T);
      _position = 0;
      Count = 0;
      _version++;
   }

   /// <summary>
   /// Determines if a specified item is currently present within
   /// the buffer.
   /// </summary>
   /// <param name="item">The item to search for within the current
   /// buffer.</param>
   /// <returns>True if the specified item is currently present within 
   /// the buffer; otherwise false.</returns>
   public bool Contains(T item) {
      int index = IndexOf(item);
      return index != -1;
   }

   /// <summary>
   /// Copies the current items within the buffer to a specified array.
   /// </summary>
   /// <param name="array">The target array to copy the items of 
   /// the buffer to.</param>
   /// <param name="arrayIndex">The start position witihn the target
   /// array to start copying.</param>
   public void CopyTo(T[] array, int arrayIndex) {
      for (int i = 0; i < Count; i++) {
         array[i + arrayIndex] = _buffer[(_position - Count + i) % Capacity];
      }
   }

   /// <summary>
   /// Gets an enumerator over the current items within the buffer.
   /// </summary>
   /// <returns>An enumerator over the current items within the buffer.
   /// </returns>
   public IEnumerator<T> GetEnumerator() {
      long version = _version;
      for (int i = 0; i < Count; i++) {
         if (version != _version)
            throw new InvalidOperationException("Collection changed");
         yield return this[i];
      }
   }

   /// <summary>
   /// Gets the position of a specied item within the ring buffer.
   /// </summary>
   /// <param name="item">The item to get the current position for.</param>
   /// <returns>The zero based index of the found item within the 
   /// buffer. If the item was not present within the buffer, this
   /// method returns -1.</returns>
   public int IndexOf(T item) {
      // loop over the current count of items
      for (int i = 0; i < Count; i++) {
         // get the item at the relative position within the internal array
         T item2 = _buffer[(_position - Count + i) % Capacity];
         // if both items are null, return true
         if (null == item && null == item2)
            return i;
         // if equal return the position
         if (item != null && item.Equals(item2))
            return i;
      }
      // nothing found
      return -1;
   }

   /// <summary>
   /// Inserts an item at a specified position into the buffer.
   /// </summary>
   /// <param name="index">The position within the buffer to add 
   /// the new item.</param>
   /// <param name="item">The new item to be added to the buffer.</param>
   /// <exception cref="IndexOutOfRangeException"></exception>
   /// <remarks>
   /// If the specified index is equal to the current count of items
   /// within the buffer, the specified item will be added.
   /// 
   /// <b>Warning</b>
   /// Frequent usage of this method might become a bad idea if you are 
   /// working with a large buffer capacity. The insertion of an item
   /// at a specified position within the buffer causes causes all present 
   /// items below the specified position to be moved one position.
   /// </remarks>
   public void Insert(int index, T item) {
      // validate index
      if (index < 0 || index > Count)
         throw new IndexOutOfRangeException();
      // add if index equals to count
      if (index == Count) {
         Add(item);
         return;
      }

      // get the maximal count of items to be moved
      int count = Math.Min(Count, Capacity - 1) - index;
      // get the relative position of the new item within the buffer
      int index2 = (_position - Count + index) % Capacity;

      // move all items below the specified position
      for (int i = index2 + count; i > index2; i--) {
         int to = i % Capacity;
         int from = (i - 1) % Capacity;
         _buffer[to] = _buffer[from];
      }

      // set the new item
      _buffer[index2] = item;

      // adjust storage information
      if (Count < Capacity) {
         Count++;
         _position++;
      }
      // buffer changed; next version
      _version++;
   }

   /// <summary>
   /// Removes a specified item from the current buffer.
   /// </summary>
   /// <param name="item">The item to be removed.</param>
   /// <returns>True if the specified item was successfully removed
   /// from the buffer; otherwise false.</returns>
   /// <remarks>
   /// <b>Warning</b>
   /// Frequent usage of this method might become a bad idea if you are 
   /// working with a large buffer capacity. The removing of an item 
   /// requires a scan of the buffer to get the position of the specified
   /// item. If the item was found, the deletion requires a move of all 
   /// items stored abouve the found position.
   /// </remarks>
   public bool Remove(T item) {
      // find the position of the specified item
      int index = IndexOf(item);
      // item was not found; return false
      if (index == -1)
         return false;
      // remove the item at the specified position
      RemoveAt(index);
      return true;
   }

   /// <summary>
   /// Removes an item at a specified position within the buffer.
   /// </summary>
   /// <param name="index">The position of the item to be removed.</param>
   /// <exception cref="IndexOutOfRangeException"></exception>
   /// <remarks>
   /// <b>Warning</b>
   /// Frequent usage of this method might become a bad idea if you are 
   /// working with a large buffer capacity. The deletion requires a move 
   /// of all items stored abouve the found position.
   /// </remarks>
   public void RemoveAt(int index) {
      // validate the index
      if (index < 0 || index >= Count)
         throw new IndexOutOfRangeException();
      // move all items above the specified position one step
      // closer to zeri
      for (int i = index; i < Count - 1; i++) {
         // get the next relative target position of the item
         int to = (_position - Count + i) % Capacity;
         // get the next relative source position of the item
         int from = (_position - Count + i + 1) % Capacity;
         // move the item
         _buffer[to] = _buffer[from];
      }
      // get the relative position of the last item, which becomes empty
      // after deletion and set the item as empty
      int last = (_position - 1) % Capacity;
      _buffer[last] = default(T);
      // adjust storage information
      _position--;
      Count--;
      // buffer changed; next version
      _version++;
   }

   /// <summary>
   /// Gets if the buffer is read-only. This method always returns false.
   /// </summary>
   bool ICollection<T>.IsReadOnly { get { return false; } }

   /// <summary>
   /// See generic implementation of <see cref="GetEnumerator"/>.
   /// </summary>
   /// <returns>See generic implementation of <see cref="GetEnumerator"/>.
   /// </returns>
   IEnumerator IEnumerable.GetEnumerator() {
      return this.GetEnumerator();
   }
}

How to write code that syncs one table to another

  1. Source must have a timestamp column
  2. Target must remember the last timestamp processed.
  3. Source queries target for the last processed timestamp
  4. Source checks whether target
    1. is still importing previous data
    2. or has data that it hasn’t processed yet (import could have been killed halfway)
  5. If target is available to import data, source uploads data and triggers processing
  6. As data is imported, target saves the last processed timestamp
  7. Errors, exceptions are accumulated and reported

It is also handy to have a script that monitors the status of the batch process.

  1. is processing
  2. last timestamp processed
  3. number of rows left

Learning SWIG

Warning, I don’t know SWIG. Here’s my notes as a total neophyte.

All posts in the series

  1. Learning SWIG
  2. Anatomy of SWIG
  3. Automatic garbage collection of opaque pointers in SWIG

Here’s a simple one from sql.h

   %module sql
   SQLRETURN SQL_API SQLAllocEnv(SQLHENV *OUTPUT);

The above is clearly not going to work without some defines

   %module sql
   
   typedef short           SQLSMALLINT;
   typedef SQLSMALLINT     SQLRETURN;
   /* SQL_API is __stdcall on Windows */
   #define SQL_API
   
   SQLRETURN SQL_API SQLAllocEnv(SQLHENV *OUTPUT);

Taking a look at the generated code, and it is incorrect because we want to call the function without arguments. i.e. in python env = SQLAllocEnv().

if (!PyArg_ParseTuple(args,(char *)"O:SQLAllocEnv",&amp;obj0)) SWIG_fail;

Sounds like we’ll need one of those dreaded typemaps. Here’s the result after a few attempts. The plan is as follows, we’ll define a local SQLHENV called env, and then assign it’s address to arg1, which will be passed to SQLAllocEnv. Observe the absence of semicolons.

%module sql

typedef short           SQLSMALLINT;
typedef SQLSMALLINT     SQLRETURN;
/* SQL_API is __stdcall on Windows */
#define SQL_API

%typemap(in, numinputs=0) SQLHENV *OUTPUT
(
   SQLHENV env
)
{
   $1 = &amp;amp;env
}

SQLRETURN SQL_API SQLAllocEnv(SQLHENV *OUTPUT);

The result looks a bit better.

SQLHENV *arg1 = (SQLHENV *) 0 ;
SQLHENV env1 ;
SQLRETURN result;

{
  arg1 = &amp;env1
}
if (!PyArg_ParseTuple(args,(char *)":SQLAllocEnv")) SWIG_fail;
result = (SQLRETURN)SQLAllocEnv(arg1);
resultobj = SWIG_From_short((short)(result));
return resultobj;

We’re passing in the right arguments, but we are returning the SQLRETURN. SQLRETURN is an error code, which we should check and probably raise an exception. But first, we should probably cast the SQLHENV to a pointer type. (My preference would be a reference-counted pointer type, so that we can call SQLDealloc automatically, but I don’t know how yet).

To create a pointer to the SQLHENV, I used the SWIG_NewPointerObj incantation. Where did I get this from? I cheated. I created a prototype like this SQLHENV Dummy(); and observed what Swig generated. This is almost certainly not the right way to do this.

%typemap(argout) SQLHENV *OUTPUT
{
   $result = SWIG_NewPointerObj((SQLHENV *)memcpy((SQLHENV *)malloc(sizeof(SQLHENV)),$1,sizeof(SQLHENV)), SWIGTYPE_p_SQLHENV, SWIG_POINTER_OWN |  0 );
}

However, the wrapper code generated is almost decent.

result = (SQLRETURN)SQLAllocEnv(arg1);
resultobj = SWIG_From_short((short)(result));
{
  resultobj = SWIG_NewPointerObj((SQLHENV *)memcpy((SQLHENV *)malloc(sizeof(SQLHENV)),arg1,sizeof(SQLHENV)), SWIGTYPE_p_SQLHENV, SWIG_POINTER_OWN |  0 );
}

It would be a good time to check for exceptions.

/* I found it necessary to add the following include */
%include &quot;exception.i&quot;
%typemap(ret) SQLRETURN
{
   if (!SQL_SUCCEEDED($1)) SWIG_exception(SWIG_RuntimeError, &quot;failed!&quot;);
}

Which gives us the following:

 #ifdef __cplusplus
 extern &quot;C&quot; {
 #endif
 SWIGINTERN PyObject *_wrap_SQLAllocEnv(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
   PyObject *resultobj = 0;
   SQLHENV *arg1 = (SQLHENV *) 0 ;
   SQLHENV env1 ;
   SQLRETURN result;

   {
     arg1 = &amp;amp;env1
   }
   if (!PyArg_ParseTuple(args,(char *)&quot;:SQLAllocEnv&quot;)) SWIG_fail;
   result = (SQLRETURN)SQLAllocEnv(arg1);
   resultobj = SWIG_From_short((short)(result));
   {
     resultobj = SWIG_NewPointerObj((SQLHENV *)memcpy((SQLHENV *)malloc(sizeof(SQLHENV)),arg1,sizeof(SQLHENV)), SWIGTYPE_p_SQLHENV, SWIG_POINTER_OWN |  0 );
   }
   {
     if (FAILED(result)) SWIG_exception(SWIG_RuntimeError, &quot;failed!&quot;);
   }
   return resultobj;
 fail:
   return NULL;
 }

I don’t really like the SWIG_NewPointerObj bit. It seems a little too error-prone. Instead, I investigated an alternate approach of letting SWIG generate all its typemaps.

%module sql

typedef short           SQLSMALLINT;
typedef SQLSMALLINT     SQLRETURN;
/* SQL_API is __stdcall on Windows */
#define SQL_API

%rename(SQLAllocEnv) SQLAllocEnv_new;
%inline %{
SQLHENV SQLAllocEnv_new()
{
    SQLHENV hEnv;
    SQLRETURN result = SQLAllocEnv(&amp;amp;hEnv);
    if (SQL_SUCCEEDED(result))
        return hEnv;
}
%}

The result is better, but now I’ve lost my exception handling capability.

SWIGINTERN PyObject *_wrap_SQLAllocEnv(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  SQLHENV result;

  if (!PyArg_ParseTuple(args,(char *)":SQLAllocEnv")) SWIG_fail;
  result = SQLAllocEnv_new();
  resultobj = SWIG_NewPointerObj((SQLHENV *)memcpy((SQLHENV *)malloc(sizeof(SQLHENV)),&amp;result,sizeof(SQLHENV)), SWIGTYPE_p_SQLHENV, SWIG_POINTER_OWN |  0 );
  return resultobj;
fail:
  return NULL;
}

With a little help from StackOverflow, we are now closer to a final solution.


%module sql

typedef short           SQLSMALLINT;
typedef SQLSMALLINT     SQLRETURN;
/* SQL_API is __stdcall on Windows */
#define SQL_API

%exception SQLAllocEnv_new {
    $action;
    if (!SQL_SUCCEEDED($1)) SWIG_exception(SWIG_RuntimeError, "failed!");
}

%rename(SQLAllocEnv) SQLAllocEnv_new;
%inline %{
SQLHENV SQLAllocEnv_new()
{
    SQLHENV hEnv;
    SQLRETURN result = SQLAllocEnv(&amp;hEnv);
    if (SQL_SUCCEEDED(result))
        return hEnv;
}
%}

This gives us an almost equivalent result to the first attempts.

SQLHENV SQLAllocEnv_new()
{
    SQLHENV hEnv;
    SQLRETURN result = SQLAllocEnv(&amp;hEnv);
    if (SQL_SUCCEEDED(result))
        return hEnv;
}

#ifdef __cplusplus
extern "C" {
#endif
SWIGINTERN PyObject *_wrap_SQLAllocEnv(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  SQLHENV result;
  
  if (!PyArg_ParseTuple(args,(char *)":SQLAllocEnv")) SWIG_fail;
  {
    result = SQLAllocEnv_new();;
    if (!SQL_SUCCEEDED($1)) SWIG_exception(SWIG_RuntimeError, "failed!");
  }
  resultobj = SWIG_NewPointerObj((SQLHENV *)memcpy((SQLHENV *)malloc(sizeof(SQLHENV)),&amp;result,sizeof(SQLHENV)), SWIGTYPE_p_SQLHENV, SWIG_POINTER_OWN |  0 );
  return resultobj;
fail:
  return NULL;
}

Using Reactive Extensions to Delay Searches

Here’s some notes from experimenting with Reactive Extensions to clean up an otherwise messy tangle of state and event handlers.

The result required is a TextBox which acts as a filter on a set of results. However, we do not want the filter to execute immediately after typing, but instead wait until there are no more KeyDown events for a second or so.

Here’s a step by step account: (You will require the Reactive Extensions, downloadable via NuGet)


       IDisposable keyPressSubscription = null;

       public void InitSearchBox()
        {
            // Show a label 'Searching...' only when filtering is being performed
            // Hide the label for now
            this.SearchingLabel.Visibility = Visibility.Collapsed;

            // Create a stream of keyPressEvents
            var keyPressEvents =
                Observable.FromEventPatter<KeyEventHandler, KeyEventArgs>(
                (KeyEventHandler ev) => { SearchTextBox.KeyDown += ev; },
                (KeyEventHandler ev) => { SearchTextBox.KeyDown -= ev; });

            // Throttle the events so that we don't get more than one 
            // event every 1500 milliseconds. This will hold back the event
            // until there is a momentary pause in key presses.
            keyPressSubscription = keyPressEvents
                .Throttle(TimeSpan.FromMilliseconds(1500))
                .ObserveOnDispatcher()
                .Subscribe(InitiateSearch);
        }

        public void InitiateSearch(EventPattern<KeyEventArgs> e)
        {
            this.Searching.Visibility = Visibility.Visible;

            // Simulate searching for 1 second, then
            // hide the 'searching...' notification
            Observable.Timer(TimeSpan.FromMilliseconds(1000))
                .ObserveOnDispatcher()
                .Take(1)  // Timer is an infinite stream. By specifying we are 
                          // only interested in one value, this allows the timer
                          // to be disposed.
                .Subscribe((e) =>
                {
                    this.Searching.Visibility = Visibility.Collapsed;
                });
            
        }

        // IDisposable
        public void Dispose()
        {
           // Prevent memory leaks
           keyPressSubscription.Dispose();
        }

Fatal: git-http-push failed

I had trouble pushing changes to one of my git repositories. The error message was:

Fatal: git-http-push failed

Looking at server logs

60.xxx.xxx.205 - xxx.xxx [06/Mar/2012:21:37:56 -0500] "MKCOL /git/Test.git/objects/77/ HTTP/1.1" 403 489

It appears that file permissions were the issue.

In my case, the Git repository was created using virtualmin, and the owner was www-data. CGI programs were run under suexec, where the program was executed using the file owner’s permissions. For suexec to work, the owner needs write permissions to the directories. The following was sufficient to do the trick.

sudo chmod -R u+w Test.git

References:
Problem with HTTP Verbs
How to troubleshoot git server issues

How to block bots from seeking exploits on your web server with Fail2Ban

Source: Exploit scanner, secure your Apache

Steps:

  • sudo apt-get install fail2ban
  • edit /etc/fail2ban/jail.conf
    [apache-nohome]
    enabled = true
    port    = http,https
    filter  = apache-nohome
    logpath = /var/log/apache*/*error.log
    maxretry = 5
    
  • fix a bug in the filter /etc/fail2ban/filter.d/apache-nohome.conf
    failregex = [[]client (?P<host>\S*)[]] File does not exist:
    
  • Restart /etc/init.d/fail2ban restart
  • Check that fail2ban has started.
     sudo fail2ban-client status
    Status
    |- Number of jail:      2
    `- Jail list:           apache-nohome, ssh