relay_gen_java.py 3 KB
Newer Older
Anders Blomdell's avatar
Anders Blomdell committed
1
2
3
4
#!/usr/bin/python

import re
import sys
Anders Blomdell's avatar
Anders Blomdell committed
5
import random
Anders Blomdell's avatar
Anders Blomdell committed
6
7
8
9
10
11
12
13
14

def split_match(pattern, multiline):
    def match(s):
        m = re.match(pattern, s)
        if m:
            return m.group(1)
        pass
    return filter(lambda s: s != None, map(match, multiline.split('\n')))
   
Anders Blomdell's avatar
Anders Blomdell committed
15
16
17
18
def shuffle(l):
    result = list(l)
    random.shuffle(result)
    return result
Anders Blomdell's avatar
Anders Blomdell committed
19
20
21
22
23

if __name__ == '__main__':
    f = open(sys.argv[1])
    sample = []
    for l in map(lambda s: s.strip(), f):
24
        lang,kind,func,arg,dummy = l[1:].split(l[0])
Anders Blomdell's avatar
Anders Blomdell committed
25
26
27
28
29
30
31
32
33
        if lang == 'Java' and kind == 'sample':
            sample.append((func, arg))
            pass
        pass
    result = []
    result.extend(split_match('^[^|]*\|(.*)$', """
      |import java.io.FileInputStream;
      |import java.io.FileOutputStream;
      |import java.io.IOException;
Anders Blomdell's avatar
Anders Blomdell committed
34
35
      |import se.lth.control.labcomm.DecoderChannel;
      |import se.lth.control.labcomm.EncoderChannel;
36
      |import se.lth.control.labcomm.Sample;
Anders Blomdell's avatar
Anders Blomdell committed
37
38
39
40
41
42
43
44
45
      |
      |public class java_relay implements
    """))
    for func,arg in sample[0:-1]:
        result.append('  %s.Handler,' % func)
        pass
    result.append('  %s.Handler' % sample[-1][0])
    result.extend(split_match('^[^|]*\|(.*)$', """
      |{
Anders Blomdell's avatar
Anders Blomdell committed
46
      |  EncoderChannel encoder;
Anders Blomdell's avatar
Anders Blomdell committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    """))
    for func,arg in sample:
        if arg == 'void':
            result.extend(split_match('^[^|]*\|(.*)$', """
              |  public void handle_%(func)s() throws IOException {
              |    %(func)s.encode(encoder);
              |  }
            """ % { 'func': func}))
            pass
        else:
            result.extend(split_match('^[^|]*\|(.*)$', """
              |  public void handle_%(func)s(%(arg)s data) throws IOException {
              |    %(func)s.encode(encoder, data);
              |  }
            """ % { 'func': func, 'arg': arg}))
            pass
        pass
    result.extend(split_match('^[^|]*\|(.*)$', """
      |  public java_relay(String InName, String OutName) throws Exception {
      |    FileInputStream InFile = new FileInputStream(InName);
Anders Blomdell's avatar
Anders Blomdell committed
67
      |    DecoderChannel decoder = new DecoderChannel(InFile);
Anders Blomdell's avatar
Anders Blomdell committed
68
      |    FileOutputStream OutFile = new FileOutputStream(OutName);
Anders Blomdell's avatar
Anders Blomdell committed
69
      |    encoder = new EncoderChannel(OutFile);
Anders Blomdell's avatar
Anders Blomdell committed
70
71
      |
    """))
Anders Blomdell's avatar
Anders Blomdell committed
72
73
    for func,arg in shuffle(sample):
        result.append('    %s.register(decoder, this);' % func)
Anders Blomdell's avatar
Anders Blomdell committed
74
        pass
Anders Blomdell's avatar
Anders Blomdell committed
75
76
77
78
    for func,arg in shuffle(sample):
        result.append('    %s.registerSampleRef(decoder);' % func)
        pass
    for func,arg in shuffle(sample):
Anders Blomdell's avatar
Anders Blomdell committed
79
80
        result.append('    %s.register(encoder);' % func)
        pass
Anders Blomdell's avatar
Anders Blomdell committed
81
82
83
    for func,arg in shuffle(sample):
        result.append('    %s.registerSampleRef(encoder);' % func)
        pass
Anders Blomdell's avatar
Anders Blomdell committed
84
85
    result.extend(split_match('^[^|]*\|(.*)$', """
      |    try {
Anders Blomdell's avatar
Anders Blomdell committed
86
      |      decoder.run();
Anders Blomdell's avatar
Anders Blomdell committed
87
88
89
90
91
92
93
94
95
96
      |    } catch (java.io.EOFException e) {
      |    }
      |  }
      |  public static void main(String[] arg) throws Exception {
      |    new java_relay(arg[0], arg[1]);
      |  }
      |}
    """))
    print "\n".join(result)
    pass