utils/annotsroll-client.py
author ymh <ymh.work@gmail.com>
Fri, 17 Apr 2015 15:03:56 +0200
changeset 164 a5d4fc661682
parent 90 111350ddb81d
permissions -rw-r--r--
Backed out changeset 890d387040cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
90
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     1
###############################################################################
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     2
# #
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     3
# #  Copyright (C) 2011-2014 Tavendo GmbH
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     4
# #
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     5
# #  Licensed under the Apache License, Version 2.0 (the "License");
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     6
# #  you may not use this file except in compliance with the License.
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     7
# #  You may obtain a copy of the License at
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     8
# #
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
     9
# #      http://www.apache.org/licenses/LICENSE-2.0
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    10
# #
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    11
# #  Unless required by applicable law or agreed to in writing, software
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    12
# #  distributed under the License is distributed on an "AS IS" BASIS,
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    13
# #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    14
# #  See the License for the specific language governing permissions and
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    15
# #  limitations under the License.
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    16
# #
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    17
###############################################################################
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    18
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    19
import sys
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    20
import csv
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    21
import time
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    22
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    23
from twisted.python import log
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    24
from twisted.internet import reactor
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    25
import argparse
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    26
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    27
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    28
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    29
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    30
class MyClientProtocol(WebSocketClientProtocol):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    31
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    32
    #def __init__(self, rows):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    33
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    34
    def onConnect(self, response):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    35
        print("Server connected: {0}".format(response.peer))
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    36
        reactor.callLater(0, self.send_messages)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    37
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    38
    def onOpen(self):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    39
        print("WebSocket connection open.")
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    40
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    41
    def send_messages(self):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    42
        with open("annotsroll_sample_formated.txt", 'rU') as datafile:
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    43
            for row in list(datafile):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    44
                self.sendMessage(row, isBinary = False)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    45
                time.sleep(0.1)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    46
            
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    47
        #time.sleep(0.1)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    48
        #self.sendMessage('{"user":"Julien"}'.encode('utf8'))
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    49
        print("Goodbye")
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    50
        self.factory.reactor.callLater(0.1, reactor.stop)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    51
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    52
    def onClose(self, wasClean, code, reason):
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    53
        print("WebSocket connection closed: {0}".format(reason))
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    54
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    55
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    56
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    57
if __name__ == '__main__':
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    58
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    59
    log.startLogging(sys.stdout)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    60
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    61
    parser = argparse.ArgumentParser(description='Simulate an (osc) pianoroll client.')
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    62
    #parser.add_argument('datafile', metavar='DATAFILE', help='The file containing the pianoroll data (CSV).')
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    63
    parser.add_argument('-e', '--event', dest='event', metavar='EVENT', required=True, help='the event code.')
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    64
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    65
    args = parser.parse_args()
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    66
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    67
    #with open(args.datafile, 'rU') as datafile:
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    68
    #    reader = csv.reader(datafile, delimiter=' ')
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    69
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    70
    factory = WebSocketClientFactory("ws://127.0.0.1:8090/annot?event=%s" % args.event, debug=True)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    71
    factory.protocol = MyClientProtocol
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    72
    
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    73
    reactor.connectTCP("127.0.0.1", 8090, factory)
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    74
    reactor.run()
111350ddb81d Add client for annotsroll and add sample file
rougeronj
parents:
diff changeset
    75