ia64/xen-unstable

view tools/python/xen/xend/server/channel.py @ 6689:7d0fb56b4a91

merge?
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 19:01:31 2005 +0000 (2005-09-07)
parents 4e4aac33809f 652bd7876153
children b2f4823b6ff0 b35215021b32 9af349b055e5 3233e7ecfa9f
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import threading
19 import select
21 import xen.lowlevel.xc; xc = xen.lowlevel.xc.new()
23 from xen.xend.XendLogging import log
25 DEBUG = 0
27 RESPONSE_TIMEOUT = 20.0
29 class EventChannel(dict):
30 """An event channel between domains.
31 """
33 def interdomain(cls, dom1, dom2, port1=0, port2=0):
34 """Create an event channel between domains.
36 @return EventChannel (None on error)
37 """
38 v = xc.evtchn_bind_interdomain(dom1=dom1, dom2=dom2,
39 port1=port1, port2=port2)
40 if v:
41 v = cls(dom1, dom2, v)
42 return v
44 interdomain = classmethod(interdomain)
46 def restoreFromDB(cls, db, dom1, dom2, port1=0, port2=0):
47 """Create an event channel using db info if available.
48 Inverse to saveToDB().
50 @param db db
51 @param dom1
52 @param dom2
53 @param port1
54 @param port2
55 """
56 try:
57 dom1 = int(db['dom1'].getData())
58 except: pass
59 try:
60 dom2 = int(db['dom2'].getData())
61 except: pass
62 try:
63 port1 = int(db['port1'].getData())
64 except: pass
65 try:
66 port2 = int(db['port2'].getData())
67 except: pass
68 evtchn = cls.interdomain(dom1, dom2, port1=port1, port2=port2)
69 return evtchn
71 restoreFromDB = classmethod(restoreFromDB)
73 def __init__(self, dom1, dom2, d):
74 d['dom1'] = dom1
75 d['dom2'] = dom2
76 self.update(d)
77 self.dom1 = dom1
78 self.dom2 = dom2
79 self.port1 = d.get('port1')
80 self.port2 = d.get('port2')
82 def close(self):
83 """Close the event channel.
84 """
85 def evtchn_close(dom, port):
86 try:
87 xc.evtchn_close(dom=dom, port=port)
88 except Exception, ex:
89 pass
91 if DEBUG:
92 print 'EventChannel>close>', self
93 evtchn_close(self.dom1, self.port1)
94 evtchn_close(self.dom2, self.port2)
96 def saveToDB(self, db, save=False):
97 """Save the event channel to the db so it can be restored later,
98 using restoreFromDB() on the class.
100 @param db db
101 """
102 db['dom1'] = str(self.dom1)
103 db['dom2'] = str(self.dom2)
104 db['port1'] = str(self.port1)
105 db['port2'] = str(self.port2)
106 db.saveDB(save=save)
108 def sxpr(self):
109 return ['event-channel',
110 ['dom1', self.dom1 ],
111 ['port1', self.port1 ],
112 ['dom2', self.dom2 ],
113 ['port2', self.port2 ]
114 ]
116 def __repr__(self):
117 return ("<EventChannel dom1:%d:%d dom2:%d:%d>"
118 % (self.dom1, self.port1, self.dom2, self.port2))
120 def eventChannel(dom1, dom2, port1=0, port2=0):
121 """Create an event channel between domains.
123 @return EventChannel (None on error)
124 """
125 return EventChannel.interdomain(dom1, dom2, port1=port1, port2=port2)
127 def eventChannelClose(evtchn):
128 """Close an event channel.
129 """
130 if not evtchn: return
131 evtchn.close()