Thursday, January 14, 2010

Develop Watchdog Timer

Summary...
Project nature is critical and I have a core P2P service (Windows service in vc++) running at backend. Requirement is that the service code should be taken care of by another watchdog (dog/timer used for guarding) application so that the original service ensures that it is always be in running in normal mode. Watchdog timer restarts the service if on the occurrence of some critical fault, or hang or service became meaningless by neglecting its assigned functionality supposed to do in normal operation.

It is another service with a timer of small interval which ensures following


  1. Is P2P Service running? If not, then start it.
  2. Is P2P Service hanging? If yes, kills it and restarts it. (This is decided if P2P Service is not consuming any CPU usage during the last X times and since the previous check. and/or CPU usage of P2P Service is aboove 80% constantly for X times) 
  3. Is P2P Service already running for X days? If yes, kills it and restarts it.
  4. Is Local IP address changed by the user? If yes, kills it and restarts it. (so that old IP addresses are removed from the tracker and fresh information is exchanged – other peers are able to make connections)
  5. Is new version of UI application and/or P2P Service downloaded by the session? If yes, kills it and install new version and then restarts it.
  6. Is P2P Service neglecting its normal operations/threads? If yes, , kills it and install (This is decided if P2P Service is not giving its benchmark signals to the Watchdog application for certain timeout and for certain times)

The Watchdog Service should maintain a log file of its actions (checking, restarting, killing) and extra information at the point of action, if any.

It should copies the log file of Core P2P Service to another specified location (same name, plus date and time concatenated), so that the information in it will be acrhived.

Tuesday, January 5, 2010

Base64 Encoding

Base64 Encoding

When text files are attached to SMTP emails, the text files can be attached in their plain text format. But binary files cannot be attached without some form of encoding. This encoding used in SMTP and many other Internet Protocols is called Base64 Encoding or simply MIME Base64.
 In VC++ binary data is stored in BYTE (unsigned char) which is Base256 (0 … 255) encoding and in not easily readable and printable. The base64 encoding use the character set encoding common to most environments and easy to understand/print.
Base64 uses 64 (0 … 63) characters where each character is using 6 bits.  The character set representation is as follows:
‘A’…’Z’ for (0-25),  ‘a’…’z’ for (26-51), ‘0’…’9’ for (52-61), ‘+’ for 62 and ‘/’ for 63 - making it 64 in total. ‘=’ character is used for padding in addition.
BYTE (unsigned char) Array vs. Base64 Encoded String
“I have a buffer as BYTE array. I want to convert it into human readable/printable form. I need data representation protocol and that is Base64 encoding. You can store this date in text, XML, or application configuration files.”
One character in C++ is minimum storage unit of 8 bits. A Base64 Encoded ASCII character represents 6 bits of Base2 (binary system).  So least common multiple of 8 and 6 is 24 which means
3 Bytes = 4 Base64 encoded characters = 24 bits in Base2
So the encoded value of SoS is U29T. Encoded in ASCII, S, o, S are stored as the bytes 83, 111, 83, which are 01010011, 01101111 and 01010011 in base 2. These three bytes are joined together in a 24 bit (24 = 8x3) buffer producing 010100110110111101010011. Packs of 6 bits are converted into 4 numbers (24 = 6x4) which are then converted to their corresponding values in Base 64.
Text
S
o
S
ASCII
83
111
83
Base2 Pattern (24 bits)
01010011
01101111
01010011
6 Bit Pattern
010100
110110
111101
010011
Index
20
54
61
19
Base64 Encoded ASCII
U
2
9
T

Above example shows that Base 64 encoding converts 3 uncoded BYTEs (simple ASCII characters) into 4 encoded ASCII characters. So Base64 encoded string is almost 4/3 (=1.33) times larger than that of corresponded simple ASCII string. The base64 encoding algorithm takes every three bytes of data and converts them into four bytes of printable encoded ASCII characters. If the size of the incoming byte array is not an exact multiple of three, the algorithm appends equal signs (one for each missing byte) at the end of the base64-encoded string. So there can be 0, 1 or 2 number of ‘=’ signs at the end depending upon the number zero 6-bits pattern found in last three-octet group of original string. This convention guarantees that the size of base64-encoded string will always be a multiple of four. The length of a base64-encoded string can be calculated as:
Base64 = ((Bytes + 3 - (Bytes % 3)) /3) x 4

where Base64 and Bytes indicate the number of bytes in the base64-encoded string and the original byte array respectively. You can use this formula to calculate the size of the column holding base64-encoded text.
For example, if a byte array contains 13 characters of the ASCII string "Hello, world!", the size of the corresponding base64-encoded string can be calculated as:

