Thursday, 1 February 2018

QR Code Scanner


Image of qr code scanner by Achilles Moraites

QR Code Scanner Download


QR codes are everywhere : cafes, airports , public spaces , WiFi hot spots , in products , stores...

But what are they ?

QR Codes are like that


They can contain information about WiFi connection details, contact information , websites , maps , text, sms , e-mails , phone numbers, even events .
You can quickly access that information to get information from promotions and special offers to contact details with just a scan!

 QR Code Scanner


With QR Code scanner or your favorite QR Scanner (hoping to make mine your new favorite) you can have access to that valuable information!


 If the scanner finds WiFi information it will prompt you to connect to that network , with one click will automatically enter the required details to join the WiFi !



Hassles like copy paste and manually entering WiFi details are now in the past.

Other Features :


  •  Scan and connect to WiFi networks that provide qr code within the app!  no more typing and time wasting!
  • QR codes containing websites are automatically opened in your preferred browser!
  • Supports : email , sms , maps , phone numbers, websites and WiFi !

Friday, 1 December 2017

Design Patterns - Adapter

The adapter design pattern is about creating a layer where  two different components can work together, just like a real life adapter you use it to connect things that couldn't work together otherwise, following is a story to help you understand what is this about. 

You are a software engineer in SoftCorp , after several months of trials and other challenges you and your team are almost done working on an innovative GPS software for self driving cars, the release date is close and all are happy...

 The problem :

Suddenly your manager is calling you at her office and wants to change the weather api!

The software is using the WeatherPoint api to get weather updates so your software can make calculations for long distance trips, your manager wants to change the api to a different vendor : WeatherOracle.


The Solution:


Of course one option is to completely refactor your code base of thousands lines of code to use the new api , or you can use the easy way of the adapter pattern and be the hero of the day!


Project : SelfDrivingCarGPS

get the code

Before we dive in to the specifics of the pattern lets take a moment to explain in a high level what we are going to do:
Our CarGPS currently is expecting a WeatherPoint class but we need to use a WeatherOracle instead.

The adapter solution is to create a class WeatherPointToOracleAdapter where we will wrap a WeatherOracle obj adapting it to a WeatherPoint so the rest of your software does not need to know about the change!

here there is a simplified UML about the adapter pattern:



The first step is to create our adapter, the WeatherPointToOracleAdapter class :
 we will  place it in the adapter package.

public class WeatherPointToOracleAdapter implements WeatherPoint {

    private WeatherOracle weatherOracle;

    public WeatherPointToOracleAdapter(WeatherOracle weatherOracle){
        this.weatherOracle = weatherOracle;
    }

     
    @Override 
    public String getWeather(String location) {
        // we use the weather oracle method to get weather 
       return weatherOracle.getWeatherData(location);
    }

 
    @Override     
   public String getLocation() {
        // we use the weather oracle method to get location 
       return weatherOracle.getTheLocation();
    }
}

Take a look in our adapter:
  • Implements the WeatherPoint interface because we will adapt the given object to a WeatherPoint
  • has a field WeatherOracle 
  • takes a WeatherOracle obj in the constructor , this object will be adapted !

we can use it like this:

public class Demo {
    public static void main(String[] args) {
        // Our new api
     WeatherOracleApi weatherOracle = new WeatherOracleApi("London");
        // adapting the api from WeatherOracle to WeatherPoint 
     WeatherPoint adaptedApi = new WeatherPointToOracleAdapter(weatherOracle);
        // using our class with no changes! 
     CarGPS carGPS = new CarGPS(adaptedApi,adaptedApi.getLocation());

 
     carGPS.getWeatherData();
 
     carGPS.planTrip();
    }
}

Our CarGPS has no knowledge that we have placed an adapter or that the api is changed and just works like before using our new api!

Now our implementation looks like :


Well done , with your new skills you have saved your colleagues many hours of trivial work!
Explore the code and try to build your own adapters!

Happy coding!



Wednesday, 29 November 2017

Design Patterns - Composite

The composite design pattern is about a hierarchy where nodes with children have different behavior than childless nodes.

The pattern consists of three elements:
  • Component : a super-class typically an interface 
  • Composite : a node with children implements Component
  • Leaf : a childless node  implements Component


A typical example is a file system where there are folders and files:
Folders can contain files , other folders  or just being empty. On the other hand files don't contain folders or other files!

