1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""
6netprotos - Network Protocols
7
8This module includes Python implementations of various network protocols to
9use in testing. These protocols can be used either with the kernel network
10stack openning UDP, TCP or RAW sockets or with the simulated network stack
11based on the lansim package.
12
13In either case, the interface used requires a Host object providing the
14following interface:
15 * Host.ip_addr property, with the host's IP address in plain text.
16 * Host.socket(family, sock_type) that returns a new asynchronous socket.
17
18An asynchronous socket must have the following interface:
19 * listen(ip_addr, port, recv_callback)
20 * send(data, ip_addr, port)
21 * close()
22
23See lansim.host.UDPSocket for details on the utilization of this interface.
24Note that this interface is asynchronous since there's no blocking recv()
25method. Instead, when the main loop event handler receives a packet for
26this socket, the recv_callback passed will be called.
27
28
29To create new protocols you can follow the example of ZeroconfDaemon and
30CrosP2PDaemon which implement part of those protocols to serve files on
31the LAN.
32
33To launch a ZeroconfDaemon on a simulated Host, simply create that
34object for the given Host instance as follows:
35
36    zero = zeroconf.ZeroconfDaemon(host_b, "host-name-b")
37
38Once again, a CrosP2PDaemon requires a ZeroconfDaemon instance to
39interact with, so simply creating the object will make it available.
40Although it is not sharing any file it anounces the num_connections
41attribute among other mDNS records required for P2P to work.
42
43    p2p = cros_p2p.CrosP2PDaemon(zero)
44
45To add files and share them on the P2P server, the interface is the
46following:
47
48    p2p.add_file('some_payload', 3000)
49    p2p.add_file('other_payload', 6000)
50
51"""
52