Week 10 Mesh Networking

The Hard Way

To really get down into the nitty gritty of mesh networking, one should use the lowest level presence server calls in order to create a mesh networked activity. In order to accomplish a working game using this methodology, you must have an understanding of D-Bus and all of its quirks. This is outside the scope of this course, but the following may get you started onto your journey of mesh zen:

The Easy Way

Making use of OLPCGames in order to do mesh networking between XOs is much much simpler. The necessary import is olpcgames.mesh, and requires significantly less programming than the previous option. Communication between XOs can be done simply by sending strings, or possibly even structs (as we will see later), between XOs. API documentation can be found at: http://dev.laptop.org/~mcfletch/OLPCGames/pydoc/olpcgames.mesh.html.

OLPCGames Mesh Events

The following are all of the event types that you can subscribe to in order to get all asynchronous data from the network.

  • olpcgames.CONNECT
    • This means that the host user has clicked Share in the top Sugar bar. This means the user is interested in sharing the activity in some way
      • For example, a multiplayer game
  • olpcgames.PARTICIPANT_ADD
    • A new participant has joined the shared activity. This occurs for all users, not just the host. The user's handle is provided as well, which can later be used for unicast messaging.
    • A participant has left the activity. Again, the handle of that user is provided.
  • olpcgames.MESSAGE_UNI
    • Somebody has sent a unicast message to you. That user's handle, and the message content are both provided.
  • olpcgames.MESSAGE_MULTI
    • A broadcast message has been sent. The user handle and message content is provided.

Connecting to These Events

So how do you make use of these events? Well they're simply added to the event queue in pygame. So here's the code to go through the events, find one that means a new participant has joined, and print their handle.

import olpcgames

# More code here...

def withBuddy( buddy ):
    print buddy

# More code here...

for event in pygame.event.get():
    if event.type == olpcgames.PARTICIPANT_ADD:
        olpcgames.mesh.lookup_buddy(event.handle, callback=withBuddy)

So what does this do exactly? Whenever a participant is added, a human readable string representation of the buddy is printed to the commandline.

Other Functions of Use

In order to complete the assignment below, you will still need a couple of functions

  • send_to( handle, message )
    • How you will send a unicast message between XOs.
  • broadcast( message )
    • How you will broadcast a message to all connected users.
  • get_participants()
    • Get a list of all participants in the room
    • Should be used if you arrive to the party a little late (not the host, so you don't know everybody that has connected)

How to Send Data

What if we want to send more than just a message. Say a date stamp as well as a message for instance? We'd use Python structs. Here is an example from the Python Docs: [http://docs.python.org/library/struct.html]

>>> from struct import *
>>> pack('hhl', 1, 2, 3)
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)

As long as both the sender and receiver understand how the struct has been packed, it can be coded and decoded from objects->text->objects. In this particular instance, the sender has packaged two shorts and a long into a struct.

The Assignment

Make use of OLPCGames's mesh functionality in order to create a lobbied chat activity to the following specification (work in pairs):

  • A 3-paned GTK view with a user list, message display area, and text input area
  • When a user just types in a message into the text input area, it is sent to all connected users with a timestamp of when the sender sent it
    • The displayed message on other XOs is "USERNAME (TIMESTAMP): MESSAGE"
  • When a user types in a message as "/msg USERNAME MESSAGE", it should be sent as a unicast message to that user
    • This message should also be displayed in the message display area, but italicized
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License