IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 13899


Ignore:
Timestamp:
Jun 19, 2007, 4:37:43 PM (19 years ago)
Author:
eugene
Message:

optionally use named sockets

Location:
trunk/Ohana/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/kapa2/src/CheckPipe.c

    r13479 r13899  
    77
    88// we can supply a port here, with only small changes
    9 void InitPipe () {
    10   InitSocket = KapaServerInit (&Address);
     9void InitPipe (char *namedSocket) {
     10
     11  if (namedSocket == NULL) {
     12    InitSocket = KapaServerInit (&Address);
     13  } else {
     14    sock = KapaWaitNamedSocket (namedSocket);
     15  }
    1116  return;
    1217}
  • trunk/Ohana/src/kapa2/src/Layout.c

    r13479 r13899  
    66  int N;
    77  Section *section;
     8  char *namedSocket;
     9 
     10  if ((N = get_argument (argc, argv, "-socket"))) {
     11    remove_argument (N, &argc, argv);
     12    namedSocket = argv[N];
     13    remove_argument (N, &argc, argv);
     14  }
    815
    9   InitPipe();
     16  // if we specify a named socket, wait until we are contacted
     17  // otherwise, open an INET sock and check occasionally for requests
     18  InitPipe (namedSocket);
    1019
    1120  ACTIVE_CURSOR = FALSE;
  • trunk/Ohana/src/libkapa/include/kapa.h

    r13479 r13899  
    227227int KapaDefineValidIP (char *ipstring);
    228228int KapaClientSocket (char *hostname);
     229int KapaOpenNamedSocket (char *kapa_exec, char *name);
     230int KapaWaitNamedSocket (char *sockpath);
    229231
    230232/* define Kapa names for shared functions */
  • trunk/Ohana/src/libkapa/src/KapaOpen.c

    r13479 r13899  
    243243  return (sock);
    244244}
     245
     246/* start socketed connection (UNIX Socket) */
     247int KapaOpenNamedSocket (char *kapa_exec, char *name) {
     248
     249  int InitSocket, status;
     250  struct sockaddr_un Address;
     251  socklen_t AddressLength;
     252  char temp[128], socket_name[64];
     253  int Ntry, fd;
     254
     255  sprintf (socket_name, "/tmp/Kapa.XXXXXX");
     256  if ((fd = mkstemp (socket_name)) == -1) {
     257    fprintf (stderr, "error starting kapa\n");
     258    return (-1);
     259  }
     260  close (fd);
     261  unlink (socket_name);
     262
     263  strcpy (Address.sun_path, socket_name);
     264  Address.sun_family = AF_UNIX;
     265  InitSocket = socket (AF_UNIX, SOCK_STREAM, 0);
     266  status = bind (InitSocket, (struct sockaddr *) &Address, sizeof (Address));
     267  status = listen (InitSocket, 1);
     268 
     269  if (name == NULL) {
     270    sprintf (temp, "%s -socket %s &", kapa_exec, socket_name);
     271  } else {
     272    sprintf (temp, "%s -socket %s -name %s &", kapa_exec, socket_name, name);
     273  }
     274  system (temp);
     275
     276  AddressLength =  sizeof (Address);
     277  fcntl (InitSocket, F_SETFL, O_NONBLOCK);
     278
     279# define NTRY 500
     280  Ntry = 0;
     281  while (Ntry < NTRY) {
     282    fd = accept (InitSocket, (struct sockaddr *)&Address, &AddressLength);
     283    if (fd != -1) break;
     284    if (errno != EAGAIN) break;
     285    usleep (10000);
     286    Ntry ++;
     287  }
     288
     289  if (fd < 0) return (-1);
     290
     291  // the client uses a BLOCKing socket by default
     292  fcntl (fd, F_SETFL, !O_NONBLOCK);
     293  return (fd);
     294}
     295
     296// wait for the initiating process to connect to the socket
     297int KapaWaitNamedSocket (char *sockpath) {
     298
     299  int sock, status;
     300  struct sockaddr_un Address;
     301
     302  strcpy (Address.sun_path, sockpath);
     303  Address.sun_family = AF_UNIX;
     304  sock = socket (AF_UNIX, SOCK_STREAM, 0);
     305  status = connect (sock, (struct sockaddr *) &Address, sizeof (Address));
     306  if (status < 0) {
     307    fprintf (stderr, "unsuccessful connection: %d\n", status);
     308    exit (0);
     309  }
     310
     311  // the server uses an unblocked socket
     312  fcntl (sock, F_SETFL, O_NONBLOCK); 
     313  unlink (sockpath);
     314  return (sock);
     315}
Note: See TracChangeset for help on using the changeset viewer.