forked from VoIP-co-uk/sftf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileEventHandler.py
164 lines (151 loc) · 4.91 KB
/
FileEventHandler.py
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
#
# Copyright (C) 2004 SIPfoundry Inc.
# Licensed by SIPfoundry under the GPL license.
#
# Copyright (C) 2004 SIP Forum
# Licensed to SIPfoundry under a Contributor Agreement.
#
#
# This file is part of SIP Forum Test Framework.
#
# SIP Forum Test Framework 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.
#
# SIP Forum Test Framework 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 SIP Forum Test Framework; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id: FileEventHandler.py,v 1.7 2004/03/19 18:37:25 lando Exp $
#
from EventHandler import EventHandler
import Log, Config
import SCException
import SipEvent as SE
def fixCRLF(_input):
pos = _input.find("\n")
while pos != -1:
if _input[pos-1] != "\r":
_input = _input[:pos] + "\r\n" + _input[pos+1:]
pos = pos+1
pos = _input.find("\n", pos+1)
return _input
class FileEventHandler (EventHandler):
"""This class implements the EventHandler class for files. It can read in
SIP messages from files and can write SIP event to a file.
"""
def __init__(self, _filename=None, _fixLF=True, addResource=True):
"""If a filename is given as parameter that file will be opened
for reading. If the second parameter is True single line-feeds will
be replaced by carriage-return line-feed while reading in and writing
to a file.
"""
EventHandler.__init__(self)
self.filename = None
self.fileobj = None
self.fixLF = _fixLF
if _filename is not None:
self.filename = _filename
self.openFile("r")
if addResource:
Config.resources['FEH'][self.filename] = self
def __del__(self):
if self.fileobj is not None:
self.fileobj.close()
def __str__(self):
return '[filename:\'' + str(self.filename) + '\', ' \
'fileobj:\'' + str(self.fileobj) + '\']'
def openFile(self, mode=None):
"""Trys to open the file in the desired mode.
"""
if mode is None:
mode = "r"
Log.logDebug("trying to open:\'" + str(self.filename) + '\', mode:\'' + str(mode) + '\'' , 4)
self.fileobj = file(self.filename, mode)
Log.logDebug("opened successfully", 4)
def close(self):
"""Close the file object if one is still open.
"""
if (not self.fileobj is None):
self.fileobj.close()
self.fileobj = None
def readFileLine(self):
"""Reads and returns a single line from the allready opened file.
"""
s = self.fileobj.readline()
if self.fixLF:
s = fixCRLF(s)
if (s == "."):
return ""
else:
return s
def readBlock(self):
"""Reads and returns a block from the file. A block is either ended
by a line which only consist of a newline or carriage-return newline
combination or it ends when the file end is reached.
"""
empty = 0
block = []
while (not empty):
line = self.readFileLine()
empty = ((line == "\r\n") or (line == "\n") or (line == ""))
if (not empty):
if (line.startswith(' ') or line.startswith('\t')):
block[len(block)-1] = block[len(block)-1] + line
else:
block.append(line)
return block
def readEvent(self):
"""Reads a SIP request from the file and returns it as SIP event.
The source address will be set to 'file' + filename. The destination
address will be None.
"""
if self.fileobj.closed:
if self.filename is not None:
self.openFile("r")
else:
raise SCException("FileEventHandler", "readEvent", "missing filename")
event = SE.SipEvent()
event.srcAddress = ("file", self.fileobj.name, '')
event.headers = self.readBlock()
event.body = self.readBlock()
self.fileobj.close()
return event
def readFile(self, filename):
"""Sets the filename to the given parameter and reads and returns the
resulting SIP event from this file.
"""
if self.fileobj.closed:
self.filename = filename
self.openFile("r")
return self.readEvent()
def writeEvent(self, event):
"""Writes the given SIP event to the file.
The destination address will be 'file' + filename. The source address
will be None.
"""
if self.fileobj.closed:
if self.filename is not None:
self.openFile("w")
else:
raise SCException("FileEventHandler", "writeEvent", "missing filename")
elif self.fileobj.mode != "w":
self.openFile("w")
event.dstAddress = ("file", self.fileobj.name, '')
if self.fixLF:
self.fileobj.writelines(fixCRLF(event.headers))
else:
self.fileobj.writelines(event.headers)
self.fileobj.write("\r\n")
if (len(event.body) > 0):
if self.fixLF:
self.fileobj.writelines(fixCRLF(event.body))
else:
self.fileobj.writelines(event.body)
self.fileobj.close()