File: drawlink.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (226 lines) | stat: -rw-r--r-- 6,875 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (c) 2004 Scott E. Johnston
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 */

/*
 * Implementation of Drawlink class.
 */

#ifdef __llvm__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <DrawServ/ackback-handler.h>
#include <DrawServ/drawlink.h>
#include <DrawServ/drawserv.h>
#include <DrawServ/drawserv-handler.h>
#include <DrawServ/sid.h>
#include <Unidraw/globals.h>
#include <fstream.h>
#include <unistd.h>
#include <iostream>

using std::cout;
using std::cerr;

int DrawLink::_linkcnt = 0;

implementTable(IncomingSidTable,unsigned int,unsigned int)

const char* DrawLink::_state_strings[] =  { "new_link", "one_way", "two_way", "redundant" };

/*****************************************************************************/

DrawLink::DrawLink (const char* hostname, int portnum, int state)
{
  _host = strnew(hostname);
  _althost = nil;
  _port = portnum;
  _ok = false;
  _local_linkid = _linkcnt++;
  _remote_linkid = -1;
  _state = state;

#ifdef HAVE_ACE
  _addr = nil;
  _socket = nil;
  _conn = nil;
#endif

  _comhandler = nil;
  _ackhandler = nil;
  _incomingsidtable = new IncomingSidTable(32);
  _incomingsidtable_size = 0;
}

DrawLink::~DrawLink () 
{
#ifdef HAVE_ACE
    if (_socket->close () == -1)
        ACE_ERROR ((LM_ERROR, "%p\n", "close"));
    delete _conn;
    delete _socket;
    delete _addr;
    delete _host;
    delete _althost;
#endif
    delete _incomingsidtable;
}

int DrawLink::open() {

#if defined(HAVE_ACE) && (__GNUC__>3 || __GNUC__==3 && __GNUC_MINOR__>0)
  _addr = new ACE_INET_Addr(_port, _host);
  _socket = new ACE_SOCK_Stream;
  _conn = new ACE_SOCK_Connector;
  if (_conn->connect (*_socket, *_addr) == -1) {
    ACE_ERROR ((LM_ERROR, "%p\n", "open"));
    return -1;
  } else {
    _socket->enable(ACE_NONBLOCK);

    /* set up handler to monitor backtalk */
    ackhandler(new AckBackHandler);
    ackhandler()->drawlink(this);
    ackhandler()->set_handle(_socket->get_handle());
    if (ComterpHandler::reactor_singleton()->register_handler(ackhandler(), ACE_Event_Handler::READ_MASK|ACE_Event_Handler::TIMER_MASK)==-1)
      fprintf(stderr, "drawserv: error registering ackback handler (handle==%d)\n", _socket->get_handle());

    FILEBUF(obuf, fdopen(_socket->get_handle(), "w"), ios_base::out);
    ostream out(&obuf);
    out << "drawlink(\"";
    char buffer[HOST_NAME_MAX];
    gethostname(buffer, HOST_NAME_MAX);
    unsigned int sid = ((DrawServ*)unidraw)->sessionid();
    void* ptr = nil;
    ((DrawServ*)unidraw)->sessionidtable()->find(ptr, sid);
    SessionId* sessionid = (SessionId*)ptr;
    out << buffer << "\"";
    out << " :port " << ((DrawServ*)unidraw)->comdraw_port();
    out << " :state " << _state+1;
    out << " :rid " << _local_linkid;
    out << " :lid " << _remote_linkid;
    out << " :sid 0x" << std::hex << sid << std::dec;
    if (sessionid) {
      out << " :pid " << sessionid->pid();
      out << " :user \"" << sessionid->username() << "\"";
    }
    out << ")\n";
    out.flush();
    _ok = true;

    /* schedule timer on ackback link, to detect timeout */
    ackhandler()->start_timer();

    return 0;
  }
#else
  fprintf(stderr, "drawserv requires ACE and >= gcc-3.1 for full functionality\n");
  return -1;
#endif
}

int DrawLink::close() {
#ifdef HAVE_ACE
  fprintf(stderr, "Closing link to %s (%s) port # %d (lid=%d, rid=%d)\n", 
	  hostname(), althostname(), portnum(), local_linkid(), remote_linkid());
  if (comhandler()) comhandler()->drawlink(nil);
  if (_socket) {
    if (ackhandler()) {
      if (ackhandler()->get_handle() !=-1)
	if (ComterpHandler::reactor_singleton()->remove_handler(ackhandler(), ACE_Event_Handler::READ_MASK|ACE_Event_Handler::TIMER_MASK)==-1)
	  cerr << "drawserv: error removing ackback handler\n";
      delete ackhandler();
      ackhandler(nil);
    }
    if (_socket->close () == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "close"));
  }
#endif
  return 1;
}

void DrawLink::hostname(const char* host) {
  delete _host;
  _host = nil;
  if (host) _host = strnew(host);
  notify();
}

void DrawLink::althostname(const char* althost) {
  delete _althost;
  _althost = nil;
  if (althost) _althost = strnew(althost);
  notify();
}

int DrawLink::handle() {
#ifdef HAVE_ACE
  if (_socket) 
    return _socket->get_handle();
  else 
#endif
    return -1;
}

unsigned DrawLink::sid_lookup(unsigned int sid) {
  unsigned int local_sid = 0;
  if (_incomingsidtable_size==0) return sid;
  incomingsidtable()->find(local_sid, sid);
  return local_sid ? local_sid : sid;
}

void DrawLink::sid_change(unsigned int& id) {
#ifdef HAVE_ACE
  if (_incomingsidtable_size==0) return;
  id = sid_lookup(id & DrawServ::SessionIdMask) | (id & DrawServ::GraphicIdMask);
#endif
  return;
}

void DrawLink::sid_insert(unsigned int sid, unsigned int alt_sid) {
  if (sid!=alt_sid) {
    _incomingsidtable_size++;
    _incomingsidtable->insert(sid, alt_sid);
  }
}

void DrawLink::dump(FILE* fptr) {
  fprintf(fptr, "Host                            Alt.                            Port    LID  RID  State\n");
  fprintf(fptr, "------------------------------  ------------------------------  ------  ---  ---  -----\n");
  fprintf(fptr, "%-30.30s  %-30.30s  %-6d  %-3d  %-3d  %-3d\n", 
	  hostname(), althostname(), portnum(),
	  local_linkid(), remote_linkid(), state());
  dump_incomingsidtable(fptr);
}

void DrawLink::dump_incomingsidtable(FILE* fptr) {
  IncomingSidTable* table = incomingsidtable();
  IncomingSidTable_Iterator it(*table);
  printf("sid         osid\n");
  printf("----------  ----------\n");
  while(it.more()) {
    unsigned int osid = (unsigned int)it.cur_value();
    unsigned int sid = (unsigned int)it.cur_key();
    fprintf(fptr, "0x%08x  0x%08x\n", sid, osid);
    it.next();
  }
}