Thursday, 4 June 2015

Something Interesting has appeared in my notification area

Something interesting has appeared in the notification area of my windows 8.1 task bar.
Can you spot it in the picture below? -Click on Image to enlarge.

Time to do some backup and prepare!

Monday, 11 May 2015

A New Begining

To be precise, a Brand New Begining.

Not long ago I posted this:
End of a fantastic Server 2000-2015

Since then I have been looking at what I can get to replace it. I have a US Robotics Router that serves as the internet gateway, I need a gigabit Ethernet switch (my current one is 100 Mbps), it will draw considerably less power than my Server did, but provide many functionalities that are important for me.

After long tiresome searches and consulting with people who have owned network devices from interested brands, I came across an alien face hugger; Whoops, I mean a Netgear Nighthawk X6 R8000.
Behold:
Ahh the sight and smell of new gear!

In the picture above is the face hugger, along with Belkin surge protector (I need the extra sockets), and a Western Digital 1 TB 2.5" USB 3.0 HDD. Wow what a mouth full that was!

Here is what I wanted:
-Good filtering and well configurable firewall.
-Network sharing my printer.
-Some kind of NAS like functionality to a connected drive.
-God awesome Radio for wireless.
-Built in Gigabit switch

Here is what the Netgear Nighthawk x6 R8000 delivers:
-All of the above.
-THREE RADIOS with the ability to join the 5GHz radios for automatic channel balancing.
-BONUS unexpected awesomeness: I can segment my network between the LAN side and the WAN side!
-BONUS expected(from reading specs) awesomeness: FTP server I can access from the internet as well as internal network.
-BONUS expected(from reading specs) awesomeness: TIVO media server compatible!

If I were asked to describe this awesome face hugger in one word, it would be "Incredible".

I can list and talk about all its features but that would be a large post, so I will only talk about the features important to me. You can read the full awesomeness, eh, I mean specs on the Netgear website.

On my server, one of the cool things I liked was the network segmentation that kept the internet side of the network different to the LAN side of the network. When setting up the netgear router I found that what it called Internet setup is really a WAN/LAN bridge. As a result, the ip addresses need to be different for the router(X6) and your gateway (modem, fiber modem, etc). My US Robotics allows a VLAN to be created which can be on a different network than its own network. What I did was do just that, the VLAN on the USR is on the X6 side of the network(my LAN) and the USR's address is on a different network. The USR itself creates a WAN bridge to the ISP but this is invisible to the X6 and it doesn't care either. So in the internet setup of the X6, I assigned an ip address for the routers network side and a gateway which is the USR's router address. I can still access the USR via the VLAN address for maintenance allowing me to disable internet remote access for security. Connecting wireless devices use the address provided by the X6 via its DHCP and get given the gateway address that is on the USR's lan side.

There is another advantage to this segmentation. I can fully utilise the firewall functions of both routers. More than that, I can setup the FTP service on the X6 and rather than fiddle with its accessibility, I can simply turn on or off the pinhole on my USR. DNS services are provided by the USR, which the X6 happily passes to connecting devices.

It just works, and works wonderfully. I checked the range and found that the 2.4 GHz radio gets me to about 30 Metres, and the 5 GHz radio manages 14 Metres. This is despite having the router placed inside an old concrete building! The 5 GHz range is lower because usually the lower frequency will travel further than the higher frequency, so not even this router can change the laws of physics. However in saying this, the stability and range seriously blows away any wireless devices I have owned.

It was so easy to configure the external hard drive. In fact it was so easy, you just plug it in and that's it, you have network shared drive! The only time you will need to change the work group on the router is if you use a different Workgroup for your windows computers. That is the only thing I changed.

Of course, I know what you are REALLY waiting for, you want to see the un-boxing pictures.
Well, here they are:
First opening of the box

What is sitting underneath the router in the box

Top view straight out of the box. The black strips on the label is my paint program edit so you don't eyeball stuff where it says no stuff eye balling. mmk?

Bottom view

Rear view

