Web services and Transport layer protocols


















Image obtained from : http://www.highteck.net/images/45-Transport-layer.jpg

Because web services operate over internet the role of TCP/IP stack is aslo essential. In TCP/IP layers Transport layer protocols can play a huge role because it helps to move data between machines over internet.   

WS over Non-HTTP Protocols 

These include protocols like FTP, SMTP, POP like protocols.
WS can operate over these as well.
These are asynchronous (meaning request response doesn't happen immediately ) 

WS over HTTP 

This is the most WS protocol.
HTTP is text based.
HTTP is synchronous (meaning request reply can happen immediately)

HTTP request happens via verbs (GET, POST, PUT, DELETE, HEAD .........)

But not all the web services respond to HTTP verbs.

HTTP syntax
protocol :// [HOST] / [Resource root] / [Parameters - optional]
https://www.youtube.com/results?search_query=gangamstyle 

Response data over HTTP can be any format we request.

Ex if we request to send repnose data in XML data format, we can add request header 
ACCEPT : application/xml

Share:

Brief understanding of web services

Normally when you want to communicate with another computer from our computer we can use a computer network.

Web services are just like that.
We have a client who request a service x.
A server who has the service x implemented in its server.
We can request for the service x over internet.
Image obtained from : http://pdn.pelco.com/sites/default/files/original/4274web_service_diag.jpg














According to Web Service Architecture Working Group (WSAWG) web service is defined technically as follows.

Web service has an interface described in a machine-processable format (specifically WSDL). Other systems interact with the Web service in a manner prescribed by its description using SOAP (Simple Object Access Protocol) messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web-related standards.
http://dev.w3.org/2002/ws/arch/wsa/wd-wsa-arch-review2.html#whatis


Typically we can just call a remote web service. We have to go through its API(Application Programming Interface) to access it.
(ex: If we build a mobile device and we want to get some data to our app from Facebook server we have to go through Facebook API first)


API components

  • Request message format (can be SOAP, XML or JSON or any other format)
  • Response message format (can be SOAP, XML or JSON or any other format)
  • Service request syntax (call as a named method / Call as URIs and Parameters)
  • Requested action (What is being requested from the server)
  • Authentication ( Authenticity of the connection between client and server, login exchange or token exchange)

Machine- Machine communication mechanism (from history to now)

  • Electroic Data Exchange (EDI , machine-machine communication method)
  • Remote Procedure Calls (RPC)
  • Microsodt RPC (based on RPC and COM - Common Object Model)
  • Common Object Request Broker Architecture (CORBA)
  • Java Remote Method Invocation (Java RMI)
  • XML-RPC (XML based RPC)
  • ATOM based WS (Atom is based on XML)
  • RSS based WS (RSS is also based XML)
  • Simple Object Access Protocol (SOAP. Also known as WS*-Web services everything. One of the popular one)
  • JSON based WS




Share:

Binary tree - Data structure


What differ a binary tree from a standard tree data structure is binary tree parent can only hold 2 children (from left side and right side).

Advantages of using a binary tree is it combines both good qualities of ordered array and linked lists.

  • Fast search (Like using ordered array and binary search)
  • Fast insertion/deletion (Like in a linked list)

Image obtained from : https://upload.wikimedia.org/wikibooks/en/4/41/HSE_ch5_binary_tree.png

















Following figure illustrates how search/add items work on a BST.



Tree Traversal 

Since tree is not a linear data structure like array or lists or queues, there are more than one way of traversing through a tree.Tree traversal simply means that you start at a node and go through each node only once.

We have 2 kinds of traversal.(This is also valid for graphs also. In fact these traversals came from graphs because tree is a special kind of a graph. )

  • Depth first traversal (also called as level order trversal)
  • Breadth first travesal
    • Pre order (visiting order root -> left sub tree -> right sub tree)
    • In order (visiting order left sub tree -> root ->  right sub tree)
    • Post order (visiting order left sub tree ->  right sub tree -> root )
Image

Breadth first(level order): A,B,C,D,E,F,G,H,I (finish each level then go to next level)
Pre order: A,B,D,E,H,I,C,F,G (Only after finishing nodes left sub tree, move to right sub tree)
In order: D,B,H,E,I,A,F,C,G (First finish nodes left and node and nodes right)
Post order: D,H,I,E,B,F,G,C,A (Unless left and right sub trees are done, dont visit middle node)


Level order traversal (Depth first) process

  • Hopefully we have a root node. Otherwise how can we traverse :p 
  • We can use a queue for this type of traversal.
  • En-queue a node.
  • Print that node or do anything with that node so it is visited.
  • De-queue that node. But before de-queuing, enqueue it's child nodes to the queue.
  • Do this until all the nodes are over in the BST. 

Pre order/in order/post order traversal process

  •  Select a node
  • Visit that node
  • Then visit its left node. (If that node also has a left, process is similar)
  • after visiting left node,visit its right node ( (If that node also has a left, process is similar))
In in order and post order traversals the process is similar. Only the node,left,right sequences gets changed.


Binary search tree - node - Java code (For illustration)


package BinaryTreePackage;

import java.util.LinkedList;
import java.util.Queue;

public class Node {

 int data;
 Node left;
 Node right;
 
 public Node(int data) {

  this.data = data;
 }
 
 //-----------------------------------------
 // Add nodes
 // ----------------------------------------
 public void addNode(int value)
 {
  
  if(value == data){ System.out.println("Similar values!");}
  
  if(value < data)
  {
   if( left==null ){ left = new Node(value); }
   else{left.addNode(value); System.out.println("added:" + value);}
  }
  else
  { 
   if( right==null ){ right = new Node(value);  }
   else{right.addNode(value);System.out.println("added:" + value);}
  }
  
  
 }


 //-----------------------------------------
 // Search nodes
 // ----------------------------------------
 public void search(int value) {
  
  if (data==value) {System.out.println("Item found:"+data);   }
  else if(data > value)
  {
   if (left==null) {
     System.out.println("Item not found!");
   } else {
     left.search(value);
   }
  }
  else 
  {
   if (right==null) {
     System.out.println("Item not found!");
   } else {
     right.search(value);
   }
  }
 }
 
 //-----------------------------------------
 // Depth first (level order traversal)
 // ----------------------------------------
  
 public void levelOrderTraversal(Node root)
 {
  System.err.println("Initiating level order traversal");
  
  if( root==null )System.out.println("No nodes in BST"); 
  Queue queue = new LinkedList();
  queue.add(root);
  
  while (!queue.isEmpty()) {
    
   Node current = queue.poll();
   System.out.print("-"+current.data);
   
   if(current.left != null)
   { queue.add(current.left);}


   if(current.right != null)
   { queue.add(current.right); }
  }
  
 }

 public void preOrderTraversal(Node current) {

  if (current == null) return;
  System.out.print(current.data+"-");
  
  preOrderTraversal(current.left);
  preOrderTraversal(current.right);

 }

 public void inOrderTraversal(Node current) {
  
  if (current == null) return;
  inOrderTraversal(current.left);
  System.out.print(current.data+"-");
  inOrderTraversal(current.right);

 }

 public void postOrderTraversal(Node current) {
  
  if (current == null) return;
  postOrderTraversal(current.left);
  postOrderTraversal(current.right);
  System.out.print(current.data+"-");
 }
 
 }

Binary search tree - Tree - Java code

This class's goal is to crate an object that can hold other node objects and form the tress structure along with its operation.
package BinaryTreePackage;

public class BinaryTree {

 private Node root;
 
 public BinaryTree() {
  root = null;
 }
 
 public void addNode(int value)
 {
  if(root==null){root = new Node(value); }
  else{root.addNode(value);}
 }
 
 
 public void search(int value)
 {
  if (root==null) {System.out.println("No items in BST");  }
  else{root.search(value);}
 }
 
 
 public void levelOrderTraversal()
 {
  if(root==null) return;
  else root.levelOrderTraversal(root);
 }

 public void preOrderTraversal()
 {
  System.out.println("Initiating pre order traversal");
  if(root ==null) return;
  else root.preOrderTraversal(root);
 }
 
 public void inOrderTraversal()
 {
  System.out.println("Initiating in order traversal");
  if(root ==null) return;
  else root.inOrderTraversal(root);
 }
 
 public void postOrderTraversal()
 {
  System.out.println("Initiating post order traversal");
  if(root ==null) return;
  else root.postOrderTraversal(root);
 }
 
}







Share:

Tree - Data strucrure

Data structures like array, linked lists, stacks, queues are linear data structures.
Tree data structure is a non-linear data structure and its good to use in hierarchical data situations.

Image obtained from : http://www.teach-ict.com/as_as_computing/ocr/H447/F453/3_3_5/data_structures/miniweb/images/tree.jpg


Tree terminogy

Lets say we have a tree T with set of nodes where nodes have parent child relationship.

root - which has no parents. Only one root is in T
nodes - nodes other than root are called nodes and they all have a parent. and may be children as well
siblings - if some nodes share a same parent they are called siblings
internal nodes - If a node has children its is a internal nodes
external nodes/leaves - if a node has no children it is an external node or a leave
edge - A connection between parent and a child
path - sequence of edges from a source node to destination node
ordered tree - where siblings have a meaningful linear fashion relationship

There are various kinds of trees that can be implemented using these concepts.
Ex: Binary threes, red black treens, 234 trees.
Share:

Doubly Linked List - Data Structure

One natural characteristic of singly linked list (SLL)is that it is asymmetric. Meaning a node only knows its next node. In doubly linked list (DLL) a node can refer to its previous and next nodes.









Image obtained from : http://www.cs.usfca.edu/~srollins/courses/cs112-f07/web/notes/linkedlists/ll5.gif

In DLL we need 2 sentinels (Dummy nodes) for header and trailer.

Initially empty list is created so that,
header.next -----points to ------>  trailer
trailer.prev -----points to -------> header

header and trailer sentinels(dummy nodes) never change. Their next and prev changes with the intermediate nodes.


Doubly linked lists implementation - for illustration purposes



private static class Node<E> {
  private E element;
  private Node<E> prev;          // this the only additional reference comparing with singly linked list 
  private Node<E> next;

  public Node(E e, Node<E> p, Node<E> n) {
   element = e;
   prev = p;
   next = n;
  }
}


public class DoublyLinkedList<E> {

  private Node<E> header;   // header and trailer sentimental for setting up the boundary of the list
  private Node<E> trailer;
  private int size = 0;


public DoublyLinkedList( ) {
  header = new Node<>(null, null, null);      // initiall creates an emptry list where header and trailer
  trailer = new Node<>(null, header, null);   // sentinels are referred to each other
  header.setNext(trailer);
 }



public int size( ) { return size; }


public boolean isEmpty( ) { return size == 0; }

public E first( ) {
  if (isEmpty( )) return null;
  return header.getNext( ).getElement( );
}


public E last( ) {
  if (isEmpty( )) return null;
  return trailer.getPrev( ).getElement( );
}


public void addFirst(E e) {
  addBetween(e, header, header.getNext( ));    // adds e node between header and header.next
 }


public void addLast(E e) {
  addBetween(e, trailer.getPrev( ), trailer);     // add e node between trailer and trailer.prev
}


public E removeFirst( ) {
  if (isEmpty( )) return null;                       // cant remove node from an empty list
  return remove(header.getNext( ));          
}


public E removeLast( ) {
  if (isEmpty( )) return null;
  return remove(trailer.getPrev( ));
}


private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
  
  // newest node's next and previous is set to left side and right side nodes
  Node<E> newest = new Node<>(e, predecessor, successor);
 // left sides nodes next is set to newest node
  predecessor.setNext(newest);
  //right side nodes previous is set to newest node
  successor.setPrev(newest);
  size++;
}

private E remove(Node<E> node) {
  Node<E> predecessor = node.getPrev( );    // Temporary predecessor and successor is set
  Node<E> successor = node.getNext( );       //  their next and prev is set to each other
  predecessor.setNext(successor);                 //
  successor.setPrev(predecessor);                  //
  size−−;
  return node.getElement( );
}
Share:

Circular Linked Lists - Data Structures

There are some times data can be efficiently stored in a linked list like data structure but in a circular fashion. Just like a wheel. That is when circular linked lists come to play.


Basics about RR algorithm 

Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive).  - Wikipedia

