Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • scs/ipaaca
  • ramin.yaghoubzadeh/ipaaca
2 results
Show changes
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
# Forschungsgemeinschaft (DFG) in the context of the German # Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative. # Excellence Initiative.
from __future__ import division, print_function #from __future__ import division, print_function
import itertools import itertools
import os import os
...@@ -43,7 +43,10 @@ import ipaaca ...@@ -43,7 +43,10 @@ import ipaaca
def iu_update_handler(iu, event_type, local): def iu_update_handler(iu, event_type, local):
print(event_type + ': ' + str(iu)) try:
print(event_type + ': ' + str(iu))
except:
print(u"An error occurred printing an IU for an event of type "+event_type)
parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Injector -- Sent ipaaca messages or IUs from the command line') parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Injector -- Sent ipaaca messages or IUs from the command line')
...@@ -83,22 +86,24 @@ parser.add_argument( ...@@ -83,22 +86,24 @@ parser.add_argument(
if __name__ == '__main__': if __name__ == '__main__':
arguments = parser.parse_args() arguments = parser.parse_args()
print('BackEnd is '+str(ipaaca.backend.get_default_backend().name))
ob = ipaaca.OutputBuffer('IpaacaIUInjector') ob = ipaaca.OutputBuffer('IpaacaIUInjector')
ob.register_handler(iu_update_handler) ob.register_handler(iu_update_handler)
iu = ipaaca.Message(arguments.category) if arguments.iu_type == 'Message' else ipaaca.IU(arguments.category) iu = ipaaca.Message(arguments.category) if arguments.iu_type == 'Message' else ipaaca.IU(arguments.category)
if arguments.json_payload: if arguments.json_payload:
# Treat payload values as Python expressions # Treat payload values as Python expressions
iu.payload = {k: eval(v) for (k, v) in itertools.izip_longest(arguments.payload[::2], arguments.payload[1::2])} iu.payload = {k: eval(v) for (k, v) in itertools.zip_longest(arguments.payload[::2], arguments.payload[1::2])}
else: else:
iu.payload = {k: v for (k, v) in itertools.izip_longest(arguments.payload[::2], arguments.payload[1::2])} iu.payload = {k: v for (k, v) in itertools.zip_longest(arguments.payload[::2], arguments.payload[1::2])}
ob.add(iu) ob.add(iu)
print( print(
'Sent {iu_type} with category "{category}" and payload {{'.format(**vars(arguments)), u'Sent {iu_type} with category "{category}" and payload {{'.format(**vars(arguments)),
end='\n' if len(iu.payload) > 0 else '') end='\n' if len(iu.payload) > 0 else '')
for k, v in iu.payload.items(): for k, v in iu.payload.items():
print(" '{key}': {value},".format(key=k, value=v)) print(u" '{key}': {value},".format(key=k, value=v))
print('}.') print(u'}.')
# Wait for updates to the IU # Wait for updates to the IU
try: try:
...@@ -109,8 +114,7 @@ if __name__ == '__main__': ...@@ -109,8 +114,7 @@ if __name__ == '__main__':
time.sleep(0.1) time.sleep(0.1)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
except Exception as e:
if platform.system() == 'Windows': print(u'Exception: '+str(traceback.format_exc()))
os._exit(0) ipaaca.exit(1)
else: ipaaca.exit(0)
sys.exit(0)
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
# Forschungsgemeinschaft (DFG) in the context of the German # Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative. # Excellence Initiative.
from __future__ import division, print_function #from __future__ import division, print_function
import logging import logging
import os import os
...@@ -51,20 +51,23 @@ def event_type_color(typ): ...@@ -51,20 +51,23 @@ def event_type_color(typ):
'MESSAGE': '34;1', 'MESSAGE': '34;1',
'COMMITTED': '35;1', 'COMMITTED': '35;1',
'LINKSUPDATED': '36;1', 'LINKSUPDATED': '36;1',
'RETRACTED': '37;1', 'RETRACTED': '37',
} }
return colors.get(typ, '1') return colors.get(typ, '1')
def highlight_if_color(s, c='1'): def highlight_if_color(s, c='1'):
return s if not arguments.color else '[' + c + 'm' + s +'' return s if arguments.color else '[' + c + 'm' + s +''
def pretty_printed_dict(d): def pretty_printed_dict(d):
s='{\n' s='{\n'
for k, v in d.items(): for k, v in d.items():
v = str(v) if isinstance(v, str) or isinstance(v, str):
v = "'"+str(v)+"'"
else:
v = str(v)
v2 = v if len(v) <= arguments.max_size else v[:arguments.max_size] + '<excess length omitted>' v2 = v if len(v) <= arguments.max_size else v[:arguments.max_size] + '<excess length omitted>'
v2.replace('\\','\\\\').replace('\n', highlight_if_color('\\n')) v2.replace('\\','\\\\').replace('\n', highlight_if_color('\\n'))
s += "\t '%s': '%s',\n" % (highlight_if_color(unicode(k),'1'), unicode(v2)) s += "\t '%s': %s,\n" % (highlight_if_color(str(k),'1'), str(v2))
s+='}' s+='}'
return s return s
...@@ -75,7 +78,8 @@ def pretty_printed_iu_event(iu, event_type, local): ...@@ -75,7 +78,8 @@ def pretty_printed_iu_event(iu, event_type, local):
s += highlight_if_color('%.3f' % t, '1') s += highlight_if_color('%.3f' % t, '1')
s += ' %02d:%02d:%02d' % (lt.tm_hour, lt.tm_min, lt.tm_sec) s += ' %02d:%02d:%02d' % (lt.tm_hour, lt.tm_min, lt.tm_sec)
s += ' ' + highlight_if_color('%-9s' % event_type, event_type_color(event_type)) s += ' ' + highlight_if_color('%-9s' % event_type, event_type_color(event_type))
s += ' category=' + highlight_if_color(iu.category,event_type_color(event_type)) s += ' category=' + highlight_if_color(iu.category,event_type_color(event_type))
s += ' channel=' + iu.buffer._channel
s += ' uid=' + iu.uid s += ' uid=' + iu.uid
s += ' owner=' + iu.owner_name s += ' owner=' + iu.owner_name
if event_type is not 'MESSAGE': if event_type is not 'MESSAGE':
...@@ -83,7 +87,7 @@ def pretty_printed_iu_event(iu, event_type, local): ...@@ -83,7 +87,7 @@ def pretty_printed_iu_event(iu, event_type, local):
s += '\npayload=' + pretty_printed_dict(iu.payload) s += '\npayload=' + pretty_printed_dict(iu.payload)
return s return s
def my_update_handler(iu, event_type, local): def iu_event_handler(iu, event_type, local):
if arguments.regex: if arguments.regex:
for cat in arguments.categories: # actually now regexs, not categories for cat in arguments.categories: # actually now regexs, not categories
if re.match(cat, iu.category): if re.match(cat, iu.category):
...@@ -92,19 +96,14 @@ def my_update_handler(iu, event_type, local): ...@@ -92,19 +96,14 @@ def my_update_handler(iu, event_type, local):
return return
print(pretty_printed_iu_event(iu, event_type, local), end='\n\n') print(pretty_printed_iu_event(iu, event_type, local), end='\n\n')
def exit(code):
ipaaca.exit(code)
#if platform.system() == 'Windows':
# os._exit(code)
#else:
# sys.exit(code)
parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Sniffer -- Selectively listen to IPAACA traffic') parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Sniffer -- Selectively listen to IPAACA traffic')
parser.add_argument(
'-r', '--regex',
action='store_true',
dest='regex',
help='match categories by regular expressions')
parser.add_argument(
'-c', '--color',
action='store_true',
dest='color',
help='colorize output')
parser.add_argument( parser.add_argument(
'--channels', '--channels',
dest='channels', dest='channels',
...@@ -113,12 +112,29 @@ parser.add_argument( ...@@ -113,12 +112,29 @@ parser.add_argument(
nargs='+', nargs='+',
help="set the channels to listen on (otherwise 'default')") help="set the channels to listen on (otherwise 'default')")
parser.add_argument( parser.add_argument(
'--categories', '-c', '--categories',
default=[''], default=[''],
dest='categories', dest='categories',
metavar='CATEGORY', metavar='CATEGORY',
nargs='+', nargs='+',
help='set categories (or regex patterns) to be matched') help='set categories (or regex patterns) to be matched')
parser.add_argument(
'-e', '--event-types',
default=None,
dest='event_types',
metavar='EVENT-TYPE',
nargs='+',
help='set event types ({})'.format(' '.join(ipaaca.iu.IUEventType._choices)))
parser.add_argument(
'-r', '--regex',
action='store_true',
dest='regex',
help='match categories by regular expressions')
parser.add_argument(
'--no-color',
action='store_true',
dest='color',
help='do not colorize output')
parser.add_argument( parser.add_argument(
'--size-limit', '--size-limit',
default=2048, default=2048,
...@@ -130,37 +146,61 @@ parser.add_argument( ...@@ -130,37 +146,61 @@ parser.add_argument(
if __name__ == '__main__': if __name__ == '__main__':
arguments = parser.parse_args() arguments = parser.parse_args()
buffers = {} buffers = {}
backend_name = str(ipaaca.backend.get_default_backend().name)
print('BackEnd is '+backend_name)
universal_listener_category = ''
if backend_name == 'mqtt':
universal_listener_category = '#'
if arguments.categories == ['']: arguments.categories = ['#']
elif backend_name == 'ros':
if arguments.categories == [''] or arguments.regex:
print('ATTENTION: listening to all categories not implemented for ROS back-end!')
print(' (By extension, the same goes for filtering all categories by regex.)')
print(' !! You will receive nothing, please provide fixed category names. !!')
# Create one input buffer for each channel we are listening on
for channel in arguments.channels: for channel in arguments.channels:
buffers[channel] = ipaaca.InputBuffer( buffers[channel] = ipaaca.InputBuffer(
'IpaacaIUSniffer', 'IpaacaIUSniffer',
category_interests=arguments.categories if not arguments.regex else [''], category_interests=arguments.categories if not arguments.regex else [universal_listener_category],
channel=channel, channel=channel,
resend_active=True) resend_active=True)
buffers[channel].register_handler(my_update_handler) # Check whether the specified event_types are valid
if arguments.event_types is not None:
channellist = 's ' if len(arguments.channels) > 1 else ' ' for et in arguments.event_types:
channellist += ', '.join(arguments.channels) if et.upper() not in ipaaca.iu.IUEventType._choices:
print('ERROR: "{et}" is not a valid IPAACA event type.'.format(et=et.upper()))
print('Listening on channel' + channellist + ' for IU events of ', end='') exit(code=1)
if arguments.categories == ['']: buffers[channel].register_handler(
print('any category ...') iu_event_handler,
for_event_types=[et.upper() for et in arguments.event_types] if arguments.event_types is not None else None)
# Tell user what we are doing
print('ipaaca-iu-sniffer listening')
print(' * on channel(s): ' + ', '.join(arguments.channels))
print(' * for event type(s): ', end='')
if arguments.event_types is None:
print('any')
else:
print(', '.join([highlight_if_color(et.upper(), event_type_color(et.upper())) for et in arguments.event_types]))
print(' * for category/ies', end='')
if arguments.categories == [universal_listener_category]:
print(': any')
else: else:
if arguments.regex: if arguments.regex:
print('whose category matches one of the regexes:') print(' that match the regex', end='')
else: print(': ' + ', '.join(arguments.categories))
print('categories:')
print('\t' + ', '.join(arguments.categories)) # Wait for the user to kill the sniffer
print('')
try: try:
while True: while True:
time.sleep(1) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
except Exception as e:
if platform.system() == 'Windows': print(u'Exception: '+str(traceback.format_exc()))
os._exit(0) ipaaca.exit(1)
else: ipaaca.exit(0)
sys.exit(0)
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of IPAACA, the
# "Incremental Processing Architecture
# for Artificial Conversational Agents".
#
# Copyright (c) 2009-2015 Social Cognitive Systems Group
# CITEC, Bielefeld University
#
# http://opensource.cit-ec.de/projects/ipaaca/
# http://purl.org/net/ipaaca
#
# This file may be licensed under the terms of of the
# GNU Lesser General Public License Version 3 (the ``LGPL''),
# or (at your option) any later version.
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the LGPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the LGPL along with this
# program. If not, go to http://www.gnu.org/licenses/lgpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The development of this software was supported by the
# Excellence Cluster EXC 277 Cognitive Interaction Technology.
# The Excellence Cluster EXC 277 is a grant of the Deutsche
# Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative.
from __future__ import division, print_function
import sys
import time
import ipaaca
import ipaaca.util.logger as ipaacalog
def main(log_mode, filename=None):
ipaacalog.logger_send_ipaaca_logs(False)
il = ipaacalog.LoggerComponent(filename, log_mode)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
il.close_logfile()
print('Logging-Component closed by keyboard interrupt.')
if __name__ == '__main__':
import traceback
try:
iap = ipaaca.IpaacaArgumentParser(
'ipaaca-logger')
iap.add_argument(
'-m', '--log-mode', dest='log_mode',
choices=ipaacalog.LOG_MODES,
default='append',
help="set what to do when logfile exists "
"(default: 'append'; 'timestamp' adds timestamp in any case)")
iap.add_argument(
'filename', nargs='?',
metavar='FILE',
help='set name of logfile')
arguments = iap.parse_args()
main(arguments.log_mode, arguments.filename)
except KeyboardInterrupt:
pass
except Exception, e:
print(u'Exception: '+unicode(traceback.format_exc()))
ipaaca.exit(1)
ipaaca.exit(0)
#!/usr/bin/env python
import ipaaca
import time
class PingSender(object):
def __init__(self):
self.pings = []
self.times = []
self.ob = ipaaca.OutputBuffer('IpaacaPing')
self.ob.register_handler(self.iu_event_handler)
def iu_event_handler(self, iu, event_type, local):
recv_t = time.time()
if event_type=='UPDATED':
print('OK')
send_t = float(iu.payload['sender_local_t'])
receiver_recv_t = float(iu.payload['receiver_local_t'])
round_trip_t = recv_t - send_t
locally_estimated_receiver_recv_t = (recv_t + send_t) / 2
estimated_clock_skew = receiver_recv_t - locally_estimated_receiver_recv_t
self.times.append(estimated_clock_skew)
average_clock_difference = sum(self.times) / len(self.times)
self.pings.append(500.0 * round_trip_t)
average_estimated_single_trip = sum(self.pings) / len(self.pings)
print('Received ping reply')
print(' measured round trip time [ms]: %0.3f'%(1000.0 * round_trip_t))
print(' estimated single trip [ms]: %0.3f'%(500.0 * round_trip_t))
print(' averaged single trip [ms]: %0.3f'%(average_estimated_single_trip))
print(' estimated system clock difference [s]: %0.3f'%(average_clock_difference))
def send_ping(self):
t = time.time()
iu = ipaaca.IU('ipaacaPing')
iu.payload['sender_local_t'] = t
self.ob.add(iu)
iap = ipaaca.IpaacaArgumentParser('ipaaca-ping')
arguments = iap.parse_args()
ps = PingSender()
while True:
ps.send_ping()
time.sleep(1)
#!/usr/bin/env python
import ipaaca
import time
def iu_event_handler(iu, event_type, local):
if event_type=='ADDED':
iu.payload['receiver_local_t'] = time.time()
print('Sent IPAACA ping reply')
iap = ipaaca.IpaacaArgumentParser('ipaaca-pong')
arguments = iap.parse_args()
ib = ipaaca.InputBuffer('IpaacaPong', ['ipaacaPing'])
ib.register_handler(iu_event_handler)
while True:
time.sleep(1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of IPAACA, the
# "Incremental Processing Architecture
# for Artificial Conversational Agents".
#
# Copyright (c) 2009-2015 Social Cognitive Systems Group
# CITEC, Bielefeld University
#
# http://opensource.cit-ec.de/projects/ipaaca/
# http://purl.org/net/ipaaca
#
# This file may be licensed under the terms of of the
# GNU Lesser General Public License Version 3 (the ``LGPL''),
# or (at your option) any later version.
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the LGPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the LGPL along with this
# program. If not, go to http://www.gnu.org/licenses/lgpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The development of this software was supported by the
# Excellence Cluster EXC 277 Cognitive Interaction Technology.
# The Excellence Cluster EXC 277 is a grant of the Deutsche
# Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative.
from __future__ import division, print_function
import logging
import os
import platform
import re
import sys
import time
import ipaaca
def event_type_color(typ):
colors = {
'ADDED': '32;1',
'RETRACTED': '31;1',
'UPDATED': '33;1',
'MESSAGE': '34;1',
'COMMITTED': '35;1',
'LINKSUPDATED': '36;1',
'RETRACTED': '37;1',
}
return colors.get(typ, '1')
def highlight_if_color(s, c='1'):
return s if arguments.color else '[' + c + 'm' + s +''
def pretty_printed_dict(d):
s='{\n'
for k, v in d.items():
if isinstance(v, unicode) or isinstance(v, str):
v = "'"+unicode(v)+"'"
else:
v = unicode(v)
v2 = v if len(v) <= arguments.max_size else v[:arguments.max_size] + '<excess length omitted>'
v2.replace('\\','\\\\').replace('\n', highlight_if_color('\\n'))
s += "\t '%s': %s,\n" % (highlight_if_color(unicode(k),'1'), unicode(v2))
s+='}'
return s
def pretty_printed_iu_event(iu, event_type, local):
s = ''
t = time.time()
lt = time.localtime(t)
s += highlight_if_color('%.3f' % t, '1')
s += ' %02d:%02d:%02d' % (lt.tm_hour, lt.tm_min, lt.tm_sec)
s += ' ' + highlight_if_color('%-9s' % event_type, event_type_color(event_type))
s += ' category=' + highlight_if_color(iu.category,event_type_color(event_type))
s += ' channel=' + iu.buffer._channel
s += ' uid=' + iu.uid
s += ' owner=' + iu.owner_name
if event_type is not 'MESSAGE':
s += '\nlinks=' + pretty_printed_dict(iu.get_all_links())
s += '\npayload=' + pretty_printed_dict(iu.payload)
return s
def machine_formatted_iu_event(iu, event_type, local):
return repr({
'time': time.time(),
'uid': iu.uid,
'category': iu.category,
'type': event_type,
'payload': iu.payload,
#'links': iu.links,
}).encode('utf-8')
def iu_event_recorder_handler(logfile, iu, event_type, local):
if arguments.regex:
for cat in arguments.categories: # actually now regexs, not categories
if re.match(cat, iu.category):
break
else:
return
if logfile is None:
# requested log to stdout, only printing the structured data
print(machine_formatted_iu_event(iu, event_type, local)+'\n')
else:
# write structured data to log file
logfile.write(machine_formatted_iu_event(iu, event_type, local)+'\n')
# duplicate human-readable on terminal (unless data being sent there)
print(pretty_printed_iu_event(iu, event_type, local), end='\n\n')
def exit(code):
ipaaca.exit(code)
#if platform.system() == 'Windows':
# os._exit(code)
#else:
# sys.exit(code)
parser = ipaaca.IpaacaArgumentParser(description='Ipaaca IU Session Recorder -- Selectively record [and print] IU streams')
parser.add_argument(
'-o', '--output-file',
dest='output_file',
default=['default.ipaacasession'],
metavar='OUTPUT-FILE',
nargs=1,
help="set the channels to listen on (otherwise 'default')")
parser.add_argument(
'--channels',
dest='channels',
default=['default'],
metavar='CHANNEL',
nargs='+',
help="set the channels to listen on (otherwise 'default')")
parser.add_argument(
'-c', '--categories',
default=[''],
dest='categories',
metavar='CATEGORY',
nargs='+',
help='set categories (or regex patterns) to be matched')
parser.add_argument(
'-e', '--event-types',
default=None,
dest='event_types',
metavar='EVENT-TYPE',
nargs='+',
help='set event types ({})'.format(' '.join(ipaaca.iu.IUEventType._choices)))
parser.add_argument(
'-r', '--regex',
action='store_true',
dest='regex',
help='match categories by regular expressions')
parser.add_argument(
'--no-color',
action='store_true',
dest='color',
help='do not colorize output')
parser.add_argument(
'--size-limit',
default=2048,
dest='max_size',
metavar='LIMIT',
type=int,
help='limit payload display chars (default: 2048)')
if __name__ == '__main__':
arguments = parser.parse_args()
buffers = {}
log_file = None if (arguments.output_file[0]=='-') else open(arguments.output_file[0], 'w')
# Create one input buffer for each channel we are listening on
for channel in arguments.channels:
buffers[channel] = ipaaca.InputBuffer(
'IpaacaIUSniffer',
category_interests=arguments.categories if not arguments.regex else [''],
channel=channel,
resend_active=True)
# Check whether the specified event_types are valid
if arguments.event_types is not None:
for et in arguments.event_types:
if et.upper() not in ipaaca.iu.IUEventType._choices:
print('ERROR: "{et}" is not a valid IPAACA event type.'.format(et=et.upper()))
exit(code=1)
buffers[channel].register_handler(
lambda iu,typ,loc: iu_event_recorder_handler(log_file, iu, typ, loc),
for_event_types=[et.upper() for et in arguments.event_types] if arguments.event_types is not None else None)
# Tell user what we are doing
print('ipaaca-session-record listening')
print(' * writing to file: '+('(stdout)' if log_file is None else arguments.output_file[0]))
print(' * on channel(s): ' + ', '.join(arguments.channels))
print(' * for event type(s): ', end='')
if arguments.event_types is None:
print('any')
else:
print(', '.join([highlight_if_color(et.upper(), event_type_color(et.upper())) for et in arguments.event_types]))
print(' * for category/ies', end='')
if arguments.categories == ['']:
print(': any')
else:
if arguments.regex:
print(' that match the regex', end='')
print(': ' + ', '.join(arguments.categories))
# Wait for the user to kill the sniffer
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
except Exception, e:
print(u'Exception: '+unicode(traceback.format_exc()))
if log_file is not None:
log_file.close()
ipaaca.exit(1)
if log_file is not None:
log_file.close()
ipaaca.exit(0)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of IPAACA, the
# "Incremental Processing Architecture
# for Artificial Conversational Agents".
#
# Copyright (c) 2009-2015 Social Cognitive Systems Group
# CITEC, Bielefeld University
#
# http://opensource.cit-ec.de/projects/ipaaca/
# http://purl.org/net/ipaaca
#
# This file may be licensed under the terms of of the
# GNU Lesser General Public License Version 3 (the ``LGPL''),
# or (at your option) any later version.
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the LGPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the LGPL along with this
# program. If not, go to http://www.gnu.org/licenses/lgpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The development of this software was supported by the
# Excellence Cluster EXC 277 Cognitive Interaction Technology.
# The Excellence Cluster EXC 277 is a grant of the Deutsche
# Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative.
from __future__ import division, print_function
import itertools
import os
import platform
import sys
import time
import traceback
import ipaaca
def iu_update_handler(iu, event_type, local):
try:
print(event_type + ': ' + unicode(iu))
except:
print(u"An error occurred printing an IU for an event of type "+event_type)
parser = ipaaca.IpaacaArgumentParser(description='Ipaaca Session Replay -- Replay IU event logs from the command line')
parser.add_argument(
'-i', '--input-file',
default=['default.ipaacasession'],
dest='input_file',
metavar='INPUT-FILE',
nargs=1,
help='file from which to read IU data for replay')
parser.add_argument(
'-k', '--keep-alive',
default=1.0,
dest='keep_alive',
metavar='SECONDS',
type=float,
help='set time in seconds to wait for final remote IU updates (default: 1.0)')
parser.add_argument(
'-c', '--categories',
dest='categories',
metavar='CATEGORIES',
required=False,
default=[],
nargs='*',
help='send events for these categories only')
if __name__ == '__main__':
arguments = parser.parse_args()
for_categories = list(arguments.categories)
ob = ipaaca.OutputBuffer('IpaacaSessionReplay')
# CAUTION: special semantics for replay script: do NOT auto-retract at shutdown
# (should only replicate observed retractions form the data file!)
ob._teardown = lambda: True
ob.register_handler(iu_update_handler)
known_ius = {}
log_file = sys.stdin if (arguments.input_file[0]=='-') else open(arguments.input_file[0], 'r')
ref_time = None
last_time = None
for line in log_file:
record = eval(line, {'__builtins__': None}, {})
# event data
uid = record['uid']
category = record['category']
typ = record['type']
payload = record['payload']
# consider only if category filter passes
if len(for_categories)==0 or category in for_categories:
# take ref time from first event
if ref_time is None:
ref_time = record['time']
last_time = ref_time
# recreate delay from previous event
print('Sleep for '+str(record['time'] - last_time)+' s')
time.sleep(record['time'] - last_time)
last_time = record['time']
# do it
print('Synthesize event '+typ+' for IU UID '+uid+', category "'+category+'"')
if typ=='MESSAGE':
msg = ipaaca.Message(category)
msg.payload = payload
ob.add(msg)
elif typ=='ADDED':
if uid in known_ius:
print('ERROR - already added UID '+uid+' before!')
else:
iu = ipaaca.IU(category)
iu.payload = payload
ob.add(iu)
known_ius[uid] = iu
elif typ=='UPDATED':
if uid not in known_ius:
print('ERROR - have not received IU with UID '+uid+' before!')
else:
iu = known_ius[uid]
iu.payload = payload
elif typ=='COMMITTED':
if uid not in known_ius:
print('ERROR - have not received IU with UID '+uid+' before!')
else:
iu = known_ius[uid]
iu.commit()
elif typ=='RETRACTED':
if uid not in known_ius:
print('ERROR - have not received IU with UID '+uid+' before!')
else:
iu = known_ius[uid]
ob.remove(iu)
try:
print('Waiting; specified grace time '+str(arguments.keep_alive)+' s')
time.sleep(arguments.keep_alive)
except KeyboardInterrupt:
pass
except Exception, e:
print(u'Exception: '+unicode(traceback.format_exc()))
if log_file is not sys.stdin:
log_file.close()
ipaaca.exit(1)
if log_file is not sys.stdin:
log_file.close()
ipaaca.exit(0)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of IPAACA, the
# "Incremental Processing Architecture
# for Artificial Conversational Agents".
#
# Copyright (c) 2009-2016 Social Cognitive Systems Group
# CITEC, Bielefeld University
#
# http://opensource.cit-ec.de/projects/ipaaca/
# http://purl.org/net/ipaaca
#
# This file may be licensed under the terms of of the
# GNU Lesser General Public License Version 3 (the ``LGPL''),
# or (at your option) any later version.
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the LGPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the LGPL along with this
# program. If not, go to http://www.gnu.org/licenses/lgpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The development of this software was supported by the
# Excellence Cluster EXC 277 Cognitive Interaction Technology.
# The Excellence Cluster EXC 277 is a grant of the Deutsche
# Forschungsgemeinschaft (DFG) in the context of the German
# Excellence Initiative.
from __future__ import division, print_function
import os
import platform
import sys
import time
import traceback
import ipaaca
def exit(code):
ipaaca.exit(code)
#if platform.system() == 'Windows':
# os._exit(code)
#else:
# sys.exit(code)
parser = ipaaca.IpaacaArgumentParser(description='Ipaaca Maintainor')
if __name__ == '__main__':
arguments = parser.parse_args()
# Warn if user tries to configure a transport other than socket or a
# server mode other than server
if (ipaaca.defaults.IPAACA_DEFAULT_RSB_TRANSPORT is not None and
ipaaca.defaults.IPAACA_DEFAULT_RSB_TRANSPORT != 'socket'):
print("ERROR: Works only with RSB transport type 'socket'.")
ipaaca.exit(1)
if (ipaaca.defaults.IPAACA_DEFAULT_RSB_SOCKET_SERVER is not None and
ipaaca.defaults.IPAACA_DEFAULT_RSB_SOCKET_SERVER != '1'):
print("ERROR: Works only in socket server mode '0'.")
ipaaca.exit(1)
# Configure rsb socket transport
ipaaca.defaults.IPAACA_DEFAULT_RSB_TRANSPORT = 'socket'
ipaaca.defaults.IPAACA_DEFAULT_RSB_SOCKET_SERVER = '1'
ob = ipaaca.OutputBuffer("IPAACAMaintainor")
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
decision = raw_input('Shutdown the IPAACA socket transport hub (y/[n])?')
if decision in 'yY':
break
except Exception, e:
print(u'Exception: '+unicode(traceback.format_exc()))
ipaaca.exit(1)
ipaaca.exit(0)