source: trunk/src/testing/dim/hwcapture.py @ 4

Revision 4, 5.6 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
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
41import socket, time
42from threading import Thread
43import traceback as tb
44
45
46# some globals
47
48thisMachine = socket.gethostbyname(socket.gethostname())
49TIMEOUT_INTERVAL = 3    # seconds
50
51
52
53
54class ManagerConnection:
55    """ This should be imported by the hwcapture code and
56        used to send the data to the device manager
57    """
58   
59    def __init__(self, host, port=20005):
60        #self.connections = {}   # key=host, value=OneConnection object
61        self.conn = OneConnection(host, port, doReconnect=True)
62       
63
64##     def newConnection(self, host, port):
65##         # make the initial connection to the manager we specify
66       
67##         conn = OneConnection(host, port)
68##         if host in self.connections:
69##             self.connections[host].disconnect()
70##             del self.connections[host]
71##         self.connections[host] = conn
72##         conn.start()
73       
74
75    def sendMessage(self, deviceName, deviceType, data):
76        """ - deviceName can be anything but it needs to be unique to this machine
77            (for example: mouse1)
78        """
79        deviceId = thisMachine+str(deviceName)
80        msg = deviceId + " " + deviceType + " " + data + "\n"
81        self.conn.sendMessage(msg)
82       
83
84
85
86
87
88
89class OneConnection:
90    """ This is just one connection to the device manager """
91   
92    def __init__(self, host, port, doReconnect):
93        self.doReconnect = doReconnect
94        self.host = host
95        self.port = port
96        self.connected = False
97
98        # connect thread that's constantly running
99        self.connThread = Thread(target=self.connectThread)
100        self.connThread.start()
101   
102
103    def connectThread(self):
104        while True:
105           
106            if not self.connected:
107                # set up the socket
108                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
109                self.sock.settimeout(TIMEOUT_INTERVAL)
110
111                # try connecting now
112                try:
113                    self.sock.connect(self.host, self.port)
114
115                    # connection successful so start the receiving thread
116                    self.connected = True
117                    ## self.receiverThread = Thread(target=self.receiver)
118##                     self.receiverThread.start()
119                   
120                except socket.error:
121                    print "failed to connect to: ", (self.host, self.port)
122                    if not self.doReconnect:
123                        break   # one time connections should only connect once
124                    time.sleep(4)  # try reconnecting every 4 seconds
125                                   
126            elif self.doReconnect:    # we are currently connected and we want to reconnect if connection breaks
127                time.sleep(2)  # don't loop like crazy if we are connected already
128
129            else:   # we are connected but we don't want to reconnect if connection breaks
130                break
131           
132
133    def disconnect(self):
134        self.connected = False
135        self.sock.close()
136
137
138    def sendMessage(self, msg):
139        """ try to send the message. Return False if not sent, True otherwise """
140        if not self.connected:  return False
141
142        try:
143            self.sock.sendall(msg)
144        except socket.error:
145            print "unable to send message... socket error"
146            self.disconnect()
147            return False
148        except:
149            tb.print_exc()
150            return False
151           
152        return True
153
154
155    def __sendPing(self):
156        self.sendMessage(" \n")
157           
158
159   ##  def receiver(self):
160##         """ in separate thread """
161       
162##         while self.connected:
163
164##             try:
165               
166##             except socket.timeout:
167##                 self.__sendPing()
168##                 continue
Note: See TracBrowser for help on using the repository browser.