## Thursday, May 03, 2018

### Numerical Integration Basics

Consider the equation $$y = 4$$. If we plot this on two axes, x and y, it has no slope; it's just a horizontal line. We could also write the equation as $$y = 0x + 4$$ because the value of y is unaffected by the value of x. We could also write it as $$y = f(x)$$.

The integral of a function can be used to find the area between the function and the x-axis and is written $$I = \int_a^b f(x) dx$$. It's essentially the sum of tiny changes in x ($$dx$$) multiplied by their corresponding y values ($$f(x)$$). The equation reminds us that we need to restrict ourselves to a lower and an upper value of x (otherwise the area would be infinite).

Let's choose a = x = 0 (the y-axis) and b = x = 3 as those two bounds.

$$I = \int_0^3 4 dx$$
$$I = \left[4 x\right]_0^3$$
$$I = \left(4\times3\right) - \left(4\times0\right)$$
$$I = 12$$

We're not restricted to 0 as the lower bound, but it exposes an interesting property: the area under a-to-b is equal to the area under 0-to-b less the area under 0-to-a. Let's try the same integral as above, but from 0-to-1.

$$I = \int_0^1 4 dx$$
$$I = \left[4 x\right]_0^1$$
$$I = \left(4\times1\right) - \left(4\times0\right)$$
$$I = 4$$

We might then subtract 4 from 12 to arrive at 8, but why not do this in one go?

$$I = \int_1^3 4 dx$$
$$I = \left[4 x\right]_1^3$$
$$I = \left(4\times3\right) - \left(4\times1\right)$$
$$I = 8$$

## Thursday, March 22, 2018

### Singleton Pattern vs. C# Static Class

I ran into an old foe the other day when we were discussing interview questions; which to choose: standard singleton pattern or static class? At the most basic level, both constructs are useful in limiting the number of instances of an object that can be created. There are numerous subtle differences at the implementation level, but the single biggest difference is at an object-oriented level: static classes do not support polymorphism. This stems from a combination of factors at the language level; in short, a static class cannot inherit from a base class (abstract or concrete) nor can it implement any interface methods. Conversely, you can neither define static virtual methods on a class, nor can you define them on an interface. So, if you're ever using dependency injection - a technique that relies heavily on polymorphism - you will likely find that static classes will be inadequate. There are a number of other runtime level differences, but these are all secondary to the polymorphism issue. The simplest is that a static class can never be instantiated, and all methods are executed against the type instance. Contrast this with the singleton, of which exactly one instance is created, and where methods are executed against that instance. Next time I choose, I'm going anti-static for all but the simplest "utility" classes.

## Monday, January 22, 2018

### Cisco 7960 SIP Phone

I bought a Cisco 7960 IP phone (preloaded with SIP firmware) to connect with my experimental asterisk server running on a Raspberry Pi. Setting it up wasn't that hard, once I stopped following the bad advice that abounded on Google search results. I'm partially documenting my own experience here - not to help, but to hinder. You don't need a TFTP server (and therefore you don't need to set any hard-to-reach DHCP options) to distribute configuration files, you can do everything you need through the phone's keypad. If you're going to make changes, you need to perform the "Unlock Configuration" steps each time.

#### Unlock the Configuration

1. Press Settings
2. Scroll down to 9: Unlock

#### Set the TFTP server IP address manually

1. Press Settings
2. Scroll down to 3: Network Configuration
3. Scroll down to TFTP Server
4. Press Edit
5. Enter the IP address of the TFTP server

#### Configure SIP on the Phone

1. Press Settings
2. Scroll down to SIP Configuration
3. Select the line you want to edit
4. Confirm the following entries exist (or add them) [They should match what you've got setup in sip.config on the Asterisk server).

#### Setup SIP on the Asterisk Server

1. Add a new block to sip.conf. Make sure the extension and secret match the values you provided when configuring SIP on the phone. [1234] context=home secret=secret etc.
2. Add a dialplan rule or two to extensions.conf. Make sure the context in sip.conf matches the section name in extensions.conf. exten => 1234,1,Dial(SIP/1234)
3. asterisk -r then sip reload and dialplan reload

## Friday, October 20, 2017

If you have ever called up your ISP support team to complain, you may have heard them quote a number called SNR to describe the quality of your connection.

It's not quite as simple as a single number, though.

ADSL2+ uses multiple "channels" at once to transfer data between the ISP and your home modem. In the case of my ASUS modem, there are 512 channels: there is 2.208 MHz of available spectrum on the copper wire; these are split into equally spaced 4.3125 kHz wide channels.

However, not every channel (or bin) is created equally: there is almost certainly some electrical "noise" or congestion on the cables. Some bins are reserved for normal telephone connections, others for upstream and downstream communication with the server, with a few reserved as guards to keep sections separated.

I pay for a 4 Mb/s connection, so let's assume the ISP allows me 1672 bits' worth of downstream sync rate*. It is up to the modem and ISP to negotiate how to allocate the sync rate across the available bins. My modem surveys the ratio of signal to noise (SNR) across each bin in the downstream set of channels. A rule of thumb is that each bit requires about 3dB of SNR to reliably decode the signal without errors. Bins with higher SNR will therefore have higher bit capacity for bits.

My modem allows me to connect with SSH and look at three files, which are ordinarily used by the administrative web server interface to present me a way to easily change modem settings and view diagnostics:
• /var/tmp/spectrum-bpc-ds
• /var/tmp/spectrum-bpc-us
• /var/tmp/spectrum-snr
Let's do some very simple mathematics with the data in these files that won't be too difficult to follow:

Data taken from the SNR file:
1672 bits to allocate
268 channels into which they must be allocated
10,523.27 dB (the sum of SNR across the available reception channels)

Assumptions:
3 dB/bit required to convert received signal without errors

1) 1672 bits * 3 dB/bit = 5016 dB SNR required to decode the signal

