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

# Copyright (C) 2010  Internet Systems Consortium. 

# 

# Permission to use, copy, modify, and distribute this software for any 

# purpose with or without fee is hereby granted, provided that the above 

# copyright notice and this permission notice appear in all copies. 

# 

# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM 

# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL 

# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, 

# 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. 

 

import isc 

 

class WouldBlockForever(Exception): 

    """ 

    This is thrown by the FakeModuleCCSession if it would need 

    to block forever for incoming message. 

    """ 

    pass 

 

# 

# We can probably use a more general version of this 

# 

class FakeModuleCCSession: 

    def __init__(self): 

        self.subscriptions = {} 

        # each entry is of the form [ channel, instance, message ] 

        self.message_queue = [] 

        self._socket = "ok we just need something not-None here atm" 

        # if self.timeout is set to anything other than 0, and 

        # the message_queue is empty when receive is called, throw 

        # a SessionTimeout 

        self._timeout = 0 

        self._closed = False 

 

    def group_subscribe(self, group_name, instance_name = None): 

        if not group_name in self.subscriptions: 

            self.subscriptions[group_name] = [] 

        if instance_name: 

            self.subscriptions[group_name].append(instance_name) 

 

    def group_unsubscribe(self, group_name, instance_name = None): 

 

        # raises SessionError if the session has been already closed. 

50        if self._closed: 

            raise isc.cc.SessionError("Session has been closed.") 

 

        if group_name in self.subscriptions: 

            if instance_name: 

55                if len(self.subscriptions[group_name]) > 1: 

                    del self.subscriptions[group_name][instance_name] 

                else: 

                    del self.subscriptions[group_name] 

            else: 

                del self.subscriptions[group_name] 

 

 

    def has_subscription(self, group_name, instance_name = None): 

69        if group_name in self.subscriptions: 

            if instance_name: 

                return instance_name in self.subscriptions[group_name] 

            else: 

                return True 

        else: 

            return False 

 

    def group_sendmsg(self, msg, channel, target = None): 

        self.message_queue.append([ channel, target, msg ]) 

 

    def group_reply(self, env, msg): 

exit        if 'group' in env: 

            self.message_queue.append([ env['group'], None, msg]) 

 

    def group_recvmsg(self, nonblock=True, seq = None): 

        for qm in self.message_queue: 

            if qm[0] in self.subscriptions and (qm[1] == None or qm[1] in 

                self.subscriptions[qm[0]]): 

                self.message_queue.remove(qm) 

                return qm[2], {'group': qm[0], 'from': qm[1]} 

91        if self._timeout == 0: 

            if nonblock: 

                return None, None 

            else: 

                raise WouldBlockForever( 

                    "Blocking read without timeout and no message ready") 

        else: 

            raise isc.cc.SessionTimeout("Timeout set but no data to " 

                                 "return to group_recvmsg()") 

 

    def get_message(self, channel, target = None): 

99        for qm in self.message_queue: 

95            if qm[0] == channel and qm[1] == target: 

                self.message_queue.remove(qm) 

                return qm[2] 

        return None 

 

    def close(self): 

        # need to pass along somehow that this function has been called, 

        self._socket = None 

        self._closed = True 

 

    def set_timeout(self, timeout): 

        self._timeout = timeout 

 

    def get_timeout(self): 

        return self._timeout