Base64 = ((13 + 3 - (13 % 3)) / 3) x 4 = 20 (bytes)

The resulting value will be "SGVsbG8sIHdvcmxkIQ==". The last two characters of the base64-encoded string contain two equal signs ("==") indicating the two missing bytes in the last three-byte block of the byte array.
 
Padding
The first case where you have one byte remaining, you should pad two additional bytes with all zeros onto the end of the binary sequence. You can then represent the one byte with two base-64 characters followed by two padding characters.
Let consider an example.
‘00000001’
Pad the single-byte instance with two more bytes of zeros.
‘00000001’ ‘00000000’ ‘00000000’
Now break up the binary sequence in sets of six bytes.
‘000000’ ‘010000’ ‘000000’ ‘000000’
Take the first two base-64 characters and pad two ‘=’ characters to the end of the sequence.
‘AQ==’
The second case is where you have two bytes remain.
‘00000010’ ‘00000001’
Here you should pad one additional zero byte to the end of the binary sequence.
‘00000010’ ‘00000001’ ‘00000000’
Now break up the binary sequence in sets of six bytes.
‘000000’ ‘100000’ ‘000100’ ‘000000’
We then take three base-64 characters and pad with one ‘=’ sign.
‘AgE=’
Line Length
To improve human readability in the stream the base64 specification requires that each line should be at most of 76 encoded base-64 characters in length. After each 76 characters, we should insert a carriage return and line feed (\r\n) into the stream. This increases the stream length by approximately 3%.
 
References:
Base64 on Wikipedia
 
How to Base64 by Randy Charles Morin
 
How to Calculate the Size of Encrypted Data?

Friday, April 25, 2008

One page summary of the Protocols of Zion

PROTOCOLS OF THE MEETINGS OF THE LEARNED ELDERS OF ZION
 Original connection http://www.iamthewitness.com/Protocols-of-Zion.htm

One Page Summary…
Goyim (non-Jews) are mentally inferior to Jews and can’t run their nations properly. For their sake and ours, we need to abolish their governments and replace them with a single government. This will take a long time and involve much bloodshed, but it’s for a good cause. Here’s what we’ll need to do:
* Place our agents and helpers everywhere
* Take control of the media and use it in propaganda for our plans
* Start fights between different races, classes and religions
* Use bribery, threats and blackmail to get our way
* Use Freemasonic Lodges to attract potential public officials
* Appeal to successful people’s egos
* Appoint puppet leaders who can be controlled by blackmail
* Replace royal rule with socialist rule, then communism, then despotism
* Abolish all rights and freedoms, except the right of force by us
* Sacrifice people (including Jews sometimes) when necessary
* Eliminate religion; replace it with science and materialism
* Control the education system to spread deception and destroy intellect
* Rewrite history to our benefit
* Create entertaining distractions
* Corrupt minds with filth and perversion
* Encourage people to spy on one another
* Keep the masses in poverty and perpetual labor
* Take possession of all wealth, property and (especially) gold
* Use gold to manipulate the markets, cause depressions etc.
* Introduce a progressive tax on wealth
* Replace sound investment with speculation
* Make long-term interest-bearing loans to governments
* Give bad advice to governments and everyone else
Eventually the Goyim will be so angry with their governments (because we’ll blame them for the resulting mess) that they’ll gladly have us take over. We will then appoint a descendant of David to be king of the world, and the remaining Goyim will bow down and sing his praises. Everyone will live in peace and obedient order under his glorious rule.


 Original connection http://www.iamthewitness.com/Protocols-of-Zion.htm

Wednesday, February 22, 2006

Search Keywords - Relationship between Pipes-and-Filters and Decorator design patterns

An article on the implementation of both Pipes-and-Filters architectural design pattern and Decorator pattern to address similar solution in .NET 2.0.
For more details and source code for my article please go to this Location on Codeproject.


Introduction

The article provides an implementation of the simple problem using both Pipes-and-Filters architectural pattern and Decorator pattern, and explores a relationship between them, if any.

Pipes-and-Filters Pattern - An architectural design pattern

Intent