First power on!

It is now settled in to my place. Shes mine now!


I still miss my Linux sever and it will still return one day, but the Netgear Nighthawk X6 R8000 does a damn good job of replacing it at a considerably reduced power consumption. It is by far the best network product I have ever owned (previously my USR). Well done Netgear with this one! Cant wait to see what they will think of next!

Saturday, 2 May 2015

C# Parameters, Value or Reference? -Explained.

C# Pass by Reference vs value

C# is one of my favorite languages with a chief architect that is also my favorite language designer. After working with C# for some time, I know C# pretty well however being an expert in it is something I am yet to achieve. There are so many things to remember about it that you really need a reference nearby. Alternatively you can browse blogs by blog posters posting their experience with the language! The other equation of programming is good design patterns so this brings me to another part of C# that I like. It makes it quite intuitive to implement design patterns such as three-tier Programming Architecture.

Recently I had a discussion with someone about whether C# passed by reference or value. It isn't a simple an answer as one would expect at the onset, however I'll explain with examples.

Firstly what does the C# specification say about parameter passing?
C# specification 1.6.6 (Version 5.0) states that parameters are passed by value. This is where the confusion arises.

Consider:
private void PassByValue(int index)
{
    index = 10;
}
What happens in the above code? The original Index remains unchanged but the index inside the method changes to 10. This is the correct behavior as outlined in the C# Specification.

The confusion arises when we do this:
private void PassByValue(int[] index)
{
    index[0] = 10;
}
What happens in the above code? In the first example and according to the C# specification if not well understood would have you believe that  the array "index" was passed by value, correct. That changing the value inside the method for cell one will not change the original, which is incorrect!

So what happens is that C# passes everything that is not a primitive, implicitly, as reference. If I just left it at that, then that statement is not entirely correct and that is where the confusion arises. When is a value not a value? When it is a reference! The array is actually a reference to memory location and when you pass the array to the method, you are passing the "value" of that reference to the method. Hence when you change a "cell" inside the array, you are changing the value pointed to by the reference. Since the original reference is not broken this change is reflected in the original. However if you changed the "value" i.e you changed the reference which was passed by value, you are breaking that therefore the original remains unchanged and you are working on a "new" reference.
Hence if you did this:
private void PassByValue(int[] index)
{
    index = new int[1];
    index[0] = 10;
}
In the above code, the reference to the original is broken. You are now working on a new reference and changing this will have no effect on the original. This does not break the C# specification because you passed by value, except in this case that value is the reference. Also the original is not left "dangling" simply because in C#, much like java, an object is never GC'ed if there is still a reference to it (the calling scope in the above example).

In order to maintain the changes you make to an object in C# in the original passed from outside the method, you need to tell C# that you want to pass a reference to a reference and that is done using the ref keyword.
The code below:
private void PassByReference(ref int[] index)
{
    index = new int[1];
    index[0] = 10;
}
The code above will reassign the reference the original is pointing to, to the new location created in-scope of the method it was passed to. So cell 0 of the original will show the value changed to 10.
This is also another advantage of C# over C++. If you did this in C++ you will get a "dangling pointer" where by once the method goes out of scope, the "new" object created inside the method is destroyed, but the original pointer is changed to point to nothing. This also highlights why C# is considered a "safe" language (Chapter 18, Paragraph 1, C# Language specification version 5.0).

If you do not understand this behavior correctly, you will either abuse the use of "ref" and "out" Modifiers or you will create a bug in your code and have trouble finding out and even understanding what went wrong.

Here is another example:
//Class for testing.
public class DataStorage
{
    public string FirstName; //Field
    public string LastName { get;  set; } //Property
}

//The test Method
private void TestCase(DataStorage ds)
{
    ds.FirstName = "John";
    ds.LastName = "Smith";
}
In the above example, the DataStorage instance passed by the caller to the TestCase Method will have its members changed to reflect the new values set by the method. If you print the original in its respective order, it will print "John Smith". It does not matter whether the members of the class are fields, properties, other class instances, they will all behave exactly the same way. Once again, it does not break the C# language specification.

