Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5385da8778200f2720e76318da0642c19dc975ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# *****************************************************************************
# * Copyright (c) 2011, 2013-2014 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

"""
TCF - Target Communication Framework
"""

import types

from . import protocol, peer, channel
from .util import task

__all__ = ('connect', 'peers')


def connect(params, wait=True):
    """Connect to peer. Argument is a string of the form
    <transport>:<host>:<port>, e.g. "TCP:127.0.0.1:1534".
    """
    if type(params) is types.StringType:
        params = _parse_params(params)
    elif type(params) is not types.DictType:
        raise TypeError("Expected string or dict")
    p = peer.TransientPeer(params)
    if wait:
        c = task.Task(_openChannel, p).get()
    else:
        c = protocol.invokeAndWait(p.openChannel)
    return c


def peers():
    "Return list of discovered remote peers"
    locator = protocol.getLocator()
    if locator:
        return protocol.invokeAndWait(locator.getPeers)


def _openChannel(p, done=None):
    assert protocol.isDispatchThread()
    c = p.openChannel()
    if done is None:
        return

    class ChannelListener(channel.ChannelListener):
        def onChannelOpened(self):
            c.removeChannelListener(self)
            done(None, c)

        def onChannelClosed(self, error):
            done(error, None)
    c.addChannelListener(ChannelListener())


def _parse_params(paramStr):
    args = paramStr.split(":")
    if len(args) != 3:
        raise ValueError("Expected format: <transport>:<host>:<port>")
    transp, host, port = args
    return {
        peer.ATTR_IP_HOST: host,
        peer.ATTR_IP_PORT: port,
        peer.ATTR_TRANSPORT_NAME: transp
    }

Back to the top