The Pipes and Filters architectural pattern provides a structure for systems, having components that process a stream of data (filters) and connections that transmit data between adjacent components (pipes). This architecture provides reusability, maintainability, and decoupling for the system processes having distinct, easily identifiable, and independent but somehow compatible tasks.
The usage of Pipes and Filters pattern is limited to systems where the order in which filters are processed is strongly determined and sequential in nature. The pattern applies to problems where it is natural to decompose the computation into a collection of semi-independent tasks. In the Pipeline pattern, the semi-independent tasks represent the stages of the pipeline, the structure of the pipeline is static, and the interaction between successive stages is regular and loosely synchronous. A pipeline is a definition of steps/tasks that are executed to perform a business function. Each step may involve reading or writing to data confirming the “pipeline state,” and may or may not access an external service. When invoking an asynchronous service as part of a step, a pipeline can wait until a response is returned (if a response is expected), or proceed to the next step in the pipeline if the response is not required in order to continue processing.
Use the pipeline pattern when:
  • You can specify the sequence of a known/determined set of steps.
  • You do not need to wait for an asynchronous response from each step.
  • You want all downstream components to be able to inspect and act on data that comes from upstream (but not vice versa).
Advantages of the pipeline pattern include:
  • It enforces sequential processing.
  • It is easy to wrap it in an atomic transaction.
Disadvantages of the pipeline pattern include:
  • The pattern may be too simplistic to cover all cases in business logic, especially for service orchestration in which you need to branch the execution of the business logic in complex ways.
  • It does not handle conditional constructs, loops, and other flow control logic as it's mostly sequential in nature.

Background

Filters

The filters are the processing units of the pipeline. A filter may enrich, refine, process, or transform its input data.
  • It may refine the data by concentrating or extracting information from the input data stream and passing only that information to the output stream.
  • It may transform the input data to a new form before passing it to the output stream.
  • It may, of course, do some combination of enrichment, refinement, and transformation.
A filter may be active (the more common case) or passive.
  • An active filter runs as a separate process or thread; it actively pulls data from the input data stream and pushes the transformed data onto the output data stream.
  • A passive filter is activated by either being called:
    • as a function, a pull of the output from the filter.
    • as a procedure, a push of output data into the filter.

Pipes

The pipes are the connectors having the following links between a data source and the first filter, between filters, between the last filter and a data sink. As needed, a pipe synchronizes the active elements that it connects together.

Data source

A data source is an entity (e.g., a file or input device) that provides the input data to the system. It may either actively push data down the pipeline or passively supply data when requested, depending upon the situation.

Data sink

A data sink is an entity that gathers data at the end of a pipeline. It may either actively pull data from the last filter element or it may passively respond when requested by the last filter element. 
 
For more details and source code for my article please go to this Location on Codeproject.

Sunday, December 25, 2005

Cell Phone - An implementation of behavioral pattern to describe the states

An article on the State pattern describing cell phone states behavior.

For more details and source code for my article please go to this Location on Codeproject

Introduction

A Finite State Machine (FSM) is the significant model for designing dynamic behavior, i.e., to receive events from a finite collection and keep track of the current state and the previous state, and provide a mechanism for changing states based on an event from a set of predetermined events. The implementation is the state-transition system based on the trendy cell phone activities developed in .NET.

Finite State Machine

No FSM is an isolated process. Every aspect of an FSM depends on its context, requirements, limitation of programming language and other factors. The most popular non-object oriented implementations of FSM are cascading if statements, nested switch statements, state transition tables and code using goto statements. Their popularity is due to fast execution, but at the cost of weaker reliability and maintainability. On the other hand, the pure Object-Oriented FSM designs require “Complete Reification”, that is, making every element of the state machine an object. Finite State Machine implementation comes from “the Gang of Four” (GoF) with their design pattern “The State Pattern”. This is an object-oriented approach with a tightly coupled structure of classes or class hierarchy for each state and event.
A Finite State Machine has at least two common characteristics:
  1. First, a distinct, finite number of states exist.
  2. Second, methods exist that facilitate movement between the states, i.e., transition.

The State Pattern

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. The solution is provided in the following way; each state is encapsulated in a separate class that implements the common State interface (IState) and defining the behavior to handle all external events. The Context class is responsible for maintaining the knowledge of the current state and other data. External entities communicate with the system via the Context class that forwards requests to the current state object. There are the following consequences while using the State Pattern.
  1. State specific behavior is localized and partitioned between different states.
  2. State transition is performed exactly in the code.
  3. If an object has many states, many concrete classes need to be created, which may result in an excessively large class hierarchy.
  4. The Context class delegates all external events to corresponding methods in the interface of the State class; it is responsible for providing the correct mapping. This results in a tight coupling between the Context class and concrete events.
  5. The Context class keeps one instance of each state in a hash table so that state changes does not result in creation or deletion of objects.
  6. The Context class keeps many state objects, but only one is active (i.e., responds to events) at a given time.
  For more details and source code for my article please go to this Location on Codeproject.