Project : FileSystem

 get the code here.

Here you will explore a simple file system , where you can see a simple implementation of the pattern.

Here you can see a UML of our File System  :

take a moment to explore the code and:
  •  note what component of the pattern each class represents 
  •  observe the output of the code and see the difference between a file and a folder 
  •  in what scenarios do you think this pattern would be useful?

Project : Extending our FileSystem

 

 Time to get your hands dirty!
 Extend our FileSystem by adding new file types!


Take the code and implement a new File type: Img

General Guidelines:
  • The Img should implement FileComponent
  • Img must have a data field, you can use anything you like
  • You can use the display method of the file to customize it for the new file
  • Create an Img object and add it to a folder!


Happy coding !!!



Sunday, 26 November 2017

Design Patterns - Advanced Iterator

In the Basic Iterator article you have used an existing iterator to simplify our Car Parts Store , having learned about iterators you might have wondered if you can create your own iterator...


Project : CarPartsStore - Advanced


At some point our Car Parts Store has become more sophisticated , at the beginning we had only one List<CarPart> carPartList with all the available parts, but the Warehouse manager also wants to have a log where he/she can keep track of what parts have been sold.

The soldLog is also a List<CarPart> where we have all the parts that we have sold to our customers, things are getting more interesting for our Warehouse class and we need to use the advanced iterator pattern to accommodate our new needs.

As we have done in the basic iterator pattern we will start by making our Warehouse class to implement Iterable :

public class Warehouse implements Iterable<CarPart> 



 and implement :

@Override 
public Iterator<CarPart> iterator() {
     return new WarehouseIterator();
}

WarehouseIterator is not existing right now, this will be our custom iterator !
We will make it a private inner class as it needs access to our Warehouse inner workings, we can name it anything the important things are:
  • it is an inner class
  • implements Iterator<CarPart>
Our inner class will look like:

private class WarehouseIterator implements Iterator<CarPart> {
 
 @Override 
 public boolean hasNext() {
        return false;
    } 
 
 @Override 
 public CarPart next() {
        return null;
    }
}

now it's time to get it done:
 we add the following cursors in our WarehouseIterator class:

 // Here we put our cursors: 
Iterator<CarPart> partsInStockIterator = carPartList.iterator();
Iterator<CarPart> soldPartsIterator = soldLog.iterator();

now we can keep tracking both Lists using their iterators

 
@Override 
 public boolean hasNext() {
    // if any of the iterators have next return it till there is no next 
 return partsInStockIterator.hasNext()|| soldPartsIterator.hasNext();
}

as long either iterator has a next we return it till there are no more.

 
@Override
 public CarPart next() {
    CarPart carPart;
    if(partsInStockIterator.hasNext()){
        System.out.print("Stocked : ");
        carPart = partsInStockIterator.next();
    }else {
        System.out.print("Sold : ");
        carPart =  soldPartsIterator.next();
    }
    return carPart;
}

The print statements are added so you can see what is going on when the code is running.
You should remove or comment them out when you verify the code works as expected!

Now our code has the ability to iterate through both lists in our Warehouse class!

Happy coding!




Friday, 24 November 2017

Design Patterns - Basic Iterator

Among the most common things in a collection of items is the ability to iterate through the collection , in Java for example there are data structures that allowing us to create collections for primitive data or our custom classes.
Here some : ArrayList<T>, LinkedList<T> , HashSet<T> and others...

The above have something in common , all are implementing  the Interfaces : Collection and Iterable.

Below there is a simplified UML diagram  illustrating the ArrayList <T>:


The above relationships are allowing us to use the magic:

for(String s: stringList){
     // do something with s
      System.out.print(s);
}

The above is a convenient way to write a foreach  loop in Java, but the language it self translates it to:

Iterator<String> si = stringList.iterator();

while(si.hasNext()){
   String s = si.getNext();
   // do something with s
    System.out.print(s);
}

Now armed with this knowledge we can go beyond the default behaviors of the build in collections and can customize it for our own needs!

Basic Iterator Pattern:



Project : CarPartsStore


Here we will illustrate the customized behavior by exploring a Store that sells car parts, in this code-base we have the Warehouse class that contains a List of CarPart .

What we do here: We will use the basic Iterator Pattern to iterate through our Warehouse class for CarPart .

We will make something like the following to work:

for(CarPart part: Warehouse){
  // do interesting stuff ...
}

