clientslistener.py 5.36 KB
Newer Older
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
#!/usr/bin/python
#/******************************************************************************
# *                   clientslistener.py
# *
# * This file is part of the Castor project.
# * See http://castor.web.cern.ch/castor
# *
# * Copyright (C) 2003  CERN
# * 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.
# *
# * the clients listener thread of the disk server manager daemon of CASTOR
# *
# * @author Castor Dev team, castor-dev@cern.ch
# *****************************************************************************/

"""clients listener thread of the disk server manager daemon of CASTOR."""

import threading, socket, select, time, random
29
import dlf
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from diskmanagerdlf import msgs


class MoverSocket(object):
  '''A little container describing an open socket for a mover'''
  def __init__(self, qTransfer, callback, socket):
    self.qTransfer = qTransfer
    self.callback = callback
    self.socket = socket
    self.expirationTime = time.time() + qTransfer.transfer.getTimeout()


class ClientsListenerThread(threading.Thread):
  '''the clients listener thread.
  This thread is responsible for listening to clients callbacks, so that movers
45
  can be executed in inetd mode only when the client has already connected.
46
47
48
49
50
51
52
53
54
55
56
57
  During the listening time, the transfer slot is already reserved (cf. activitycontrol),
  so that when the client arrives, the transfer starts immediately.
  '''

  def __init__(self, runningTransfers, config):
    '''constructor'''
    super(ClientsListenerThread, self).__init__(name='ClientsListener')
    self.alive = True
    self.runningTransfers = runningTransfers
    self.config = config
    # an fd -> MoverSocket dictionary for keeping track of the outstanding movers
    self.outstandingMovers = {}
58
    # a multi-poll structure to look for incoming connections from clients
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
    self.clientsPoll = select.poll()
    # start the thread
    self.setDaemon(True)
    self.start()

  def createSocketForMover(self, qTransfer, callback):
    '''
    add a client to the set of polled connections; callback is a callable of type
    lambda(transfer) that is called after a successful connection from the client.
    '''
    # get the port range for the mover of this transfer
    lowPort, highPort = qTransfer.transfer.getPortRange()
    port = random.randrange(lowPort, highPort)
    while True:
      try:
        # attempt to bind and listen
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind((socket.gethostname(), port))
        sock.listen(0)
        break
      except Exception:
        # port is already taken, go to next one
        port += 1
        if port > highPort:
          port = lowPort
Giuseppe Lo Presti's avatar
Giuseppe Lo Presti committed
84
          time.sleep(.1)
85
86
87
88
89
90
91
92
93
94
        continue
    # add the socket to our map of outstanding movers
    self.outstandingMovers[sock.fileno()] = MoverSocket(qTransfer, callback, sock)
    # and add the fd to the multipoll structure
    self.clientsPoll.register(sock, select.POLLIN|select.POLLPRI)
    # return the mover port for the client
    return port

  def run(self):
    '''main method, containing the infinite poll listening loop'''
Giuseppe Lo Presti's avatar
Giuseppe Lo Presti committed
95
    nextCheck = 0
96
97
98
    while self.alive:
      try:
        fds = self.clientsPoll.poll(1000)
Giuseppe Lo Presti's avatar
Giuseppe Lo Presti committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        for (fd, event_unused) in fds:
          # a client connected: drop this fd from our poll record
          self.clientsPoll.unregister(fd)
          # get the corresponding container
          # in case we don't find it, i.e. we got a client for a mover
          # that got dropped, raise error and go to next one
          moverSock = self.outstandingMovers[fd]
          del self.outstandingMovers[fd]
          # accept this incoming connection
          clientSock, addr_unused = moverSock.socket.accept()
          # keep the fd for the mover (inetd-like mode)
          moverSock.qTransfer.transfer.moverFd = clientSock.fileno()
          # now we are ready to start the mover
          moverSock.callback(moverSock.qTransfer.transfer)
        # at least once per second, check all outstanding movers for their timeouts
        if time.time() > nextCheck:
          for moverSock in self.outstandingMovers.values():
            if moverSock.expirationTime < time.time():
              # the client for this mover did not come on time, drop it from our structures.
              # The transfer will be failed by the manager thread polling all running transfers
              self.clientsPoll.unregister(moverSock.socket.fileno())
              del self.outstandingMovers[moverSock.socket.fileno()]
          nextCheck = time.time()+1
122
123
124
125
126
127
128
      except Exception, e:
        # "Caught exception in ClientsListener thread" message
        dlf.writeerr(msgs.CLIENTSLISTENEREXCEPTION, Type=str(e.__class__), Message=str(e))

  def stop(self):
    '''stops processing in this thread'''
    self.alive = False