Friday, March 31, 2017

Android client connecting to java server example

As a case study on how to use a VPS, I bought a VPS from Amazon LightSail, wrote a server in Java and a client in Android:

VPS:
  1. I selected the preconfigured Ubuntu option. It has no GUI, you have to do everything on the command line.
  2. I created a static IP. 
  3. In order to access it freely (ping etc.), go to networking tab, under firewall add “All TCP+UDP”
  4. On Ubuntu, I installed OpenJDK8:
    1. sudo apt-get update
    2. sudo apt-get install openjdk-8*
  5. I installed Putty to use as an SSH terminal
  6. I installed Filezilla for file transfers.
Server software:
  1. Wrote server in Java on my laptop and transferred the code to VPS via FileZilla.
  2. I used gson for object-JSON conversions. The latest gson jar file can be downloaded from here.
  3. I wrote a java build script in Ubuntu using nano.
    1. Note that if you write the script in Windows and transfer it to Ubuntu, it might not work because line endings in Windows and Linux are different.
    2. To make the file executable: chmod +x filename
  4. I built and run the server. I prepared a shell script and ran it like so: ./script.sh
  5. To make sure that server keeps running when I disconnect putty, I used nohup and disown
  6. Update 15.10.2017: To keep nodejs server running do the following: 
    1. nodejs index.js> stdout.txt 2> stderr.txt &
    2. Do not close putty by clicking the top right "x", type exit.
  7. To kill a process
    1. Note PID by running ps -ef
    2. Kill process with sudo kill PID

Android client software:
  1. Wrote client in Android Studio.
  2. I used gson for object-JSON conversions.
  3. I had to use AsyncTask for connection, otherwise I get NetworkOnMainThreadException
The source code for java server and android client is on GitHub.

TODO:
  • Server: Handle multiple simultaneous connection requests

Saturday, March 18, 2017

Dealing with legacy Matlab code

A friend of mine has a project that works in Matlab Simulink 2007. He recently opened that simulink model in Matlab 2010 and could run it from Simulink without problems. But when he tried to transfer that model to xpc target for real time integration, the simulation hung and became unresponsive.

He and his team tried to find the problem by disabling Simulink blocks one at a time and in the end found the block that was causing problems. When they compared the 2007 version with the 2010 version, they could not find any differences which means that there is probably a backward compatibility issue that affects real time deployment. They had to manually recreate the whole model (more than hundred blocks) manually in Matlab 2010. Doing manual editing in code always carries the risk of breaking something. You might not know it unless you do tests with high coverage.

My first advice was to create a unit test framework so that you are sure that the new version yields the same outputs for the same inputs. A good strategy is to write scripts that vary inputs using unform distribution, run simulation on xpc target for every input, record outputs (or timeout when simulation hangs) and then compare outputs with previous results and create a report that highlights differences.

My second advice was to automate the update-to-newer-Matlab process by writing a program that automatically creates a new Simulink mdl file from the old one, beause it is easy to parse the mdl file which has an xml-look. Steps:
  1. Takes as input the depth of parsing. 1 = Copy the whole model. 2=Go one step deep and create separate mdl files for blocks in that depth 3=Go 2 steps deep etc.
  2. Parses the old Simulink mdl file and extracts the blocks
  3. Creates an mdl file in the new version format.
  4. Inserts the blocks into new mdl file(s)
  5. Runs unit tests on xpc target to see if the two versions have the same outputs (i.e. they are within 1e-15) neighborhood.
With this program, my friend can first check if for input depth =1, the model passes tests. If not, he can increase the depth until he can isolate the problem. Increasing depth on error could be automated too.

Saturday, March 11, 2017

Intelligence tests

I don't have much faith in intelligence tests. My definition of intelligence is successful adaption to new circumstances. Adaption and persistence determine your material success. How can you hope to test adaption, especially the social component in an hour long test?

Recently a friend of mine told me that his child's test score indicated his child being super intelligent but my friend didn't value the result much, although it was better than having an imbecile score. Our conclusion is that such tests mainly measure how much care the child was given by the parents. If parents have done a good job of parenting, I suspect any kid who does not have a mental condition will score highly in those tests.

Unrealistic proposal requests

Besides software development, my responsibilities also include assessing and answering simulation related proposal requests from other firms. Since simulation is usually poorly understood, the requester puts in sentences like "the simulation shall be realistic" without knowing how vague and overly ambitious that is.

Without going into philosphical details about what is "realistic", I can say that it involves a lot of real world testing and modeling. The budget that is available is usually less than 1% needed to fulfill realism. In that case we have two choices:
  1. Prepare a proposal that takes every possible risk into account and demand a billion dollars (same as saying NO).
  2. Think about what can be done with the given budget, write down your assumptions and limitations and present that as a proposal. This is the way to go if you want to avoid suffering.
The major cost factor in a simulation project is integration and testing. Your first question to any proposal request should be "how will you know that our simulation works as you expect". The answer will determine not only how much testing is needed but also how detailed your models should be. It is like unit tests driving design. If you don't get a satisfactory answer, write down in your proposal how and where you will test it.

Sunday, March 05, 2017

Software manager reading list

A few days ago, a manager friend of mine asked me for advice about what to do in a project that is near crisis, i.e. it has missed a deadline and looks like will be over budget.

Crises are common in software projects because their complexity usually is beyond our time and resource prediction capabilities. I offered my friend the following books and recommended reading them first before demanding that his team starts working overtime:
These books will ground him in reality and make him realize what kind of a mess he is in. I also told him that he should only tell his superiors the reality if he is certain that they are interested in the truth. A lot of times people want to hear only good news and are content with ignorance until it hits.

If you sense that your superiors want to live in fantasy land, never tell them the bad news because you will be labeled as a pessimist. Even after your predictions prove to be true in the end, you being labeled as a nuisance won't be washed away.

These books will help you understand what is going to happen and why. You might not be able to change the outside world, but you will have less stress inside, not feeling any guilt, knowing that you did the best you could.