IBM
Contents Index Previous Next



Appendix D: The Environment Functions


This section contains the environment functions included in the example. Note that this example is not updated to use the ASCII encoder.

/****+*******************************************************
00   sctEnv.c for SimplePhoneSys
************************************************************/
#include "scttypes.h"
#include <stdio.h>

#include "phone.ifc"

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#ifdef AIXV3CC
#include <sys/select.h>
#endif
#include <sys/un.h>

#include <unistd.h>
#define getdtablesize() ( (int) sysconf(_SC_OPEN_MAX) )

int Out_Socket, In_Socket;
struct sockaddr_un  Connection_Socket_Addr;
struct sockaddr_un  Connected_Socket_Addr;

#ifdef ULTRIXCC
#define PRINTF(s) \
  printf(s); \
  /* flush output to get a prompt */ \
  fflush( stdout)
#else
#define PRINTF(s) printf(s)
#endif

#ifdef XENV

/*#if !defined(XPMCOMM) && defined(XENV)*/
/*---+-------------------------------------------------------
     xGlobalNodeNumber  extern
-----------------------------------------------------------*/
#ifndef XNOPROTO
 int 
xGlobalNodeNumber( void )
#else
 int 
xGlobalNodeNumber()
#endif
{
  static int ProcId = -1;

  if (ProcId < 0)        
    ProcId = getpid();
  return (ProcId);
}
/*#endif*/

/*---+-------------------------------------------------------
     xInitEnv  extern
-----------------------------------------------------------*/
#ifndef XNOPROTO
 void
xInitEnv( void )
#else
 void
