Tuesday 3 April 2012

Finite State Machine

#include<iostream>
#include<pthread.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;


char x_buff[1000];
char t_buff[1000];

/*Call setup mode is used to establish SVCs between DTE devices. A PLP uses the X.121 addressing
scheme to set up the virtual circuit. The call setup mode is executed on a per-virtual circuit basis,
which means that one virtual circuit can be in call-setup mode while another is in data-transfer mode.
This mode is used only with SVCs, not with PVCs.
*/

/*  data-transfer  mode is used for transferring data between two DTE devices across a virtual circuit.
In this mode, PLP handles segmentation and reassembly, bit padding, and error and flow control.
This mode is executed on a per-virtual circuit basis and is used with both PVCs and SVCs.

Idle mode is used when a virtual circuit is established but data transfer is not occurring. It is executed
on a per-virtual circuit basis and is used only with SVCs.

Call-clearing mode is used to end communication sessions between DTE devices and to terminate
SVCs. This mode is executed on a per-virtual circuit basis and is used only with SVCs.

Restarting mode is used to synchronize transmission between a DTE device and a locally connected
DCE device. This mode is not executed on a per-virtual circuit basis. It affects all the DTE device’s
established virtual circuits

*/

/* ************************TYPES OF PACKETS */
/*Four types of PLP packet fields exist:
1  General Format Identifier (GFI)—Identifies packet parameters, such as whether the packet
   carries user data or control information, what kind of windowing is being used, and whether
   delivery confirmation is required.
2  Logical Channel Identifier (LCI)—Identifies the virtual circuit across the local DTE/DCE
   interface.
3  Packet Type Identifier (PTI)—Identifies the packet as one of 17 different PLP packet types.
4  User Data—Contains encapsulated upper-layer information. This field is present only in data
   packets. Otherwise, additional fields containing control information are added.
               I bits
GFI               4
LCI              12
PTI              8
User Data       variable

*/
class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}

int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}

void reset()
{
T1=0;
T2=0;
}

~timer()
{
T1=0;
T2=0;
}
};
enum PLP
{
IDLE=1,
CALL_SETUP,
CALL_CLEAR,
DATA_TRANSFER,
RESTART
};
void *changer(void * arg);
void *FSM(void * arg);
PLP packet;
void change_state(PLP nextstate)
{
packet=nextstate;
}

void call_setup()
{
cout<<"Now in call _setup function "<<endl;
FILE *fp;
char file_name[50];
cout<<"enter file name to open";
gets(file_name);
fp=fopen(file_name,"r");
if(fp==NULL)
{
cout<<"File open Failed"<<endl;
change_state(CALL_SETUP);
}
else
{
fread(t_buff,800,800,fp);


//cout<<"waiting for input"<<endl;
if(t_buff)
{
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
cout<<"Input data is "<<t_buff<<endl;
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
}
change_state(IDLE);

}
}

void idle()
{
cout<<"Now in idle function "<<endl;
strcat(t_buff,"+IDLE_HEADER");
change_state(DATA_TRANSFER);
}

void call_clear()
{
cout<<"Now in call_clear function "<<endl;
strcpy(x_buff,t_buff);
cout<<x_buff<<endl;
strcpy(t_buff,"");
change_state(RESTART);

}

void restart()
{
cout<<"Inside restart function "<<endl;
//change_state(CALL_SETUP);
cout<<"setting value of state machine using thread"<<endl;
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
cout<<x_buff<<endl;

//pthread_create(&t1,0,FSM,0);
//pthread_join(t1,NULL);
}
void data_transfer()
{
static int counter;

counter++;
cout<<"Now in data_transfer function"<<endl;
cout<<"Session =="<<counter<<endl;
if(counter < 6)
{
cout<<"NXT STATE WILL BE IDLE "<<endl;
char *p;
p=t_buff;
if(strlen(t_buff)<32)
{
strcpy(x_buff,p);
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
//cout<<"exceed"<<endl;
}
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(IDLE);
pthread_mutex_unlock( &mutex1 );
}
else
{
counter=0;
cout<<"NEXT STAE WILL BE CALL CLEAR max data _cycle Event"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(CALL_CLEAR);
pthread_mutex_unlock( &mutex1 );
}
}

void *changer(void * arg)
{
cout<<"--------------CHANGER THREAD CALLED---------------------"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
packet=CALL_SETUP;
pthread_mutex_unlock( &mutex1 );
}

void *FSM(void * arg)
{
static int co;
co++;
cout<<" Inside FSM CALL VALUE IS NOW==="<<co<<endl;
switch(packet)
{
case IDLE:
idle();
break;
case CALL_SETUP:
call_setup();
break;
case CALL_CLEAR:
call_clear();
break;
case DATA_TRANSFER:
data_transfer();
break;
case RESTART:
restart();
break;
default:
packet=CALL_SETUP;
break;
}

}

timer tim1; // hu ha global variables timer

int main()
{
pthread_t t;
tim1.start();
int i=0;
while(1)
    {
    try
    {
   
if((tim1.get_time() > 300) && !(tim1.get_time() < 0 ))
        {
//cout<<"Time value 150 elasped"<<tim1.get_time()<<endl;
if(tim1.get_time() < 300)
{
throw 1;
}
tim1.reset();
tim1.stop();
tim1.start();
i++;
pthread_create(&t,0,FSM,0);
pthread_join(t,NULL);


if(i%10 == 0)
{

pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
throw 1;
}



        } //edif
else
        {
static int x;
tim1.stop();
x++;
if(x==1)
{
for(int k=0;k<tim1.get_time();i++)
cout<<"--";
cout<<"SYSTEM INTIALIZED "<<endl;
}


    }
    }
    catch(int)
    {
    cout<<"******************************************************************"<<endl;
    cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
    cout<<"Current state is "<<packet<<endl;
    cout<<"Current time is"<< tim1.get_time()<<endl;
    cout<<"TIMER RESET CALLED "<<endl;
    tim1.reset();
    cout<<"TIMER RESTARTED"<<endl;
    tim1.start();
    }
    catch(...)
    {
   
    cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
    cout<<"Current state is "<<packet<<endl;
    cout<<"Current time is"<< tim1.get_time()<<endl;
    cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
    cout<<"TIMER RESTARTED"<<endl;
    tim1.start();
    }
}


//FSM();
return 0;
}

Quick questions

  1. Why do we use reference and pointers in copy constructor?
  2.  Can exception be thrown from constructor or ~Distrct?
  3.  Main is a entry point of a program how can be call another function  before main?
  4.  Find mid point of linked list only transverse it once?
  5.  What is differences between Mutex and semaphores?
  6. Explain resource sharing in multi threaded environment? 
  7. How static and dynamic libraries are linked ?
  8. Are virtual constructor and distructors possible?
  9. Give syntax for operator overloading for == operator?

==================Level 2========================
Explain Socket binding.
Can two applications have binding on the the same port?
what is the disadvantage of dynamic memory allocation?
how you debug your applications in your current evironment ?
+++++++++++++++++++++++++++++++++++++++++++++++++++
Write down a program to find prime numbers?
Explain why your RTOS is diffrent then a normal kernel?
 

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc