Added threads to C and Python test plugins

This commit is contained in:
James Booth
2016-03-09 23:39:41 +00:00
parent cc98724d8f
commit ff22cd4de4
3 changed files with 50 additions and 32 deletions

View File

@@ -1,7 +1,18 @@
import prof
import threading
import time
plugin_win = "Python Test"
count = 0
def _inc_counter():
global count
while True:
time.sleep(5)
count = count + 1
def _handle_win_input(win, line):
prof.win_show(win, "Input received: " + line)
@@ -139,6 +150,10 @@ def cmd_pythontest(arg1=None, arg2=None, arg3=None, arg4=None, arg5=None):
prof.win_show(plugin_win, "called -> prof.log_error: " + arg3)
else:
prof.cons_bad_cmd_usage("/python-test")
elif arg1 == "count":
create_win()
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "Count: " + str(count))
else:
prof.cons_bad_cmd_usage("/python-test")
@@ -147,6 +162,10 @@ def timed_callback():
prof.win_show(plugin_win, "timed -> timed_callback called")
def prof_init(version, status):
t = threading.Thread(target=_inc_counter)
t.daemon = True
t.start()
prof.win_create(plugin_win, _handle_win_input)
synopsis = [
@@ -159,7 +178,8 @@ def prof_init(version, status):
"/python-test notify <message>",
"/python-test sendline <line>",
"/python-test get recipient|room",
"/python-test log debug|info|warning|error <message>"
"/python-test log debug|info|warning|error <message>",
"/python-test count"
]
description = "Python test plugins. All commands focus the plugin window."
args = [
@@ -173,7 +193,8 @@ def prof_init(version, status):
[ "sendline <line>", "Pass line to profanity to process" ],
[ "get recipient", "Show the current chat recipient, if in a chat window" ],
[ "get room", "Show the current room JID, if ina a chat room"],
[ "log debug|info|warning|error <message>", "Log a message at the specified level" ]
[ "log debug|info|warning|error <message>", "Log a message at the specified level" ],
[ "count", "Show the counter, incremented every 5 seconds by a worker thread" ]
]
examples = [
"/python-test sendline /about",
@@ -185,7 +206,7 @@ def prof_init(version, status):
prof.register_command("/python-test", 1, 5, synopsis, description, args, examples, cmd_pythontest)
prof.register_ac("/python-test",
[ "consalert", "consshow", "consshow_t", "constest", "winshow", "winshow_t", "notify", "sendline", "get", "log" ]
[ "consalert", "consshow", "consshow_t", "constest", "winshow", "winshow_t", "notify", "sendline", "get", "log", "count" ]
)
prof.register_ac("/python-test get",
[ "recipient", "room" ]

View File

@@ -1,27 +0,0 @@
import prof
import threading
import time
import sys
counter = 0
def _inc_counter():
global counter
while True:
time.sleep(1)
counter = counter + 1
def _cmd_count():
prof.cons_show("Counter: " + str(counter))
def prof_init(version, status):
t = threading.Thread(target=_inc_counter)
t.daemon = True
t.start()
synopsis = [ "/count" ]
description = "Python threading example."
args = []
examples = []
prof.register_command("/count", 0, 0, synopsis, description, args, examples, _cmd_count)

View File

@@ -3,10 +3,23 @@
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <unistd.h>
#include <profapi.h>
static PROF_WIN_TAG plugin_win = "C Test";
static pthread_t worker_thread;
static int count = 0;
void*
inc_counter(void *arg)
{
while (1) {
sleep(5);
count++;
}
}
void
handle_win_input(PROF_WIN_TAG win, char *line)
@@ -17,7 +30,8 @@ handle_win_input(PROF_WIN_TAG win, char *line)
prof_win_show(win, buf);
}
void create_win(void)
void
create_win(void)
{
if (!prof_win_exists(plugin_win)) {
prof_win_create(plugin_win, handle_win_input);
@@ -207,6 +221,12 @@ cmd_ctest(char **args)
} else {
prof_cons_bad_cmd_usage("/c-test");
}
} else if (strcmp(args[0], "count") == 0) {
create_win();
prof_win_focus(plugin_win);
char buf[100];
sprintf(buf, "Count: %d", count);
prof_win_show(plugin_win, buf);
} else {
prof_cons_bad_cmd_usage("/c-test");
}
@@ -222,6 +242,8 @@ timed_callback(void)
void
prof_init(const char * const version, const char * const status)
{
pthread_create(&worker_thread, NULL, inc_counter, NULL);
prof_win_create(plugin_win, handle_win_input);
const char *synopsis[] = {
@@ -235,6 +257,7 @@ prof_init(const char * const version, const char * const status)
"/c-test sendline <line>",
"/c-test get recipient|room",
"/c-test log debug|info|warning|error <message>",
"/c-test count",
NULL
};
const char *description = "C test plugin. All commands focus the plugin window.";
@@ -250,6 +273,7 @@ prof_init(const char * const version, const char * const status)
{ "get recipient", "Show the current chat recipient, if in a chat window" },
{ "get room", "Show the current room JID, if in a chat room" },
{ "log debug|info|warning|error <message>", "Log a message at the specified level" },
{ "count", "Show the counter, incremented every 5 seconds by a worker thread" },
{ NULL, NULL }
};
@@ -263,7 +287,7 @@ prof_init(const char * const version, const char * const status)
prof_register_command("/c-test", 1, 5, synopsis, description, args, examples, cmd_ctest);
char *cmd_ac[] = { "consalert", "consshow", "consshow_t", "constest", "winshow", "winshow_t", "notify", "sendline", "get", "log", NULL };
char *cmd_ac[] = { "consalert", "consshow", "consshow_t", "constest", "winshow", "winshow_t", "notify", "sendline", "get", "log", "count", NULL };
prof_register_ac("/c-test", cmd_ac);
char *get_ac[] = { "recipient", "room", NULL };