What happens basically in RR is to schedule processes in an OS or a packets in a network. Each process is given a time slice to execute (ie: a certain time period ). When time slice is for a process is over it is being interrupted no matter the process is completed or not. Inactive processes are removed. Each active process is given a time slice.

RR can be implemented using linked list as well.

process p = Linked List.removeFirst( )       // remove process from first
Give a time slice to process p
Linked List.addLast(p)         // add process to last

Although this is possible with a singly linked list there are inefficiencies. This is because the same node removed from the first and inserted at the last end. Plus additional computations with regard to this (ex: numberOfNodes --, numberOfNodes ++ ) have to be executed redundantly.


Circular linked list

What is different with circular LL and singly LL ?
-- singly LL's tail node'next refers to null
-- circular LL's tail node'next refers to head node

In circular ll it is enough to maintain head or tale reference because head can be located by tale.next reference.


Image obtained from :https://en.wikipedia.org/wiki/Linked_list



Implementation of Circular Linked List (CCL) ( except node implementation)

public class CircularlyLinkedList<E> {

private Node<E> tail = null;    // tail is referred to null initially
private int size = 0;                  // initial size is is 0

public CircularlyLinkedList( ) { }     // If needed tail and size could be initialized here

public int size( ) { return size; }     // gives number of nodes in CLL

public boolean isEmpty( ) { return size == 0; }       // Checks if CCL is empty


public E first( ) {
  if (isEmpty( )) return null;
  return tail.getNext( ).getElement( );       // return tail.next. which means the first node
}


public E last( ) {
  if (isEmpty( )) return null;
  return tail.getElement( );                  // returns tail node
}


public void rotate( ) {
  if (tail != null)
  tail = tail.getNext( );                      // tail reference is passed to next node
}                                                     // number of nodes doescnt change except for tail position


public void addFirst(E e) {

  if (size == 0) {
    tail = new Node<>(e, null);       // at first the tail is the new node. new node is also the first node
    tail.setNext(tail);
  } 
 else {
    Node<E> newest = new Node<>(e, tail.getNext( ));     // newly added node refers to previous head
    tail.setNext(newest);                                           // tail stays the same because insertion is at front. new tail.next that means head refers to the newly added node
 }
 size++;
}



public void addLast(E e) {
   addFirst(e);                           // add a node to front
   tail = tail.getNext( );            // set tail to front added node
}


public E removeFirst( ) {
  if (isEmpty( )) return null;                   // cant remove nodes from an empty list

 Node<E> temp = tail.getNext( );              // head node is assigned to a node

 if (head == tail) tail = null;                    // When there is only one node left

 else {
   tail.setNext(head.getNext( ));           // tail stays the same. tail.next is referred to temp.next
   size−−;
   return head.getElement( );
 }
}

Share:

Singly Linked List - Data structure

One major advantage with using arrays as a data structure is it is always fixed size. Linked lists data structure can solve that storage limit with arrays. Lists have in flavours of singly and doubly. We will consider singly lists here.

Singly linked lists are like a chain. You have the first node and you can keep adding nodes for infinity. One thing is to identify the ends of the chain we have some mechanisms like painting chain ends in different colors.

Basic feature of linked lists

Linked list is composed node nodes.
Each node has to parts. 
- First part is to store data(ints, strings or object types). 
- Second part is to store a reference to the next node. (Like in the chain example)
First node is called the head node. (This is how we identify the front)
Last node is marked as the node where its next is null which means not references to any node
size can be introduced as a variable to detect lists number of items in any given time.



Image obtained from : http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html




Node declaration in Java (For illustration purposes)



private static class Node<E>
{
   private E data;
   private Node<E> next;

   public Node(E d, Node<E> n)
   {
      data = d;
      next = n;
   }
}

Linked List Operations in Java - (For illustration purposes)


Adding a data item from the head of the lists

Process addFirst(e):
newest = Node(e)       // New node is created to add first
newest.next = head    // created nodes next reference is pointed to current head node
head = newest            // Set newly added node as the head
size = size + 1           // now that a new node is added to the chain increment its size by 1


Adding a data item from the back of the list

Process addLast(e):
newest = Node(e)       // create a new node to add at the last
newest.next = null     // to make newly created node last node, set its next to last
tail.next = newest      // set current tails next to newly crated node. So that current tails is now 1 node before last
tail = newest              // set tail to newly created node so that it is officialy the last node
size = size + 1           // now that a new node is added to the chain increment its size by 1

Remove data item from the head of the list

Process removeFirst( ):
if head == null then the list is empty // This is because cant remove a node from an empty list
head = head.next        // new head is set to next node. So that 2nd node is the new head 
size = size − 1     // New lists has 1 item less. But the reference from old new to new new is there


one major disadvantage in linked lists is it consumes more memory. This is because of the additional part for storing the reference.

See full implementation details here : http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html









Share: