Two Generals

It’s been a long while since I posted anything of interest other than fitness and running, so here’s something I found that isn’t fitness related.

Tom Scott (Twitter: @tomscott) posted this video about the Two Generals problem. Basically it centres around two generals and asks how they can synchronise communication to ensure they two something at the same time.

In multithreading computing, including back when I was learning Java, we had a synchronized keyword which ensure only one thread went through a block of text. But here, you want two threads to proceed at the same time. How do you ensure that? Turns out there’s no guaranteed way.

Even TCP requires a 3-way handshake aka the SYN, SYN-ACK, ACK (https://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml), but if any of those messages drops, the connection never establishes.

Of course you could just use the UDP method of “fire-and-forget” and say “Hey, General B, we’re going to attack at 8pm, catch up if you have to”, and have dropped packets (or casualties in the two generals problem)

This is an interesting thought experiment and one I hadn’t considered during my time learning parallel and threaded programming.

Oh well, you learn something new every day, I guess.

Google Music

A while ago, I posted of my frustration with Google Music when it refused to download my tracks. Well, I did some digging around and found that someone had written an API to expose the Google Music backend. The link is at

https://github.com/simon-weber/Unofficial-Google-Music-API

and has spawned several other tools including

https://github.com/thebigmunch/gmusicapi-scripts/

Which is a set of scripts designed to sync, upload and/or download from the Google Music collection.

I wrote my own Python script using the Gmusic API to bulk delete albums from my Gmusic account (it’s easy to bulk upload using Google’s MusicManager, but not to bulk delete), and the gmusicapi-scripts enables me to download most of my tracks.

Get Yesterday’s date in MS DOS Batch file

A useful snippet of DOS Batch Scripting to get yesterday’s date (or any other historical date for that matter). Very useful to then use with other DOS commands like xcopy or forfiles.

Linux though, it’s a hell of a lot user, just add options to the find command

@echo off
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1
if "%dayCnt%"=="" set dayCnt=1
REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100
:CHKDAY
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through
:SET31
set /A dd=31 + %dd%
goto CHKDAY
:SET30
set /A dd=30 + %dd%
goto CHKDAY
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto CHKDAY
:SET29
set /A dd=29 + %dd%
goto CHKDAY
:DONE
if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%
REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%-%mm%-%dd%

PowerCram: Get Yesterday’s date in MS DOS Batch file.

The Top Programming Languages, Ranked by Job Demand, Popularity, More

Apparently, the most sought after language is Java. Not that surprising considering it powers quite a few things, including Android and anything running Android (well, to a point, I guess since it doesn’t run Java in the same way that you would run the JVM on, say, your desktop.) Still, gives me a reason to brush up on it. And perhaps work towards a Java certification. I did have J2CP, but that was years ago, so I’m not sure if it is even valid any more.

Although it surprises me a little that Ruby and Perl aren’t on this list.

The Top Programming Languages, Ranked by Job Demand, Popularity, More.

Java

Duke, the Java Mascot, in the waving pose. Duk...

I really need to pick up and freshen up my Java knowledge. The last time I seriously programmed in it, we had no generic types, no lambda expressions, the Collections framework was not actively in use, and the dynamic object type of choice was Vector.

%d bloggers like this: