-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.py
More file actions
91 lines (73 loc) · 2.24 KB
/
connection.py
File metadata and controls
91 lines (73 loc) · 2.24 KB
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
from conn_models import *
import os
import subprocess as sub
OVERRIDE_PING = True
class ConnectionException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class Connection:
'Class to define a WAN connection'
empCount = 0
def __init__(self, interfaceName, wanName,wanPriority,wanPingIP):
self.wanName = wanName
self.interfaceName = interfaceName
self.wanPriority = wanPriority
self.wanPingIP = wanPingIP
self.status = self.fetchStatus(interfaceName) #object
def fetchStatus(self,interfaceName):
" Returns up count, down count, state"
try:
settings = wanStatus.get(wanStatus.interfaceName == interfaceName)
except:
print "Cannot fetch settings from database for %s" % interfaceName
else:
return settings
def ping(self):
"""
pings through a specific interface to check status
"""
FNULL = open(os.devnull,'w')
result = not sub.call(['ping', '-q', '-c 1', '-W 1','-I'+self.interfaceName, self.wanPingIP],stdout=FNULL,stderr=sub.STDOUT)
return result
def changeState(self):
"""
toggles state of interface
"""
self.status.state = not self.status.state
def increaseUpCount(self):
"""
increase up counter by one
"""
self.status.upCount += 1
def increaseDownCount(self):
"""
increase down counter by one
"""
self.status.downCount += 1
def resetUpCount(self):
"""
resets up counter for the interface
"""
self.status.upCount = 0
def resetDownCount(self):
"""
resets down counter for the interface
"""
self.status.downCount = 0
def resetAllCount(self):
"""
resets down counter for the interface
"""
self.resetDownCount()
self.resetUpCount()
def saveStatus(self,currentStatus):
"""
Saves settings to database
"""
status = wanStatus.get(wanStatus.interfaceName == self.interfaceName)
status = currentStatus
status.save()
#q = wanStatus.update(status).where(wanStatus.interfaceName == interfaceName)
#q.execute()