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.