Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

 

""" 

@file ion/core/managedconnections/tcp.py 

@author David Stuebe 

@author Matt Rodriguez 

@brief base classes for objects with a tcp connection using a life cycle 

""" 

 

from twisted.internet import defer 

from twisted.internet import reactor 

from twisted.python import failure 

 

import ion.util.ionlog 

log = ion.util.ionlog.getLogger(__name__) 

 

from ion.util.state_object import BasicLifecycleObject 

 

 

 

class TCPConnection(BasicLifecycleObject): 

 

    def __init__(self, host, port, factory, timeout=30, bindAddress=None): 

        BasicLifecycleObject.__init__(self) 

        self._host = host 

        self._port = port 

        self._factory = factory 

        self._timeout = timeout 

        self._bindAddress=bindAddress 

 

        self._connector = None 

 

    def on_initialize(self, *args, **kwargs): 

        log.info('on_initialize') 

 

    def on_activate(self, *args, **kwargs): 

        self._connector = reactor.connectTCP(self._host, self._port, self._factory, self._timeout, self._bindAddress) 

        log.info('on_activate: connected TCP') 

 

    def on_deactivate(self, *args, **kwargs): 

        self._connector.disconnect() 

        log.info('on_deactivate: disconnected TCP') 

 

    def on_terminate(self, *args, **kwargs): 

        log.info('on_terminate: disconnected TCP') 

        self._connector.disconnect() 

        log.info('on_terminate: disconnected TCP') 

 

    def on_error(self, *args, **kwargs): 

        self._connector.disconnect() 

        log.info('on_error') 

 

class TCPListen(BasicLifecycleObject): 

 

    def __init__(self, port, factory, backlog=50, interface=''): 

        BasicLifecycleObject.__init__(self) 

        self._port = port 

        self._factory = factory 

        self._backlog = backlog 

        self._interface=interface 

 

        self._listener = None 

 

    def on_initialize(self, *args, **kwargs): 

        log.info('on_initialize') 

 

    def on_activate(self, *args, **kwargs): 

        self._listener = reactor.listenTCP(self._port, self._factory, self._backlog, self._interface) 

        log.info('on_activate: Listening TCP') 

 

    def on_deactivate(self, *args, **kwargs): 

        self._listener.stopListening() 

        log.info('on_deactivate: disconnected TCP') 

 

    def on_terminate(self, *args, **kwargs): 

        self._listener.stopListening() 

        log.info('on_terminate: disconnected TCP') 

 

    def on_error(self, *args, **kwargs): 

        log.info('on_error')