source: contributed/trunk/dail/dail.py @ 7307

Revision 7306, 5.6 KB checked in by anonymous, 9 years ago (diff)

New Version

Line 
1# ----------------------------------------------------------------------
2#    The dacheAdminInterfaceLib V0.3  26.04.2009
3#   
4#    Copyright (c) 2009 DESY IT (Sven Sternberger)
5#    (All rights reversed)
6#    Based on "getPoolInfos.py"  from DCACHE.ORG (R)
7#
8#    This program is free software; you can redistribute it and/or
9#    modify it under the terms of version 2 of the GNU General Public
10#    License published by the Free Software Foundation.
11#
12#    This program is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with this program; if not, contact Novell, Inc.
19# ----------------------------------------------------------------------
20#
21# This is the first release it is NOT heavily tested.
22# Please don't use this for critical tasks on your
23# real world production system.
24#-----------------------------------------------------------------------
25# 0.2 - Empty results set causes endlless loop
26# 0.3 - bug resolved if you contact different instances
27#
28#-----------------------------------------------------------------------
29
30from org.pcells.services.connection import *
31from dmg.protocols.ssh import *
32from dmg.cells.nucleus import NoRouteToCellException
33from java.lang import System
34from java.io import File
35from java.io import FileNotFoundException
36import sys,time,types,string,threading,signal
37
38DEBUG=False
39
40class queryStore:
41    def __init__(self):
42        self.storeHash={}
43     
44    def save(self,id,obj):
45        if DEBUG: print "Save ID: %d" % id
46        self.storeHash[id]=obj
47
48    def retrieve(self,id):
49        if self.storeHash.has_key(id):
50            return self.storeHash[id]
51        else:
52            return False
53
54    def check(self,id):
55        return self.storeHash.has_key(id)
56     
57
58class ourDomainEventListener( DomainEventListener,DomainConnectionListener) :
59    connectionId=0
60    queryId=0
61    Connection=None
62    activ=False
63   
64    def __init__(self,store):
65        self.myStore=store
66
67    def connectionOpened( self , connection ) :
68        #self.__class__.connectionId+=1
69        #self.__class__.Connection=connection
70        #self.__class__.activ=True
71
72        self.connectionId+=1
73        self.Connection=connection
74        self.activ=True
75       
76    def domainAnswerArrived( self , obj , id ) :
77        if DEBUG: print "Answer id=%d // type=%s" % (id,type(obj))
78
79        self.myStore.save(id,obj)
80                     
81     
82    def _sendObject(self,cellName,cmd):
83        self.__class__.queryId+=1
84        if DEBUG:
85           print "CellName: %s, Command: %s, queryid: %d" % (cellName,cmd,self.__class__.queryId)
86           
87        #self.__class__.Connection.sendObject( cellName, cmd , self, self.__class__.queryId) ;
88        self.Connection.sendObject( cellName, cmd , self, self.__class__.queryId) ;
89       
90        return self.__class__.queryId
91
92
93class queryDcache:
94    def __init__(self,door,port,user,passwd):
95        self.door=door
96        if DEBUG:
97            print "Query DOOR: %s" % (door)
98           
99        self.myStore=queryStore()
100
101        #
102        #  Define Host and portnumber
103        #
104        self.connection=Ssh1DomainConnection(door,port)
105        self.connection.setLoginName(user)
106
107        #
108        # Establish the secret : either password or identity file
109        #
110        if passwd[0] == "/" :
111            self.connection.setPassword("")
112            try:
113                self.connection.setIdentityFile( File(passwd) )
114            except FileNotFoundException, e :
115                sys.stderr.write("Authentication Failed ...%s" % (e.getMessage()))
116                System.exit(1)
117
118               
119        else:
120                self.connection.setPassword(passwd)
121
122        #
123        # Define the ListenerObject for all incoming message
124        #
125        self.MyDomainEventListener=ourDomainEventListener(self.myStore)
126        self.connection.addDomainEventListener( self.MyDomainEventListener )
127
128        #
129        #  start the dcache connection thread
130        #
131        if DEBUG:
132            print "Start Thread for %s" % (door)
133        threading.Thread(target=self._startCellQuery).start()
134        if DEBUG:
135            print "Thread for %s started" % (door)
136
137
138    def query(self,domain,cmd):
139        self.timeOutCounter=0
140        if DEBUG:
141            print "Start Query DOOR: %s, CELL: %s, CMD: %s" % (self.door,domain,cmd)
142       
143        while not self.MyDomainEventListener.activ:
144            time.sleep(1)
145            self.timeOutCounter+=1     
146            if self.timeOutCounter>2:
147                return None
148     
149        if DEBUG:
150            print "DOOR: %s, CELL: %s, CMD: %s" % (self.door,domain,cmd)
151         
152        self.queryId=self.MyDomainEventListener._sendObject(domain,cmd)
153
154        while not self.myStore.check(self.queryId):
155            if DEBUG: print "wait for %d" % (self.queryId)
156            pass
157     
158        if DEBUG:
159            print "Got answer: %s" % (type(self.myStore.retrieve(self.queryId)))
160           
161        return self.myStore.retrieve(self.queryId)               
162               
163           
164    def _startCellQuery(self):
165
166        def timeout_handler(signum, frame):
167                raise SystemExit
168
169        old_handler=signal.signal(signal.SIGALRM, timeout_handler)
170        signal.alarm(2) 
171               
172        try:
173                if DEBUG: print "Start connection ..."
174                self.connection.go()
175        except SshAuthenticationException, e :
176                print "Authentication Failed ...",e.getMessage()
177        finally:
178                signal.signal(signal.SIGALRM, old_handler) 
179                signal.alarm(0)
180
181        System.exit(1)
182
Note: See TracBrowser for help on using the repository browser.