xInitEnv()
#endif
{
  fd_set  readfds;
  int  addr_size;
  int  Connection_Socket;
  char  TmpStr[132];
  struct timeval  t;

  t.tv_sec = 60;
  t.tv_usec = 0;
   
  if ( (Connection_Socket = socket(PF_UNIX,SOCK_STREAM,0)) < 0 
) {
    PRINTF("\nError: No Connection_Socket available!\n");
    SDL_Halt();
  }
  if ( (Out_Socket = socket(PF_UNIX,SOCK_STREAM,0)) < 0 ) {
    PRINTF("\nError: No Out_Socket available!\n");
    SDL_Halt();
  }

  sprintf(Connection_Socket_Addr.sun_path,
          "/tmp/Phone%d", xGlobalNodeNumber());
  Connection_Socket_Addr.sun_family = PF_UNIX;
  if ( 0 > bind(Connection_Socket, &Connection_Socket_Addr, 
           strlen(Connection_Socket_Addr.sun_path)+2) ) {
    PRINTF("\nError: Bind did not succeed!\n");
    SDL_Halt();
  }
  listen(Connection_Socket, 3);

  sprintf(TmpStr, "\nMy Pid: %d\n", xGlobalNodeNumber());
  PRINTF(TmpStr);
  PRINTF("\nConnect me to: ");

  FD_ZERO(&readfds);
  FD_SET(1,&readfds);
  FD_SET(Connection_Socket,&readfds);                 
  if ( 0 < select(getdtablesize(), &readfds,
                  (fd_set*)0, (fd_set*)0, &t) ) {
    if ( FD_ISSET(1, &readfds) ) {
      (void)gets(TmpStr);
      sscanf(TmpStr, "%s", TmpStr);
      sprintf(Connected_Socket_Addr.sun_path, "/tmp/Phone%s",
              TmpStr);
      Connected_Socket_Addr.sun_family = PF_UNIX;
      if (connect(Out_Socket, (struct sockaddr 
*)&Connected_Socket_Addr,
              strlen(Connected_Socket_Addr.sun_path)+2) < 0) {
        PRINTF("Error from connect\n");
        SDL_Halt();
      }
      FD_ZERO(&readfds);
      FD_SET(Connection_Socket,&readfds);                 
      if ( 0 < select(getdtablesize(), &readfds, (fd_set*)0,
                      (fd_set*)0, &t) ) {
        if ( FD_ISSET(Connection_Socket, &readfds) ) {
          addr_size = 
strlen(Connection_Socket_Addr.sun_path)+2;
          In_Socket = accept(Connection_Socket, 
&Connection_Socket_Addr, 
                             &addr_size);
        }
      } else {
        PRINTF("\nError: Timed out\n");
        SDL_Halt();
      }
    }
    else if ( FD_ISSET(Connection_Socket, &readfds) ) {
      addr_size = strlen(Connection_Socket_Addr.sun_path)+2;
      In_Socket = accept(Connection_Socket, 
&Connection_Socket_Addr, 
                         &addr_size);
      FD_ZERO(&readfds);
      FD_SET(1,&readfds);
      if ( 0 < select(getdtablesize(), &readfds,
                      (fd_set*)0, (fd_set*)0, &t) ) {
        if ( FD_ISSET(1, &readfds) ) {
          (void)gets(TmpStr);
          sscanf(TmpStr, "%s", TmpStr);
          sprintf(Connected_Socket_Addr.sun_path, 
"/tmp/Phone%s",
                  TmpStr);
          Connected_Socket_Addr.sun_family = PF_UNIX;
          if (connect(Out_Socket, (struct sockaddr 
*)&Connected_Socket_Addr,
                  strlen(Connected_Socket_Addr.sun_path)+2) < 
0) {
            PRINTF("Error from connect\n");
            SDL_Halt();
          }
        }
      } else {
        PRINTF("\nError: Timed out\n");
        SDL_Halt();
      }
    }
  } else {
    PRINTF("\nError: Timed out\n");
    SDL_Halt();
  }

  PRINTF("\n\n************ Welcome to SDT Phone System 
************\n");
  PRINTF("\nphone -> ");
}    

/*---+-------------------------------------------------------
     xCloseEnv  extern
-----------------------------------------------------------*/
#ifndef XNOPROTO
 void
xCloseEnv( void )
#else
 void
xCloseEnv()
#endif
{
  close(Out_Socket);
  close(In_Socket);
  unlink(Connected_Socket_Addr.sun_path);
  unlink(Connection_Socket_Addr.sun_path);
  PRINTF("\nClosing this session.\n");
}
/*---+-------------------------------------------------------
     xInEnv  extern
-----------------------------------------------------------*/
#ifndef XNOPROTO
 void
xInEnv( SDL_Time  Time_for_next_event ) 
#else
 void
xInEnv( Time_for_next_event )
  SDL_Time  Time_for_next_event; 
#endif
{
  struct timeval t;
  fd_set         readfds;
  char          *Instr;
  int            NrOfReadChars; 
  char           SignalName = '\0';
  xSignalNode    yOutputSignal;
  int            i = 0;
  char           chr = '\0';
   
  t.tv_sec = 0;
  t.tv_usec = 1000;
  FD_ZERO(&readfds);
#ifndef XMONITOR
  FD_SET(1,&readfds);
#endif
  FD_SET(In_Socket,&readfds);         
  if ( select(getdtablesize(),&readfds,0,0,&t) > 0 ) {
#ifndef XMONITOR
    /*SDL-signal TermInput */
    if FD_ISSET(1, &readfds) { 
      Instr = (char *)xAlloc(132);
      Instr[0]='L';
      Instr++;
      (void)gets(Instr);
      yOutputSignal = xGetSignal(TermInput, xNotDefPId, xEnv);
      xAss_SDL_Charstring(
        &((yPDP_TermInput)(OUTSIGNAL_DATA_PTR))->Param1, --
Instr,XASS);
      SDL_Output(yOutputSignal, (xIdNode *)NIL);
      xFree((void**)&Instr);
    }
#endif
    if FD_ISSET(In_Socket, &readfds) {
      Instr = (char *)xAlloc(151);
      do {
        read(In_Socket, &chr, 1);
        Instr[i++] = chr;
      } while ( chr!='\0' );
      sscanf(Instr, "%c", &SignalName);

      if ( SignalName == 'M' ) {
        /* SDL-signal Message */
        yOutputSignal = xGetSignal(Message, xNotDefPId, xEnv);
        sscanf(
          Instr+1, 
          "%d %x%n", 
          &(((yPDP_Message)(OUTSIGNAL_DATA_PTR))-
>Param2.GlobalNodeNr), 
          &(((yPDP_Message)(OUTSIGNAL_DATA_PTR))-
>Param2.LocalPId), 
          &NrOfReadChars);
        xAss_SDL_Charstring(
          &((yPDP_Message)(OUTSIGNAL_DATA_PTR))->Param1,
          (Instr+NrOfReadChars+2),XASS);
        SDL_Output(yOutputSignal, (xIdNode *)NIL);
      } 
      else if ( SignalName == 'T' ) {
        /* SDL-signal Terminate */
        yOutputSignal = xGetSignal(Terminate, xNotDefPId, 
xEnv);
        sscanf(
          Instr+1, 
          "%d %x", 
          &(((yPDP_Terminate)(OUTSIGNAL_DATA_PTR))-
>Param1.GlobalNodeNr), 
          &(((yPDP_Terminate)(OUTSIGNAL_DATA_PTR))-
>Param1.LocalPId));
        SDL_Output(yOutputSignal, (xIdNode*)0);
      }  
      xFree((void**)&Instr); 
    }  
  }
}      

/*---+-------------------------------------------------------
     xOutEnv  extern
-----------------------------------------------------------*/
#ifndef XNOPROTO
 void
xOutEnv( xSignalNode  *S ) 
#else
 void
xOutEnv( S )
  xSignalNode  *S; 
#endif
{ 
  char   Outstr[150];

  /* SDL-signal Message */
  if ( (*S)->NameNode == Message ) {
    sprintf(Outstr,
            "M %d %x %.*s", 
            ((yPDP_Message)((*S)))->Param2.GlobalNodeNr,
            ((yPDP_Message)((*S)))->Param2.LocalPId,
            strlen(((yPDP_Message)((*S)))->Param1), 
            ((yPDP_Message)((*S)))->Param1);
    write(Out_Socket, Outstr, strlen(Outstr)+1);
    xReleaseSignal(S); 
    return;
  }
  /* SDL-signal Terminate */
  if ( (*S)->NameNode == Terminate) {
    sprintf(Outstr,
            "T %d %x",
            ((yPDP_Terminate)((*S)))->Param1.GlobalNodeNr,
            ((yPDP_Terminate)((*S)))->Param1.LocalPId);
    write(Out_Socket, Outstr, strlen(Outstr)+1);
    xReleaseSignal(S); 
    return;
  }
  /* SDL-signal Display */
  if ( (*S)->NameNode == Display ) {
    sprintf(Outstr, "\ndisplay ->%.*s", 
           strlen(((yPDP_Display)((*S)))->Param1), 
           ((yPDP_Display)((*S)))->Param1+1);
    PRINTF(Outstr);
    PRINTF("\nphone -> ");         
    xReleaseSignal(S); 
    return;
  }    
}
#endif

http://www.ibm.com/rational
Contents Index Previous Next