Skip to content

Instantly share code, notes, and snippets.

@mazhe
Created December 27, 2011 14:20
Show Gist options
  • Save mazhe/1523788 to your computer and use it in GitHub Desktop.
Save mazhe/1523788 to your computer and use it in GitHub Desktop.
Quick'n'dirty service example with cscid
[submodule "amibis"]
path = amibis
url = git://github.com/mazhe/amibis-c.git
PROJECT(GtalkConfBot C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
ADD_SUBDIRECTORY(amibis EXCLUDE_FROM_ALL)
INCLUDE_DIRECTORIES(${amibis_SOURCE_DIR}/include)
ADD_EXECUTABLE(servicetest servicetest.c)
TARGET_LINK_LIBRARIES(servicetest amibis-shared)
#include <stdio.h>
#include <string.h>
#include <amibis.h>
int
main(void)
{
amibis_localservice_t srv = NULL;
amibis_event_t evt = NULL;
int evterr = AMIBIS_OK;
amibis_localservice_create(&srv, "ServiceTest");
amibis_localservice_addvariable(
srv, "version", "amibis", 0, AMIBIS_VARIABLE_CONST);
amibis_localservice_addvariable(
srv, "constvar", "cacheme", 0, AMIBIS_VARIABLE_CONST);
amibis_localservice_addvariable(srv,
"rvar", "readme", 0, AMIBIS_VARIABLE_READ);
amibis_localservice_addvariable(srv,
"rwvar", "writeme", 0, AMIBIS_VARIABLE_READWRITE);
amibis_localservice_addconnector(srv, "echo", AMIBIS_CONNECTOR_INOUT);
printf("> Service %08x created\n", amibis_localservice_peerid(srv));
amibis_localservice_start(srv);
amibis_event_create(&evt);
while ((evterr = amibis_localservice_nextevent(srv, evt, NULL)) == AMIBIS_OK) {
char buf[64];
size_t buflen = 0;
switch (amibis_event_type(evt)) {
case AMIBIS_EVENT_ERROR:
printf(">> Service error (should not happens here)\n");
break;
case AMIBIS_EVENT_REGISTERED:
printf(">> Service registered\n");
break;
case AMIBIS_EVENT_DEREGISTERED:
printf(">> Service deregistered\n");
break;
case AMIBIS_EVENT_CONTROLOPENED:
printf(">> Control opened by %08x\n",
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONTROLOPENABORTED:
printf(">> Control opened by %08x, but aborted\n",
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONTROLCLOSED:
printf(">> Control closed by %08x\n",
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_VARIABLEREAD:
printf(">> Variable \"%s\" read by %08x\n",
amibis_event_variablename(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_VARIABLEWRITTEN:
printf(">> Variable \"%s\" written by %08x\n",
amibis_event_variablename(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_VARIABLESUBSCRIBED:
printf(">> Variable \"%s\" subscribed by %08x\n",
amibis_event_variablename(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_VARIABLEUNSUBSCRIBED:
printf(">> Variable \"%s\" unsubscribed by %08x\n",
amibis_event_variablename(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_FULLDESCRIPTION:
printf(">> Full description requested by %08x\n",
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONNECTOROPENED:
printf(">> Connector \"%s\" opened by %08x\n",
amibis_event_connectorname(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONNECTOROPENABORTED:
printf(">> Connector \"%s\" opened by %08x, but aborted\n",
amibis_event_connectorname(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONNECTORCLOSED:
printf(">> Connector \"%s\" closed by %08x\n",
amibis_event_connectorname(evt),
amibis_event_peerid(evt));
break;
case AMIBIS_EVENT_CONNECTORINPUT:
printf(">> Connector \"%s\" input from %08x\n",
amibis_event_connectorname(evt),
amibis_event_peerid(evt));
uint32_t peerid = 0, msgid = 0;
memset(buf, 0, sizeof(buf));
buflen =
amibis_localservice_connectorinput(
srv,
amibis_event_connectorname(evt),
buf,
64,
&msgid,
&peerid);
printf(">>> `%s'\n", buf);
amibis_localservice_connectoroutput(
srv,
amibis_event_connectorname(evt),
buf,
buflen,
&msgid,
&peerid);
break;
case AMIBIS_EVENT_TIMEOUT:
printf(">> Timeout\n");
break;
case AMIBIS_EVENT_CLIENTERROR:
printf(">> Client error by %08x\n",
amibis_event_peerid(evt));
break;
default:
printf(">> Event (type %d) not handled\n",
amibis_event_type(evt));
break;
}
}
if (evterr != AMIBIS_OK) {
printf("> Service exited with error no %d\n", evterr);
}
amibis_event_free(evt);
amibis_localservice_free(srv);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment