Communication

Shared by: ajizai
Categories
Tags
-
Stats
views:
2
posted:
9/17/2012
language:
Unknown
pages:
6
Document Sample
scope of work template
							/* --------------------------------------------------------------------   */
/*                                                                        */
/*                   <Communication>.c - description                      */
/*                         -----------------------                        */
/*                                                                        */
/* --------------------------------------------------------------------   */
/*                                                                        */
/* ------------------------------------------------------------ INCLUDE   */
/* ---------------------------------------------------- Include système   */
#include <msgQLib.h>
#include <semLib.h>
/* -------------------------------------------------- Include personnel   */
#include "Communication.h"
#include "Carton.h"/* include pour avoir wdPiece */

/* -------------------------------------------------------------------- */
/*                                                                      */
/*                        PRIVE                                         */
/*                                                                      */
/* --------------------------------------------------------- Constantes */


/* -------------------------------------------------------------- Types */


/* -------------------------------------------------- Variables privees */


/*--------------------------------------------------- Fonctions privees   */
/* --------------------------------------------------------------------   */
/*                                                                        */
/*                        PUBLIC                                          */
/*                                                                        */
/* ------------------------------------------------ Variables publiques   */


/*----------------------------- Réalisation de la tâche <COMMUNICATION> */

static void recupPara ( int newFd, int nRead )
/* Mode d'emploi : Passage en paramètre du nombre de d'octets lus et du
descripteur socket

     Contrat : socket initialisee et connexion etablie
*/
{
       int i=0;
       Parametrage paramRecu;
       /*Recuperation du nombre de lots produire*/
       nRead = read ( newFd, ( char * ) & mpNbLots, sizeof ( mpNbLots ) );

       if ( nRead == ERROR )
       {
             perror ( "read" );
       }
       for(;i<mpNbLots;i++)
       {
             /* Remplissage de Lot venant du poste distant */
           nRead = read ( newFd, ( char * ) & paramRecu, sizeof ( paramRecu ) );
              if ( nRead == ERROR )
              {
                    perror ( "read" );
              }
              else
              {
                    paramLots[i] = paramRecu;
                    if(i == (mpNbLots-1))
                    {
                          paramLots[i].dernierLot = 1;
                    }
                    else
                    {
                          paramLots[i].dernierLot = 0;
                    }
              }
       }

       nbPieces = 0;
       nbPiecesDef = 0;

       mpVoyant = VERT;
       mpClapet = OUVERT;
}


int Communication()
/* Mode d'emploi : Passage en paramètre du nombre de d'octets lus et du
descripteur socket

     Contrat : socket initialisee et connexion etablie
*/
{

/* ***********Phase d'initialisation************************************ */

       struct sockaddr_in   serverAddr;     /* adresse de la socket du serveur */
       struct sockaddr_in   clientAddr;     /* adresse de la socket du client*/
       int                  sockAddrSize;   /* taille de la structure de
                                             l'adresse de la socket */
       extern int             sFd;          /* socket file descriptor */
       extern int             newFd;        /* socket descriptor from accept */
       int                    nRead;        /* nombre d'octets lus*/
       char * adr;                          /* affichage de l'adresse IP en
                                                xxx.xxx.xx.xx */
       unsigned short port;                   /* numero de port du client */

       int retour;

       extern MESSAGE_COMM msg;
       MESSAGE_LOG msgLog;
       MESSAGE_INFO_CARTON msgCarton;


       /* definition de l'adresse locale */
sockAddrSize = sizeof ( struct sockaddr_in );
bzero ( ( char * ) & serverAddr, sockAddrSize );
serverAddr.sin_family = AF_INET;
serverAddr.sin_len = ( u_char ) sockAddrSize;
serverAddr.sin_port = htons ( SERVER_PORT_NUM );
serverAddr.sin_addr.s_addr = htonl ( INADDR_ANY );

/* creation d'une socket TCP */

if ( ( sFd = socket ( AF_INET, SOCK_STREAM, 0 ) ) == ERROR )
{
      perror ( "socket" );
      return ( ERROR );
}

/* attacher socket a l'adresse locale */

if ( bind ( sFd, ( struct sockaddr * ) & serverAddr, sockAddrSize ) ==
     ERROR )
{
      perror ( "bind" );
      close ( sFd );
      return ( ERROR );
}

/* creation pile pour requete de connexion client */

if ( listen ( sFd, SERVER_MAX_CONNECTIONS ) == ERROR )
{
      perror ( "listen" );
      close ( sFd );
      return ( ERROR );
}


FOREVER
{


     if ( ( newFd = accept ( sFd, ( struct sockaddr * ) & clientAddr,
           & sockAddrSize ) ) == ERROR )
     {

           perror ( "accept" );
           close ( sFd );
           return ( ERROR );
     }
     printf ( "Connexion acceptee\n" );

  adr = ( char * ) malloc ( sizeof ( char ) );
  adr = inet_ntoa ( clientAddr.sin_addr );
  port = ntohs ( clientAddr.sin_port );

  printf ( "Connexion acceptee du client (Internet Address %s,
            port %d):\n", adr, port );

     /* Récupération des paramètres */
           recupPara ( newFd, nRead );

           /* Syncro d'initialisation */
           semGive(semBInitialisation);

/* ***********Phase Moteur*************************************/

           for ( ; ; )
           {


                 /* Récupération des messages */
           msgQReceive ( idBalComm, ( char * ) & msg,
                 sizeof ( MESSAGE_COMM ), WAIT_FOREVER );


                 /* Envoi d'un message à l'IHM */
                 if ( write ( newFd, ( char * ) & msg, sizeof ( msg ) )
                        == ERROR )
                 {
                       perror ( "write" );
                 }

                 /* Lecture du retour de l'IHM en cas d'erreur*/
                 switch(msg.type)
                 {
                       case ERREUR_ABSENCE_CARTON_COMM:
                       case ARRET_URGENCE_COMM:
                       case PLUS_DE_PIECES_COMM:
                       case NB_MAX_CARTONS_ATTENTE_COMM:
                             nRead = read ( newFd, ( char * ) & retour,
                                               sizeof ( int ) );
                             if ( nRead == ERROR )
                             {
                                   perror ( "read" );
                             }
                             break;
                 }

                 /* Traitement du retour de l'IHM */
                 if ( retour == REPRISE)
                 {
                       mpVoyant = VERT;
                       mpClapet = OUVERT;

                         taskResume(idTImpression);

                       /*Activation du watchdog pour réarmer le timer*/
                       wdStart(idWdPiece,
                 paramLots[numLotCourant].tpsMaxAttPieces*sysClkRateGet(),
                                   (FUNCPTR)wdPiece,0);
                 }
                 else if ( retour == NOTREPRISE )
                 {
                       /* fermeture du descripteur de socket */
                       close ( newFd );
                       close ( sFd );
                 /* Reset des Variables et envoie de messages d'arrets */
                       msgCarton.idCarton = 0;
                       msgCarton.nbPieces = 0;
                       msgCarton.type = STOP;
                       msgCarton.Date = time(NULL);
                       msgQSend(idBalInfoCarton,(char*) &msgCarton,
                                   sizeof(msgCarton),NO_WAIT,MSG_PRI_NORMAL);


                       msgLog.logMsg = "Arret utilisateur";
                       msgLog.type = STOP;
                       msgQSend(idBalLog,(char*) &msgLog,
                                   sizeof(msgLog),NO_WAIT,MSG_PRI_NORMAL);

                 }

           }
           /* Fin du for */
     }
     /* Fin du forever */

}
/*----- fin de <COMMUNICATION> */
/* -------------------------------------------------------------------- */
/*======================================================================*/
/*                                                                      */
/*                      Communication.h - description                   */
/*                          ----------------------                      */
/*                                                                      */
/*======================================================================*/
/* ---- Interface de la tâche <Communication> (fichier Communication.h) */
#ifndef COMMUNICATION_H
#define COMMUNICATION_H

/* --------------------------------------------------------------------   */
/* Rôle de la tâche <Communication>
*/
/* Cette tache permet de mettre en place la communication entre l'IHM     */
/* et VxWorks. Elle envoie des messages et attend des retours en cas      */
/* d'erreur.                                                              */
/* --------------------------------------------------------------------   */
/*                                                                        */
/* -------------------------------------------------------------INCLUDE   */
#include "global.h"
/*                                                                        */
/* ------------------------------------------------Interfaces utilisées   */
#include <msgQLib.h>
#include <semLib.h>
#include <sockLib.h>
#include <inetLib.h>

/*---------------------------------------------------------- Constantes */


/*--------------------------------------------------------------- Types */
struct request
{
      int reply;                         /* TRUE = request reply from server
*/
      int msgLen;                        /* length of message text */
      char message [ REQUEST_MSG_SIZE ]; /* message buffer */
};


/* --------------------------------------------------------------------   */
/*                                                                        */
/*                                                               PUBLIC   */
/*                                                                        */
/*------------------------------------------------- Fonctions publiques   */


/*--------------------------------------------------------- Tache
<Communication> */

int Communication();
/*Mode d'emploi :
Contrat :
*/
/* -------------------------------------------------------------------- */
#endif // COMMUNICATION_H

						
Related docs
Other docs by ajizai
True scary creatures.ppt - bishopcook09
Views: 280  |  Downloads: 0
Programa del curso - imfohsa
Views: 258  |  Downloads: 0
Profit Optimizer - Your Business Coaching Club
Views: 238  |  Downloads: 0
Professional body data
Views: 252  |  Downloads: 1
produkter
Views: 382  |  Downloads: 1
Produksjonsstyring Mongstad
Views: 224  |  Downloads: 0
Production optimization - PPT presentation
Views: 253  |  Downloads: 0