Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 51 additions & 47 deletions examples/boolean_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import time
import locale
Expand Down Expand Up @@ -26,64 +26,68 @@
server = "sandbox.spacebrew.cc"

for cur_ele in sys.argv:
if "name" in cur_ele:
name = cur_ele[5:]
if "server" in cur_ele:
server = cur_ele[7:]
if "name" in cur_ele:
name = cur_ele[5:]
if "server" in cur_ele:
server = cur_ele[7:]


# configure the spacebrew client
brew = Spacebrew(name=name, server=server)
brew.addPublisher("local state", "boolean")
brew.addSubscriber("remote state", "boolean")


def handleBoolean(value):
global code, stdscr
stdscr.addstr(pos_remote, pos_state, (str(value) + " ").encode(code))
stdscr.refresh()
global code, stdscr
stdscr.addstr(pos_remote, pos_state, (str(value) + " ").encode(code))
stdscr.refresh()


brew.subscribe("remote state", handleBoolean)

try:
# start-up spacebrew
brew.start()

# create and load info message at the top of the terminal window
info_msg = "This is the pySpacebrew library boolean example. It sends out a boolean message every time\n"
info_msg += "the enter or return key is pressed and displays the latest boolean value it has received.\n"
info_msg += "Connected to Spacebrew as: " + name + "\n"
info_msg += "IMPORTANT: don't shrink the Terminal window as it may cause app to crash (bug with curses lib)."
stdscr.addstr(0, 0, info_msg.encode(code))
stdscr.refresh()

# update the location for the remote and local dice state
pos_local = stdscr.getyx()[0] + 2
pos_remote = pos_local + 2

# display the label for the remote and local boolean states
stdscr.addstr(pos_local, 0, "local state: ".encode(code), curses.A_BOLD)
stdscr.addstr(pos_remote, 0, "remote state: ".encode(code), curses.A_BOLD)

# display the starting state for remote and local boolean states
stdscr.addstr(pos_local, pos_state, (str(local_state) + " ").encode(code))
stdscr.addstr(pos_remote, pos_state, (str(remote_state) + " ").encode(code))
stdscr.refresh()

# listen for keypresses and handle input
while 1:
c = stdscr.getch()

if (c == 10 or c == 13):
local_state = not local_state
brew.publish('local state', local_state)
stdscr.addstr(pos_local, pos_state, (str(local_state) + " ").encode(code))

stdscr.refresh()
# start-up spacebrew
brew.start()

# create and load info message at the top of the terminal window
info_msg = "This is the pySpacebrew library boolean example. It sends out a boolean message every time\n"
info_msg += "the enter or return key is pressed and displays the latest boolean value it has received.\n"
info_msg += "Connected to Spacebrew as: " + name + "\n"
info_msg += "IMPORTANT: don't shrink the Terminal window as it may cause app to crash (bug with curses lib)."
stdscr.addstr(0, 0, info_msg.encode(code))
stdscr.refresh()

# update the location for the remote and local dice state
pos_local = stdscr.getyx()[0] + 2
pos_remote = pos_local + 2

# display the label for the remote and local boolean states
stdscr.addstr(pos_local, 0, "local state: ".encode(code), curses.A_BOLD)
stdscr.addstr(pos_remote, 0, "remote state: ".encode(code), curses.A_BOLD)

# display the starting state for remote and local boolean states
stdscr.addstr(pos_local, pos_state, (str(local_state) + " ").encode(code))
stdscr.addstr(pos_remote, pos_state,
(str(remote_state) + " ").encode(code))
stdscr.refresh()

# listen for keypresses and handle input
while 1:
c = stdscr.getch()

if (c == 10 or c == 13):
local_state = not local_state
brew.publish('local state', local_state)
stdscr.addstr(pos_local, pos_state,
(str(local_state) + " ").encode(code))

stdscr.refresh()

# closing out the app and returning terminal to old settings
finally:
brew.stop()
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
brew.stop()
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
110 changes: 58 additions & 52 deletions examples/custom_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import time
import locale
Expand Down Expand Up @@ -27,70 +27,76 @@
server = "sandbox.spacebrew.cc"

for cur_ele in sys.argv:
if "name" in cur_ele:
name = cur_ele[5:]
if "server" in cur_ele:
server = cur_ele[7:]
if "name" in cur_ele:
name = cur_ele[5:]
if "server" in cur_ele:
server = cur_ele[7:]