2) 10,523.27 dB - 5016 dB = 5507.27 dB unused SNR

3) 5507.27 dB / 268 channels = 20.54951 dB unused SNR per channel

4) Now, we iterate each of the 268 channels in turn. I'll demonstrate with the first channel, where the SNR was 24.21 dB.

a) 24.21 dB - 20.54951 dB = ~3.66 dB to be used for data reception

b) 3.66 dB / 3 dB/bit = ~1.22 bits

c) round 1.22 bits to a whole number: 1

It turns out that this simple algorithm gets us remarkably close to the numbers of bits per channel (BPC) in the other files, which are arrived at by the modem's own algorithms. The difference could even be accounted for by (lack of, or outdated) "bitswap", where bins are periodically re-checked for SNR and reallocated to make the best use of the spectrum as it evolves over time.

* Ignore - for now - that those numbers are not equivalent, they are effectively the same thing quoted for different units of time

## Wednesday, August 23, 2017

### OpenVPN

This article on Ars Technica inspired me to try and setup a VPN, but it seemed to lack a couple of extra steps that were required to run the server on a Raspberry Pi behind a router. The official OpenVPN documentation was helpful but long-winded, and I resorted to a few askubuntu answers in the end.
1. #### Certificate names

client-no-pass doesn't have to be replaced with the client host name, but can be (pretty much) any identifier for the client. The important thing is to sign all certificates with the same CA key.
2. #### VPN topology

The default topology of net30 is perhaps not as easy to comprehend as subnet, and subnet works with Ubuntu, Windows and iPhone clients.
3. #### Default gateway

On the server, I needed to push redirect-gateway, push "route xxx.xxx.xxx.0 255.255.255.0" and consequently push "dhcp-option DNS xxx.xxx.xxx.1" so that the client would be able to route to the server's local network, and the tun0 device would be able to do DNS lookups.
4. #### Firewall rules

Besides enabling the forwarding of IPv4 so that the Raspberry Pi acted as a router, I need to add three firewall rules in iptables: one for NAT masquerading, and two for accepting new and related established connections.
5. #### DNS setup for clients

The dhcp-options that were pushed to the client were ignored, which resulted in the client being unable to resolve any DNS names to IP addresses. This was only a problem on Ubuntu clients, but the script /etc/openvpn/update-resolv-conf was provided when I installed openvpn; all I had to do was reference it from client-name.config as a pair of lines:
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf


## Sunday, August 13, 2017

### Grub

It's nice when the Grub bootloader remembers your last choice and reuses it as the default value next time your machine boots. Using Linux, put the following in /etc/default/grub:
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

Then run:
sudo update-grub

## Wednesday, March 29, 2017

### SubscribeOn / ObserveOn

Once you have an instance of an IObservable<T> object, Reactive Extensions (Rx) provides at least two points where you can choose an IScheduler which will determine the threading context under which the later operations will run. Those operations can be broken down into two phases: subscription and observation.

Subscription refers to the operations that modify the behaviours of an IObservable<T> (e.g. Throttle, Merge, Concat) The IScheduler for the subscription phase is assigned by the SubscribeOn() method.

Observation refers to the callback operations that are invoked as the result of observing an IObservable<T> (e.g. OnNext, OnCompleted, OnError) The IScheduler for the observation phase is assigned by the ObserveOn() method.

While not the subject of this post, you can also choose an IScheduler implementation to convert any IEnumerable<T> to an IObservable<T> with the ToObservable method. This is relevant when the IEnumerable<T> has the potential to take a long time to be enumerated.