[4] | 1 | ############################################################################ |
---|
| 2 | # |
---|
| 3 | # DIM - A Direct Interaction Manager for SAGE |
---|
| 4 | # Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
| 5 | # University of Illinois at Chicago |
---|
| 6 | # |
---|
| 7 | # All rights reserved. |
---|
| 8 | # |
---|
| 9 | # Redistribution and use in source and binary forms, with or without |
---|
| 10 | # modification, are permitted provided that the following conditions are met: |
---|
| 11 | # |
---|
| 12 | # * Redistributions of source code must retain the above copyright |
---|
| 13 | # notice, this list of conditions and the following disclaimer. |
---|
| 14 | # * Redistributions in binary form must reproduce the above |
---|
| 15 | # copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | # in the documentation and/or other materials provided with the distribution. |
---|
| 17 | # * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | # the names of its contributors may be used to endorse or promote |
---|
| 19 | # products derived from this software without specific prior written permission. |
---|
| 20 | # |
---|
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | # |
---|
| 33 | # Direct questions, comments etc about SAGE UI to www.evl.uic.edu/cavern/forum |
---|
| 34 | # |
---|
| 35 | # Author: Ratko Jagodic |
---|
| 36 | # |
---|
| 37 | ############################################################################ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | import socket, time |
---|
| 43 | from threading import Thread |
---|
| 44 | import traceback as tb |
---|
| 45 | import signal |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | # some globals |
---|
| 49 | thisMachine = socket.gethostbyname(socket.gethostname()) |
---|
| 50 | TIMEOUT_INTERVAL = 3 # seconds |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | class ManagerConnection: |
---|
| 56 | """ This should be imported by the hwcapture code and |
---|
| 57 | used to send the data to the device manager |
---|
| 58 | """ |
---|
| 59 | |
---|
| 60 | def __init__(self, host, port=20005): |
---|
| 61 | #self.connections = {} # key=host, value=OneConnection object |
---|
| 62 | self.conn = OneConnection(host, port, doReconnect=True) |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | ## def newConnection(self, host, port): |
---|
| 66 | ## # make the initial connection to the manager we specify |
---|
| 67 | |
---|
| 68 | ## conn = OneConnection(host, port) |
---|
| 69 | ## if host in self.connections: |
---|
| 70 | ## self.connections[host].disconnect() |
---|
| 71 | ## del self.connections[host] |
---|
| 72 | ## self.connections[host] = conn |
---|
| 73 | ## conn.start() |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | def quit(self): |
---|
| 78 | #print "quitting" |
---|
| 79 | self.conn.quit() |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | def sendMessage(self, deviceName, deviceType, data): |
---|
| 83 | """ - deviceName can be anything but it needs to be unique to this machine |
---|
| 84 | (for example: mouse1) |
---|
| 85 | """ |
---|
| 86 | deviceId = thisMachine+":"+str(deviceName) |
---|
| 87 | msg = deviceId + " " + deviceType + " " + data + "\n" |
---|
| 88 | self.conn.sendMessage(msg) |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | class OneConnection: |
---|
| 97 | """ This is just one connection to the device manager """ |
---|
| 98 | |
---|
| 99 | def __init__(self, host, port, doReconnect): |
---|
| 100 | self.doReconnect = doReconnect |
---|
| 101 | self.host = host |
---|
| 102 | self.port = int(port) |
---|
| 103 | self.connected = False |
---|
| 104 | self.doConnecting = True |
---|
| 105 | |
---|
| 106 | # connect thread that's constantly running |
---|
| 107 | self.connThread = Thread(target=self.connectThread) |
---|
| 108 | self.connThread.start() |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | def quit(self): |
---|
| 112 | self.doConnecting = False |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | def connectThread(self): |
---|
| 116 | while self.doConnecting: |
---|
| 117 | |
---|
| 118 | if not self.connected: |
---|
| 119 | # set up the socket |
---|
| 120 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 121 | self.sock.settimeout(TIMEOUT_INTERVAL) |
---|
| 122 | |
---|
| 123 | # try connecting now |
---|
| 124 | try: |
---|
| 125 | self.sock.connect( (self.host, self.port) ) |
---|
| 126 | |
---|
| 127 | # connection successful so start the receiving thread |
---|
| 128 | self.connected = True |
---|
| 129 | print "Connected to: ", self.host |
---|
| 130 | ## self.receiverThread = Thread(target=self.receiver) |
---|
| 131 | ## self.receiverThread.start() |
---|
| 132 | |
---|
| 133 | except socket.error: |
---|
| 134 | if not self.doReconnect: |
---|
| 135 | break # one time connections should only connect once |
---|
| 136 | else: |
---|
| 137 | print "Trying to connect to: ", self.host |
---|
| 138 | time.sleep(4) # try reconnecting every 4 seconds |
---|
| 139 | |
---|
| 140 | elif self.doReconnect: # we are currently connected and we want to reconnect if connection breaks |
---|
| 141 | time.sleep(1) # don't loop like crazy if we are connected already |
---|
| 142 | |
---|
| 143 | else: # we are connected but we don't want to reconnect if connection breaks |
---|
| 144 | break |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | def disconnect(self): |
---|
| 148 | self.sock.close() |
---|
| 149 | self.connected = False |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | def sendMessage(self, msg): |
---|
| 153 | """ try to send the message. Return False if not sent, True otherwise |
---|
| 154 | ******************************* |
---|
| 155 | ******* NOT THREAD SAFE ******* |
---|
| 156 | ******************************* |
---|
| 157 | """ |
---|
| 158 | if not self.connected: return False |
---|
| 159 | |
---|
| 160 | try: |
---|
| 161 | self.sock.sendall(msg) |
---|
| 162 | except socket.error: |
---|
| 163 | #print "unable to send message... socket error" |
---|
| 164 | self.disconnect() |
---|
| 165 | return False |
---|
| 166 | except: |
---|
| 167 | tb.print_exc() |
---|
| 168 | return False |
---|
| 169 | |
---|
| 170 | return True |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | def __sendPing(self): |
---|
| 174 | self.sendMessage(" \n") |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | ## def receiver(self): |
---|
| 178 | ## """ in separate thread """ |
---|
| 179 | |
---|
| 180 | ## while self.connected: |
---|
| 181 | |
---|
| 182 | ## try: |
---|
| 183 | |
---|
| 184 | ## except socket.timeout: |
---|
| 185 | ## self.__sendPing() |
---|
| 186 | ## continue |
---|
| 187 | |
---|
| 188 | |
---|