This can also cause problems with 'Deep Copy'. If you created a copy method that that copies all of an objects members and some of them are objects, then the original and the copy will share those objects members. So a Deep copy may end up actually being a shallow copy - oho! a trap! A good way to avoid that is to do a Deep Copy as a clone copy where there is a bit-by-bit copy of the entire object into a new object. This means new references and new locations. It isn't hard to do and you can achieve this in 10-13 lines of code regardless of how many members you have in the object being copied! I use bit-by-bit on any of my objects I want a deep copy on. In saying that, however, I  have rarely needed to use deep copy and good program design can reduce or eliminate its needs. If you find you need to make a Deep copy, maybe it is time to re-evaluate your design and refactor it.

So to summarise, primitives have their values passed, where as objects have their reference passed as value.  In order to assign a new reference to a closure variable (external variable passed to the method) you have to explicitly tell C# this is the intended behavior.


Sunday, 19 April 2015

Auckland Harbour Bridge, New Zealand, wired up as a VU Meter

Celebrating the City's 175th anniversary a mobile telecommunications company, 2-Degrees, wired up the bridge with more than a 1000 LED lights. People can request their songs on the 2-Degrees app and then watch it play out on the bridge. This will only stay on until 25th April and runs each week from Thursday to Saturday.

It was truly an amazing sight to behold from the Silo Park Waterfront in Auckland City when I saw this on Saturday the 18th of April. Being there is an amazing experience with the 2-Degrees speaker playing out the tune as it is being rendered in VU meter style on the bridge with cars passing on the bridge. It certainly draws a crowd making for a party like atmosphere which is absolutely spectacular.

Thanks to whoever at 2-Degrees thought this up! and the only thing I can say is, "please Mr Mayor, can we make this permanent?"

Enjoy the short video I took with the best camera in the world (The best camera in the world is the one you have with you; in this case my phone the Nokia Lumia 830).

Saturday, 4 April 2015

Blood moon, April 2015

Yay!!
That is how I will start this blog entry.
YAAAAAAA<Kermit>
Excited because I saw the blood moon Luna eclipse this time which I missed last time.

I don't have any special cameras but here are two shots from my Nokia Lumia 830 phone.

Can see the earths shadow covering half of the moons surface.

Can see the blood moon


Saturday, 21 March 2015

End of a fantastic Server 2000-2015

It is with great sadness that I have to take my Linux server offline. I monitored the server running 24/7 between the last power bill and the new power bill. The server is no longer sustainable. It will still function as normal for everything else except hosting online services like my web server, network segmentation box, MySQL Server(internal), SVN repository, ad blocker and firewall; which require a 24/7 uptime. In addition I will lose full time operation of MySQL database server, NAS, my own DNS server, and a 24/7 watchdog looking after and logging my connection from any attempts.

I wrote a pretty awesome IPTables firewall script which I constantly updated and I was quite proud of it. Attacking it using linux based nmap, and spoofing showed that it blocked them all. Although not 100% impenetrable(only a fool would think that), it did take care of a lot! It was a nice hobby to apply solid security such as one you might find on business premises in order to understand them better.

The hardware failed some  years ago (I think about 2004) due to bulging capacitors on the motherboard. The motherboard was also from a great company(then) called ABIT. I replaced 12 capacitors with low ESR's and breathed new life into this thing. The power supply also failed later on and I added in a new power supply. It has been going strong since without blinking an eye.

My internet connection will be less secure now by having a direct connection to the internet via my internet gateway. I.e no more secure than someone with a modem connected to the internet.

To compensate, I will begin looking for a router that uses a pinch of power and provides fully configurable proxy and firewall services. To resume hosting my web services and DNS server I will investigate Linode or Azure cloud services to see if that is sustainable.

However this architecture is not out for good. It will return someday! Until then, it will be on demand operation only. I.e I will turn it on when I need it to do something for me.

Until then, so long old friend.  2000-2015