# configure the spacebrew client
brew = Spacebrew(name=name, server=server)
brew.addPublisher("roll of the dice", "dice")
brew.addSubscriber("what did you roll", "dice")

# function that handles the incoming spacebrew dice messages


def handleDice(value):
global code, stdscr
stdscr.addstr(pos_remote, pos_state, (" " * 30).encode(code))
if value < 1 or value > 6:
stdscr.addstr(pos_remote, pos_state, ("you rolled a " + str(value) + "! What kind of dice is that? ").encode(code))
else:
stdscr.addstr(pos_remote, pos_state, str(value).encode(code))
stdscr.refresh()
global code, stdscr
stdscr.addstr(pos_remote, pos_state, (" " * 30).encode(code))
if value < 1 or value > 6:
stdscr.addstr(pos_remote, pos_state, ("you rolled a " +
str(value) + "! What kind of dice is that? ").encode(code))
else:
stdscr.addstr(pos_remote, pos_state, str(value).encode(code))
stdscr.refresh()


# register handler function with appropriate subscription data feed
brew.subscribe("what did you roll", handleDice)

try:
# start-up spacebrew
brew.start()

# create and load info message at the top of the terminal window
info_msg = "This is the pySpacebrew library custom data type example. It rolls the dice every time the enter or return\n"
info_msg += "key is pressed (value between 0 and 6), and displays the latest dice roll value it has received.\n"
info_msg += "Connected to Spacebrew as: " + name + "\n"
info_msg += "IMPORTANT: don't shrink the Terminal window as it may cause app to crash (bug with curses lib)."
stdscr.addstr(0, 0, info_msg.encode(code))
stdscr.refresh()

# update the location for the remote and local dice state
pos_local = stdscr.getyx()[0] + 2
pos_remote = pos_local + 2

# display the label for the remote and local dice roll states
stdscr.addstr(pos_local, 0, "local dice roll: ".encode(code), curses.A_BOLD)
stdscr.addstr(pos_remote, 0, "remote dice roll: ".encode(code), curses.A_BOLD)

# display the starting prompt next to local and remote dice state lines
stdscr.addstr(pos_local, pos_state, "waiting for first roll".encode(code))
stdscr.addstr(pos_remote, pos_state, "waiting for first roll".encode(code))
stdscr.refresh()

# listen for keypresses and handle input
while 1:
c = stdscr.getch()

if (c == 10 or c == 13):
local_state = random.randint(1,6)
brew.publish("roll of the dice", local_state)
stdscr.addstr(pos_local, pos_state, (" " * 30).encode(code))
stdscr.addstr(pos_local, pos_state, str(local_state).encode(code))

stdscr.refresh()
# start-up spacebrew
brew.start()

# create and load info message at the top of the terminal window
info_msg = "This is the pySpacebrew library custom data type example. It rolls the dice every time the enter or return\n"
info_msg += "key is pressed (value between 0 and 6), and displays the latest dice roll value it has received.\n"
info_msg += "Connected to Spacebrew as: " + name + "\n"
info_msg += "IMPORTANT: don't shrink the Terminal window as it may cause app to crash (bug with curses lib)."
stdscr.addstr(0, 0, info_msg.encode(code))
stdscr.refresh()

# update the location for the remote and local dice state
pos_local = stdscr.getyx()[0] + 2
pos_remote = pos_local + 2

# display the label for the remote and local dice roll states
stdscr.addstr(pos_local, 0, "local dice roll: ".encode(
code), curses.A_BOLD)
stdscr.addstr(pos_remote, 0, "remote dice roll: ".encode(
code), curses.A_BOLD)

# display the starting prompt next to local and remote dice state lines
stdscr.addstr(pos_local, pos_state, "waiting for first roll".encode(code))
stdscr.addstr(pos_remote, pos_state, "waiting for first roll".encode(code))
stdscr.refresh()

# listen for keypresses and handle input
while 1:
c = stdscr.getch()

if (c == 10 or c == 13):
local_state = random.randint(1, 6)
brew.publish("roll of the dice", local_state)
stdscr.addstr(pos_local, pos_state, (" " * 30).encode(code))
stdscr.addstr(pos_local, pos_state, str(local_state).encode(code))

stdscr.refresh()

# closing out the app and returning terminal to old settings
finally:
brew.stop()
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
brew.stop()
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
Loading