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

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

#!/usr/bin/env python 

 

""" 

@file ion/core/process/cprocess.py 

@author Michael Meisinger 

@brief base classes for internal processes within a capability container 

""" 

 

from twisted.internet import defer 

from zope.interface import implements, Interface 

 

import ion.util.ionlog 

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

 

from ion.core import ioninit 

from ion.core.id import Id 

import ion.util.procutils as pu 

from ion.util.state_object import BasicLifecycleObject 

 

class IInvocation(Interface): 

    pass 

 

class Invocation(object): 

    """ 

    Container object for parameters of events/messages passed to internal 

    capability container processes 

    """ 

    implements(IInvocation) 

 

    # Event inbound processing path 

    PATH_IN = 'in' 

 

    # Event outbound processing path 

    PATH_OUT = 'out' 

 

    # Event processing path 

    PATH_ANY = 'any' 

 

    # Event processing should proceed 

    STATUS_PROCESS = 'process' 

 

    # Event processing is completed. 

    STATUS_DONE = 'done' 

 

    # Event processing should stop and event dropped with no action 

    STATUS_DROP = 'drop' 

 

    # Event processing should proceed with lower priority process, if any 

    STATUS_REJECT = 'reject' 

 

    # An error has occurred and event processing should stop 

    STATUS_ERROR = 'error' 

 

    # Malformed message 

    CODE_BAD_REQUEST = 400 

 

    # Authorization error 

    CODE_UNAUTHORIZED = 401 

 

    def __init__(self, **kwargs): 

        """ 

        @param path A path designator str, e.g. a constant or other 

        @param message the message envelope 

        @param content the message content 

        @param status the processing status 

        @param route None or a str indicating subsequent routing 

        """ 

        self.args = kwargs 

        self.path = str(kwargs.get('path', Invocation.PATH_ANY)) 

        self.message = kwargs.get('message', None) 

        self.content = kwargs.get('content', None) 

        self.process = kwargs.get('process', None) 

        self.status = kwargs.get('status', Invocation.STATUS_PROCESS) 

        self.route = str(kwargs.get('route', "")) 

        self.workbench = kwargs.get('workbench',None) 

        self.note = None 

        self.code = None 

 

    def drop(self, note=None, code=None): 

        self.note = note 

        self.code = code 

        self.status = Invocation.STATUS_DROP 

 

    def done(self, note=None): 

        self.note = note 

        self.status = Invocation.STATUS_DONE 

 

    def error(self, note=None): 

        self.note = note 

        self.status = Invocation.STATUS_ERROR 

 

    def proceed(self, route=""): 

        self.status = Invocation.STATUS_PROCESS 

        self.route = str(route) 

 

class IContainerProcess(Interface): 

    """ 

    Interface for all capability container internal processes 

    """ 

    def process(invocation): 

        """ 

        @param invocation container object for parameters 

        @retval invocation instance, may be modified 

        """ 

 

class ContainerProcess(BasicLifecycleObject): 

    """ 

    Base class for capability container internal processes. 

    """ 

    implements(IContainerProcess) 

 

    def __init__(self, name): 

        BasicLifecycleObject.__init__(self) 

        self.name = name 

 

    # Life cycle 

 

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

        """ 

        """ 

        return defer.succeed(None) 

 

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

        """ 

        @retval Deferred 

        """ 

        return defer.succeed(None) 

 

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

        """ 

        @retval Deferred 

        """ 

        return defer.succeed(None) 

 

    def on_error(self, cause= None, *args, **kwargs): 

        if cause: 

            log.error("Process error: %s" % cause) 

            pass 

        else: 

            raise RuntimeError("Illegal container process state change") 

 

    # Interface API 

 

    def process(self, invocation): 

        pass 

 

    # Helpers