- Timestamp:
- Jul 7, 2008 8:14:41 PM (16 years ago)
- Location:
- pjproject/trunk/pjsip-apps/src/test-pjsua
- Files:
-
- 13 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/test-pjsua/inc_sip.py
r2084 r2110 50 50 #return re.split("[;& ]", s) 51 51 52 def get_header(msg, hname): 53 headers = msg.splitlines() 54 for hdr in headers: 55 hfields = hdr.split(": ", 2) 56 if hfields[0]==hname: 57 return hfields[1] 58 return None 52 59 53 60 class Dialog: … … 66 73 trace_enabled = True 67 74 last_request = "" 68 def __init__(self, dst_addr, dst_port=5060, tcp=False, trace=True ):75 def __init__(self, dst_addr, dst_port=5060, tcp=False, trace=True, local_port=0): 69 76 self.dst_addr = dst_addr 70 77 self.dst_port = dst_port … … 76 83 else: 77 84 self.sock = socket(AF_INET, SOCK_DGRAM) 78 self.sock.bind(("127.0.0.1", 0))85 self.sock.bind(("127.0.0.1", local_port)) 79 86 80 87 self.local_ip, self.local_port = self.sock.getsockname() … … 114 121 return msg 115 122 123 def create_response(self, request, code, reason, to_tag=""): 124 response = "SIP/2.0 " + str(code) + " " + reason + "\r\n" 125 lines = request.splitlines() 126 for line in lines: 127 hdr = line.split(":", 1)[0] 128 if hdr in ["Via", "From", "To", "CSeq", "Call-ID"]: 129 if hdr=="To" and to_tag!="": 130 line = line + ";tag=" + to_tag 131 elif hdr=="Via": 132 line = line + ";received=127.0.0.1" 133 response = response + line + "\r\n" 134 return response 135 116 136 def create_invite(self, sdp, extra_headers=""): 117 137 self.inv_branch = str(random.random()) … … 124 144 return self.create_req("BYE", "", extra_headers) 125 145 126 def send_msg(self, msg ):146 def send_msg(self, msg, dst_addr=None): 127 147 if (is_request(msg)): 128 148 self.last_request = msg.split(" ", 1)[0] 129 self.trace("============== TX MSG ============= \n" + msg) 130 self.sock.sendto(msg, 0, (self.dst_addr, self.dst_port)) 131 132 def wait_msg(self, timeout): 149 if not dst_addr: 150 dst_addr = (self.dst_addr, self.dst_port) 151 self.trace("============== TX MSG to " + str(dst_addr) + " ============= \n" + msg) 152 self.sock.sendto(msg, 0, dst_addr) 153 154 def wait_msg_from(self, timeout): 133 155 endtime = time.time() + timeout 134 156 msg = "" 157 src_addr = None 135 158 while time.time() < endtime: 136 159 readset = select([self.sock], [], [], timeout) … … 144 167 continue 145 168 try: 146 msg = self.sock.recv(2048)169 msg, src_addr = self.sock.recvfrom(2048) 147 170 except: 148 171 print "recv() exception: ", sys.exc_info()[0] … … 150 173 151 174 if msg=="": 152 return "" 175 return "", None 153 176 if self.last_request=="INVITE" and self.rem_tag=="": 154 177 self.rem_tag = get_tag(msg, "To") … … 157 180 self.rem_tag = ";tag=" + self.rem_tag 158 181 self.trace("=== rem_tag:" + self.rem_tag) 159 self.trace("=========== RX MSG ===========\n" + msg)160 return msg182 self.trace("=========== RX MSG from " + str(src_addr) + " ===========\n" + msg) 183 return (msg, src_addr) 161 184 185 def wait_msg(self, timeout): 186 return self.wait_msg_from(timeout)[0] 187 162 188 # Send request and wait for final response 163 189 def send_request_wait(self, msg, timeout): … … 237 263 self.inst_param.enable_buffer = enable_buffer 238 264 265 266 class RecvfromTransaction: 267 # The test title for this transaction 268 title = "" 269 # Optinal list of pjsua command and optional expect patterns 270 # to be invoked to make pjsua send a request 271 # Sample: 272 # (to make call and wait for INVITE to be sent) 273 # cmds = [ ["m"], ["sip:127.0.0.1", "INVITE sip:"] ] 274 cmds = [] 275 # Check if the CSeq must be greater than last Cseq? 276 check_cseq = True 277 # List of RE patterns that must exists in incoming request 278 include = [] 279 # List of RE patterns that MUST NOT exist in incoming request 280 exclude = [] 281 # Response code to send 282 resp_code = 0 283 # Additional list of headers to be sent on the response 284 # Note: no need to add CRLF on the header 285 resp_hdr = [] 286 # Message body. This should include the Content-Type header too. 287 # Sample: 288 # body = """Content-Type: application/sdp\r\n 289 # \r\n 290 # v=0\r\n 291 # ... 292 # """ 293 body = None 294 # Pattern to be expected on pjsua when receiving the response 295 expect = "" 296 297 def __init__(self, title, resp_code, check_cseq=True, 298 include=[], exclude=[], cmds=[], resp_hdr=[], resp_body=None, expect=""): 299 self.title = title 300 self.cmds = cmds 301 self.include = include 302 self.exclude = exclude 303 self.resp_code = resp_code 304 self.resp_hdr = resp_hdr 305 self.resp_body = resp_body 306 self.expect = expect 307 308 309 class RecvfromCfg: 310 # Test name 311 name = "" 312 # pjsua InstanceParam 313 inst_param = None 314 # List of RecvfromTransaction 315 transaction = None 316 # Use TCP? 317 tcp = False 318 319 # Note: 320 # Any "$PORT" string in the pjsua_args will be replaced 321 # by server port 322 def __init__(self, name, pjsua_args, transaction, tcp=False): 323 self.name = name 324 self.inst_param = cfg.InstanceParam("pjsua", pjsua_args) 325 self.transaction = transaction 326 self.tcp=tcp 327 328 329 330
Note: See TracChangeset
for help on using the changeset viewer.