Java Collection Cheat Sheet

Posted in Works | Tagged , , | Leave a comment

Some Sql Notes

In these quarentine days I’am getting some progress in sql. I nearly completed the database design, but still there is some standardization issues that I’am working on it. It be use by any small scale technology companies for storing and managing technical sales, stock tracking, client info and technical service records. Mysql forces me a lot because of its relational structure but it is fine, I’am learning and practicing lots of things, such as constraints, views, procedures, triggers and functions. The finishing can take one day more, I attached current enhanced entity-relationship diagram of it.

EER Diagram of The Database
Posted in Projects, TechLog | Tagged , | Leave a comment

OpenVPN Community Edition Server Setup Notes

Recently I setup an OpenVPN server on my remote Ubuntu Server (18.04) for all my internet traffic. After completing all the steps, I have done some extras for both of the client and server side machines. Also, my wifi access point-router needs an adjustment for handling TLS handshaking process.

First things first I have enabled and assigned the floating-ip to the droplet and setup the server like this. Then, I have found my anchor-ip with

curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/anchor_ipv4/address

I have added local anchor-ip to server.conf

sudo nano /etc/openvpn/server.conf

I added following rules for Ubuntu’s firewall and postrouting:

sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source anchor-ip
sudo iptables -A INPUT -i eth0 -p udp -m state --state NEW -m udp --dport 1194 -j ACCEPT
sudo iptables -A INPUT -i tun+ -j ACCEPT
sudo iptables -A OUTPUT -o tun+ -j ACCEPT

Then I need to save the rules, because they were not persistent and will remove after reboot. Iptables-persistent is nice tool for this.

sudo apt install iptables-persistent netfilter-persistent

The two packages are similar, but provide slightly different functionality. If you only install iptables-persistent, you won’t get the service definition file for correct handling in systemd, eg /lib/systemd/system/netfilter-persistent.service. If you only install netfilter-persistent, you will find that rules are not correctly applied at boot.

Rules can be saved in a file with the (root) command iptables-save for IPv4:

iptables-save > /etc/iptables/rules.v4

These files can be loaded again with the command iptables-restore for IPv4.

iptables-restore < /etc/iptables/rules.v4

Recent versions of iptables-persistent have two configuration files: /etc/iptables/rules.v4 for the IPv4 ruleset, and /etc/iptables/rules.v6 for the IPv6 ruleset.

Here is my IPv4 file:

To save all filter rules:

netfilter-persistent save
Continue reading
Posted in Days | Tagged , , , | Leave a comment

Tab Overload #3

Posted in Links | Tagged , , , , , , , , , | Leave a comment

Checking Some Opcodes in Assembly Language

A program written in an assembly language that implements the following Java code look like the one:

int thomas = 3, sylvester = 7, cats;
cats = thomas + sylvester;

Symbol     Opcode    Operand
------------------------------
SAMPLE     START     FIRST     Line 1
THOMAS     WORD      3         Line 2
SYLVESTER  WORD      7         Line 3
CATS       WORD      ?         Line 4
FIRST      LOAD      THOMAS    Line 5
ADD        SYLVESTER           Line 6
STORE      CATS                Line 7
STOP                           Line 8
END                            Line 9

 

The program adds two numbers 3 and 7 that are in the variables THOMAS and SYLVESTER respectively, and stores the result 10 in the variable CATS. The column labels, the line below the column labels, and line numbers are not part of the program but included to show how the words line up. In order from left to right, the three fields of an instruction are the symbol, the operation code (opcode), and the operand. The first field is typically reserved for the names of variables or labels for instructions. The second field is typically used for opcodes that represent executable instructions and also assembler directives which tell the assembler what to do. The third field is used for operands.

The first line indicates the start of the program. The symbol SAMPLE is the name of the program, the opcode START tells the computer when the program starts, and operand FIRST indicates the location of the first instruction. The second through fourth lines are directives that tell the assembler to reserve the memory locations. Line 2 reserves a space for the symbol THOMAS which is initialized to 3. Similar to the second line, the third line initializes the variable SYLVESTER to 7. The fourth line reserves an uninitialized memory location called CATS. Line 5 fetches the contents from the memory location THOMAS and stores it into a register. A register is a memory location in the CPU where among other things arithmetic can be performed. Then the ADD operation in Line 6 gets the value from the variable SYLVESTER and adds it to the contents of the register. The next line stores the value in the register into the memory location CATS. The STOP instruction halts the
execution of the program. In the last line, the END directive indicates to the assembler the end of the program.

 

Posted in General Concepts | Tagged , , | Leave a comment

Pipe-and-Filter Architecture and An Elegant Algorithm

The Problem: Given a dictionary of words in English, find all the anagrams in the dictionary. That is, find all the words that are permutations of each other. For example, pots, stop, and spot are anagrams of each other.

Bentley, J. Programming Pearls, Second Edition. (Boston, MA: Addison-Wesley, 2000.)

First of all, all the anagrams have the same letters and the same number of letters in each word. That gives us the clue to the method you’ll use to find the anagrams. If you sort each word by individual letters, you’ll end up with a string of characters that has all the letters in the word in alphabetical order. We call this creating a sign for the word. If you then sort the resulting list, all the anagrams will end up together in the sorted list because their sorted letters will be identical. If you then keep track of which words you sorted, you can then simplify the list and create a new list with, say, each set of anagrams on the same line of the output file. This is exactly how Bentley does it. But how does this relate to a pipe-and-filter architecture, you ask? Good question. Let’s break down the solution again.

  1. Create a sign for each word in the list by sorting the letters in each word; keep the
    sign and the word together.
  2. Sort the resulting list by the signs; all the anagrams should now be together.
  3. Squash the list by putting each set of anagrams on the same line, removing the
    signs as you do.

sign <dictionary.txt| sort | squash> anagrams.txt

sign is the filter we use to do step 1, with input file dictionary.txt. sign outputs a list of signs and their associated words, which is piped to the sort utility. Sort then sorts the list by the first field on each line, which happens to be the sign of each word. It then outputs the sorted list to the next pipe. Squash takes the sorted list from the incoming pipe and compresses it by putting all the words with the same sign on the same line, eliminating the signs as it does so. This final list is sent via one last pipe to the output file anagrams.txt.

Posted in Algorithm Analysis, General Concepts, Links, Software Architecture | Tagged , , , | Leave a comment