Friday, 13 February 2015

BINARY SEARCH.


A wheem away a wheem away a wheem away a wheem away.....In the Searches, the Binary Searches, the Data lurks today.......

The mighty Binary Search, the final part of Part one "Sort and search algorithms using C#"

Lets put it in perspective what it means when operating a binary search on sorted data.
Assume that we have a data set one million long and reading them takes 1 milliseconds.
Lets say that the data we are looking for is roughly in the middle (but we don't know that yet).
That is approximately 500,000 milliseconds or 500 seconds, or 8 minutes 19 seconds and 80 milliseconds. We'll just say 8 minutes 20. This happens because you have to sequentially read the data either from the start or from the back. Each read and compare taking 1 milliseconds.

Can we drop that substantially to a few milliseconds? Yes we can! Enter, the 'Binary Search'.

How this works is rather simple actually. In the initial condition, you find the mid point, start and end indexers. We will call the start and end indexers the left and right index. The data we want to find we will call the "key". We now start our binary search and we do this by first checking the key with the value at mid point index. If key is smaller than the mid indexed value we move the right indexer to one less than the mid index and find the new mid index which sits between the left and the new right index. We then compare the new mid indexed value with the key. If the key is more than the mid indexed value we then make the left index one more than the mid index and find the new mid point index between the new left and the right index. Eventually we will reach a case where left= right and either we have found a match or we return that the record does not have the value searched. We are essentially halving the problem domain at each pass. So how does this look on 1 million data set? Like this:

initial = 1 million.
Problem domain size after 1st pass: 500, 000
Problem domain size after 2nd pass: 250,000
Problem domain size after 3rd pass: 125,000
Problem domain size after 4th pass: 62,500
Problem domain size after 5th pass: 31250
Problem domain size after 6th pass: 15625
Problem domain size after 7th pass: 7813 (we round up the decimal point)
Problem domain size after 8th pass: 3907
Problem domain size after 9th pass: 1954
Problem domain size after 10th pass: 977
Problem domain size after 11th pass: 489
Problem domain size after 12th pass: 245
Problem domain size after 13th pass: 123
Problem domain size after 14th pass: 62
Problem domain size after 15th pass: 31
Problem domain size after 16th pass: 16
Problem domain size after 17th pass: 8
Problem domain size after 18th pass: 4
Problem domain size after 19th pass: 2
Problem domain size after 20th pass: 1 and it is here where we either find the data or we say we cant find it.

If each of the comparisons took 1 milliseconds, then the jobs done in 20 milliseconds. Compare this with a linear search taking 8 minutes 20 seconds.
We also only had to perform 20 passes instead of 500,000 passes.

Now for some code!

 public int BinarySearch(ref int[] array, int key)
 {
    int m = array.Length / 2;
    int l = 0;
    int r = array.Length - 1;
   
    while (l < r)
    {
      if (key > array[m])
      {
        l = m +1;
        m = (l + r) / 2;
       }
       else
       {
         r = m;
         m = (l + r) / 2;
       }
      }
     if (array[m] == key) return m;
     else return -1;
   }

How the code works
At the initial stage m is set to half the array length. "l" and "r" is set to 0 and one less than array length respectively.
The code then enters the while loop which only runs if l is smaller than r. The key is compared with value indexed by m. If bigger, l is one more than the middle indexer other wise r is adjusted to be the index m is and a new m is calculated from the new problem domain range.
Notice that there is no case where key == array[m] in the while loop because for that to happen l == r must be true. If l==r is true the while loop will break before the "if" statement and instead execute the final test of array[m]==key. If this is true the index where the found key is gets returned otherwise return -1(to indicate no key was found).

The pros is that this search is very fast. The con is that the data has to be sorted first. This is the reason also why I put this at the end after talking about sorting algorithms first.

This was the last post for part one. Part two will focus on taking these same algorithms and making them accept all data types including abstract.

Thank you for reading and I hope things were clear.

Previous: Heap Sort part two.
Landing page: Part One.