by default the above is not working as our warehouse has a field of ArrayList<CarPart> ,  but having the new awesome Iterator powers we can can do it !

 In our Warehouse class we added the implements Iterable :


public class Warehouse implements Iterable<CarPart> 
  

notice we used the CarPart type , as we want to iterate through a collection of CarPart.

also we have implemented the following :

@Override 
public Iterator<CarPart> iterator() {
    return carPartList.iterator();
}
  

Now when we try to iterate through our warehouse we iterate through the carPartList !
Congratulations ! now you have learned how to take iteration to your hands!

Feel free to experiment with the code!!! 



 





Tuesday, 21 November 2017

Design Patterns - Observer

The observer design pattern is useful in cases we want a change in an object (subject) to be known to one or more other objects (observers).
This design pattern is allowing us to avoid coupling and it's pitfalls in a clean , easy to scale way.

The observer pattern is comprised of two elements:
  1. Registration: Here the subject is adding the observer/list of observers (method is provided by the subject class) the method typically is called register , addObserver etc...
  2. Notification: After the desirable event happened the subject is updating the observers that have been registered by calling an update method (provided by the observer class) to perform the desirable action.

To implement the pattern two classes are needed: the subject and the observer , below you can see the relationship in a UML diagram :

A subject class has a list of observers , by calling the addObservers method we add more observers
and when something interesting happens in the subject we call the notifyObservers which loops through our observer list calling each observers update method.

We can illustrate this behavior by a simple example :
    We have a Blog and Subscribers , when the Blog publishes a new Article the Subscribers want to be updated about the article. In this example the Blog is the subject and Subscribers are the observers.
    Take a look at the following diagram illustrating how we could make it happen:



The Subject is a regular class and the Observer is an interface : the addObserver it will be called when the Blog is calling it's subscribe method , the notifyObserver would be called when publishArticle is called because that's the method that does something our observers are interested about.

The point is to have an action in the subject to update our observers.
You need to know :
  • When to add observers in the subject list
  • What is the action in the subject that interests your observers?
  •  What the observer is supposed to do when it's update method is called ?
Plan carefully what you will do , before you do it.

Get the example code on github

Happy coding !!!

Tuesday, 24 October 2017

Configuring Port Security

Cisco switches offer a tool called port security using it we can :
  • Limit who connects 
  • Control how many can connect to a port
  • Set an action when a violation occurs

Port security is used to mitigate MAC Flooding Attacks and can prevent rogue devices from connecting to your Network.

Before we can configure it we should take a look on the modes available , and note before the actual configuration what we want to achieve :
  1. Note the ports that will get configured , usually we use port security to the ports that connect end devices .
  2. What violation mode is appropriate for your network policy ? 
  3. How many devices are allowed on a given port ? Be EXTRA CAREFUL when you are on this step , you could deny access to a legitimate device if you allow fewer but you can have a security hole if they are more. 

Port security has 3 violation modes
  • Shutdown (default) : when used the port shuts down, it can sent an  SNMP trap , creates a syslog message and increments the violation counter.
  • Restrict : The port ignores any packets from the rogue device , stays up  , creates a syslog message and increments the violation counter.
  • Protect : This mode is similar with Restrict but it just wont do anything to let you know if there is a violation , the port stays up and ignores the offending device packets.


Configuring Port security

First you have to enable port security on the ports you want to protect :
---------------------------------------------------------------
switch# configure terminal
switch(config)# interface fa0/1
switch(config-if)# switchport mode access \\ port security needs the port to be in access mode to function
switch(config-if)# switchport port-security    \\ this enables the feature
switch(config-if)#switchport port-security maximum 1  \\ it configures the port to allow for 1 device.
 switch(config-if)#switchport port-security violation shutdown \\ it configures the violation mode to shutdown
------------------------------------------------------------------

What happened in the previous commands :  

  • We go to interface configuration
  • Enable access mode as it is required for port security to function
  • Enable the port security feature
  • Set the number of devices that can connect
  • Configure the access mode
 Note that you can hard code the MAC Address of the devices you want to allow with the following command:
  switch(config-if)#switchport port-security mac-address 1a2a.1ba1.a111
// the 1a2a.1ba1.a111 is an example how to correctly write the mac address .

Personally i believe it is better to hard code the devices if they are not changing places on your network .

Always be careful with this feature ! You can cause a Denial Of Service if configured poorly!