remove broker and add group command

This commit is contained in:
云风
2012-08-30 17:26:48 +08:00
parent f4a895fa6f
commit 4610781498
7 changed files with 36 additions and 129 deletions

View File

@@ -10,7 +10,6 @@ all : \
lualib/skynet.so \
service/gate.so \
service/client.so \
service/broker.so \
service/connection.so \
client \
lualib/socket.so \
@@ -66,9 +65,6 @@ service/connection.so : connection/connection.c connection/main.c
lualib/socket.so : connection/lua-socket.c
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iconnection
service/broker.so : service-src/service_broker.c
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src
lualib/lpeg.so : lpeg/lpeg.c
gcc $(CFLAGS) $(SHARED) -O2 $^ -o $@ -Ilpeg

View File

@@ -223,17 +223,24 @@ function skynet.start(f)
end
function skynet.newservice(name, ...)
local args = { "snlua" , name , ... }
local handle = skynet.call(".launcher", table.concat(args," "))
local handle = skynet.call(".launcher", skynet.unpack, skynet.pack("snlua", name, ...))
return handle
end
function skynet.enter_group(handle)
c.command("GROUP", "ENTER " .. tostring(handle))
local function group_command(cmd, handle, address)
if address then
return string.format("%s %d :%x",cmd, handle, address)
else
return string.format("%s %d",cmd,handle)
end
end
function skynet.leave_group(handle)
c.command("GROUP", "LEAVE " .. tostring(handle))
function skynet.enter_group(handle , address)
c.command("GROUP", group_command("ENTER", handle, address))
end
function skynet.leave_group(handle , address)
c.command("GROUP", group_command("LEAVE", handle, address))
end
function skynet.clear_group(handle)

View File

@@ -1,109 +0,0 @@
#include "skynet.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#define DEFAULT_NUMBER 16
#define LAUNCHER ".launcher"
struct worker {
int init;
uint32_t address;
};
struct broker {
int init;
int id;
uint32_t launcher;
char * name;
struct worker w[DEFAULT_NUMBER];
};
struct broker *
broker_create(void) {
struct broker *b = malloc(sizeof(*b));
memset(b,0,sizeof(*b));
return b;
}
void
broker_release(struct broker * b) {
free(b->name);
free(b);
}
static void
_init(struct broker *b, int session, uint32_t address) {
assert(session > 0 && session <= DEFAULT_NUMBER);
int id = session - 1;
assert(b->w[id].init == 0);
b->w[id].address = address;
b->w[id].init = 1;
++b->init;
}
static void
_forward(struct broker *b, struct skynet_context * context) {
skynet_forward(context, b->w[b->id].address);
b->id = (b->id + 1) % DEFAULT_NUMBER;
}
static int
_cb(struct skynet_context * context, void * ud, int session, uint32_t source, const void * msg, size_t sz) {
struct broker * b = ud;
if (b->init < DEFAULT_NUMBER) {
if (source != b->launcher)
return 0;
char addr[sz+1];
memcpy(addr, msg, sz);
addr[sz] = '\0';
uint32_t address = strtoul(addr+1, NULL, 16);
assert(address != 0);
_init(b, session, address);
if (b->init == DEFAULT_NUMBER) {
skynet_command(context, "REG", b->name);
skynet_send(context, 0, b->launcher, 0, NULL, 0, 0);
}
} else {
_forward(b, context);
}
return 0;
}
int
broker_init(struct broker *b, struct skynet_context *ctx, const char * args) {
b->launcher = skynet_queryname(ctx, LAUNCHER);
if (b->launcher == 0) {
skynet_error(ctx, "Can't query %s", LAUNCHER);
return 1;
}
char * service = strchr(args,' ');
if (service == NULL) {
return 1;
}
int len = service - args;
if (len>0) {
b->name = malloc(len +1);
memcpy(b->name, args, len);
b->name[len] = '\0';
}
service++;
int i;
len = strlen(service);
if (len == 0)
return 1;
for (i=0;i<DEFAULT_NUMBER;i++) {
int id = skynet_send(ctx, 0, b->launcher , -1, service , len, 0);
assert(id > 0 && id <= DEFAULT_NUMBER);
}
skynet_callback(ctx, b, _cb);
return 0;
}

View File

@@ -3,23 +3,26 @@ local string = string
local instance = {}
local function _extract( service, ...)
return service , table.concat({...}, " ")
end
skynet.dispatch(function(msg, sz , session, address)
local message = skynet.tostring(msg,sz)
if session == 0 then
-- init notice
local reply = instance[address]
if reply then
skynet.send(reply[2] , reply[1], skynet.address(address))
skynet.send(reply[2] , reply[1], skynet.pack(address))
instance[address] = nil
end
else
-- launch request
local service, param = string.match(message, "([^ ]+) (.*)")
local service , param = _extract(skynet.unpack(msg,sz))
local inst = skynet.launch(service, param)
if inst then
instance[inst] = { session, address }
else
skynet.send(address, session)
skynet.send(address, session, skynet.pack(nil))
end
end
end)

View File

@@ -5,7 +5,8 @@ skynet.dispatch()
skynet.start(function()
local group = skynet.query_group(1)
for i=1,10 do
skynet.newservice("testgroup_c", tostring(i))
local address = skynet.newservice("testgroup_c", tostring(i))
skynet.enter_group(1 , address)
end
skynet.send(group,"Hello World")
skynet.exit()

View File

@@ -8,5 +8,4 @@ end
skynet.start(function()
print("start",id)
skynet.enter_group(1)
end)

View File

@@ -250,8 +250,17 @@ _copy_name(char name[GLOBALNAME_LENGTH], const char * addr) {
}
static const char *
_group_command(struct skynet_context * ctx, const char * cmd, int handle) {
uint32_t self = ctx->handle;
_group_command(struct skynet_context * ctx, const char * cmd, int handle, uint32_t v) {
uint32_t self;
if (v != 0) {
if (skynet_harbor_message_isremote(v)) {
skynet_error(ctx, "Can't add remote handle %x",v);
return NULL;
}
self = v;
} else {
self = ctx->handle;
}
if (strcmp(cmd, "ENTER") == 0) {
skynet_group_enter(handle, self);
return NULL;
@@ -417,8 +426,9 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
tmp[sz] = '\0';
char cmd[sz+1];
int handle=0;
sscanf(tmp, "%s %d",cmd,&handle);
return _group_command(context, cmd, handle);
uint32_t addr=0;
sscanf(tmp, "%s %d :%x",cmd,&handle,&addr);
return _group_command(context, cmd, handle,addr);
}
return NULL;