File: client.c

package info (click to toggle)
xt-toolbuslib 0.5.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 516 kB
  • ctags: 831
  • sloc: ansic: 5,255; sh: 361; makefile: 89
file content (88 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*

   ToolBus -- The ToolBus Application Architecture
   Copyright (C) 1998-2000  Stichting Mathematisch Centrum, Amsterdam, 
   The  Netherlands.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/
#include "toolbus.h" 
#include "terms.h"
#include "tools.h"
#include "utils.h"

#include <sys/socket.h>
#include "sockets.h"

/* --- CLIENT SIDE CONNECT -------------------- */

/* client side mkports --
   1. connect to server
   2. get tool-id from server
   */

/*{{{  int mkports (TBbool local_ports, char *tool, char *tb_host, int *tid, int *tin, int *tout) */

int mkports (TBbool local_ports, char *tool, char *tb_host, int *tid, int *tin, int *tout)
{
  char buf[TB_MAX_HANDSHAKE];

  int sock = TB_ERROR;
  int get_tid = -1;
  *tin = *tout = TB_ERROR;

  /* TBmsg("mkports(%s,%d)\n", tb_host, *tid); */
  /* 1. connect to well known socket */
  if(streq(tb_host, this_host) || local_ports)
    sock = connectWellKnownSocket(NULL, WellKnownSocketPort);
  else
    sock = connectWellKnownSocket(tb_host, WellKnownSocketPort);

  if (sock < 0)  return TB_ERROR;

  /* client coming from "this_host",  open connection */
  sprintf(buf, "%s %s %d", tool, this_host, *tid);

  if(write(sock, buf, TB_MAX_HANDSHAKE) < 0){
    err_sys_warn("mkports -- can't write");
    close(sock);
    return TB_ERROR;
  }

  /* 2. get tool id from server */
  get_tid = getInt(sock, tool);

  /* sanity check */
  if(get_tid < 0){
    err_fatal("no port assigned by ToolBus");
    close(sock);
    return TB_ERROR;
  }
  if(*tid < 0)
    *tid = get_tid;
  else
    if(*tid != get_tid){
      err_warn("mkports -- tool id out of phase, giving up!"); 
      close(sock);
      return TB_ERROR;
    }

  *tout = sock;
  *tin  = sock;	
  return TB_OK;
}

/*}}}  */