2014-09-16 22:42:47 +03:00
|
|
|
#include "interface/module.h"
|
2014-09-16 23:14:04 +03:00
|
|
|
#include "interface/server.h"
|
2014-09-17 01:04:51 +03:00
|
|
|
#include "interface/event.h"
|
2014-09-17 02:18:03 +03:00
|
|
|
#include "test1/include/api.h"
|
2014-09-18 23:47:05 +03:00
|
|
|
//#include "client_lua/include/api.h"
|
|
|
|
#include "client_file/include/api.h"
|
2014-09-17 20:24:06 +03:00
|
|
|
#include "network/include/api.h"
|
2014-09-17 23:44:42 +03:00
|
|
|
#include "core/log.h"
|
2014-09-16 22:42:47 +03:00
|
|
|
|
2014-09-17 01:04:51 +03:00
|
|
|
using interface::Event;
|
|
|
|
|
2014-09-17 02:18:03 +03:00
|
|
|
namespace test1 {
|
|
|
|
|
2014-09-16 23:14:04 +03:00
|
|
|
struct Module: public interface::Module
|
2014-09-16 22:42:47 +03:00
|
|
|
{
|
2014-09-17 02:18:03 +03:00
|
|
|
interface::Server *m_server;
|
2014-09-17 20:49:30 +03:00
|
|
|
|
2014-09-18 18:12:59 +03:00
|
|
|
Event::Type m_EventType_test1_thing; // You can cache these for more speed
|
2014-09-17 02:18:03 +03:00
|
|
|
|
|
|
|
Module(interface::Server *server):
|
2014-09-17 23:00:31 +03:00
|
|
|
interface::Module("test1"),
|
2014-09-17 02:18:03 +03:00
|
|
|
m_server(server),
|
2014-09-17 16:43:05 +03:00
|
|
|
m_EventType_test1_thing(Event::t("test1:thing"))
|
2014-09-16 23:14:04 +03:00
|
|
|
{
|
2014-09-17 23:44:42 +03:00
|
|
|
log_v(MODULE, "test1 construct");
|
2014-09-16 23:14:04 +03:00
|
|
|
}
|
2014-09-16 22:42:47 +03:00
|
|
|
|
2014-09-18 01:46:16 +03:00
|
|
|
~Module()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "test1 destruct");
|
|
|
|
}
|
|
|
|
|
2014-09-17 14:53:06 +03:00
|
|
|
void init()
|
|
|
|
{
|
2014-09-17 23:44:42 +03:00
|
|
|
log_v(MODULE, "test1 init");
|
2014-09-17 23:00:31 +03:00
|
|
|
m_server->sub_event(this, Event::t("core:start"));
|
2014-09-17 14:53:06 +03:00
|
|
|
m_server->sub_event(this, m_EventType_test1_thing);
|
2014-09-17 20:24:06 +03:00
|
|
|
m_server->sub_event(this, Event::t("network:new_client"));
|
2014-09-18 23:47:05 +03:00
|
|
|
m_server->sub_event(this, Event::t("client_file:files_sent"));
|
2014-09-18 01:35:53 +03:00
|
|
|
m_server->sub_event(this, Event::t("network:packet_received"));
|
2014-09-17 14:53:06 +03:00
|
|
|
}
|
|
|
|
|
2014-09-17 18:52:59 +03:00
|
|
|
void event(const Event::Type &type, const Event::Private *p)
|
2014-09-16 23:21:41 +03:00
|
|
|
{
|
2014-09-17 23:00:31 +03:00
|
|
|
EVENT_VOIDN("core:start", on_start)
|
2014-09-17 20:15:46 +03:00
|
|
|
EVENT_TYPE(m_EventType_test1_thing, on_thing, Thing)
|
2014-09-17 20:24:06 +03:00
|
|
|
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
|
2014-09-18 23:47:05 +03:00
|
|
|
EVENT_TYPEN("client_file:files_sent", on_files_sent,
|
|
|
|
client_file::FilesSent)
|
2014-09-18 01:35:53 +03:00
|
|
|
EVENT_TYPEN("network:packet_received", on_packet_received, network::Packet)
|
2014-09-16 23:21:41 +03:00
|
|
|
}
|
|
|
|
|
2014-09-17 23:00:31 +03:00
|
|
|
void on_start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:43:05 +03:00
|
|
|
void on_thing(const Thing &thing)
|
2014-09-17 01:04:51 +03:00
|
|
|
{
|
2014-09-17 23:44:42 +03:00
|
|
|
log_i(MODULE, "test1.thing: some_data=%s", cs(thing.some_data));
|
2014-09-17 01:04:51 +03:00
|
|
|
}
|
2014-09-17 20:24:06 +03:00
|
|
|
|
|
|
|
void on_new_client(const network::NewClient &new_client)
|
|
|
|
{
|
2014-09-17 23:44:42 +03:00
|
|
|
log_i(MODULE, "test1::on_new_client: id=%zu", new_client.info.id);
|
2014-09-17 20:24:06 +03:00
|
|
|
|
2014-09-18 02:09:53 +03:00
|
|
|
network::access(m_server, [&](network::Interface * inetwork){
|
|
|
|
inetwork->send(new_client.info.id, "test1:dummy", "dummy data");
|
2014-09-18 21:54:29 +03:00
|
|
|
/*inetwork->send(new_client.info.id, "core:run_script",
|
2014-09-18 14:25:07 +03:00
|
|
|
"print(\"Remote script is running\")\n"
|
|
|
|
"require \"Polycode/Core\"\n"
|
|
|
|
"scene = Scene(Scene.SCENE_2D)\n"
|
|
|
|
"scene:getActiveCamera():setOrthoSize(640, 480)\n"
|
2014-09-18 14:43:34 +03:00
|
|
|
"label = SceneLabel(\"Hello from remote module!\", 32)\n"
|
|
|
|
"label:setPosition(0, -100, 0)\n"
|
2014-09-18 14:25:07 +03:00
|
|
|
"scene:addChild(label)\n"
|
2014-09-18 21:54:29 +03:00
|
|
|
);*/
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-18 23:47:05 +03:00
|
|
|
void on_files_sent(const client_file::FilesSent &event)
|
2014-09-18 21:54:29 +03:00
|
|
|
{
|
2014-09-18 23:47:05 +03:00
|
|
|
log_v(MODULE, "on_files_sent(): recipient=%zu", event.recipient);
|
2014-09-18 21:54:29 +03:00
|
|
|
network::access(m_server, [&](network::Interface * inetwork){
|
|
|
|
inetwork->send(event.recipient, "core:run_script",
|
|
|
|
"print(\"TODO: Run init.lua\")");
|
2014-09-19 01:42:52 +03:00
|
|
|
inetwork->send(event.recipient, "core:run_script",
|
|
|
|
"buildat:send_packet(\"foo\", \"bar\")");
|
2014-09-18 02:09:53 +03:00
|
|
|
});
|
2014-09-17 20:24:06 +03:00
|
|
|
}
|
2014-09-18 01:35:53 +03:00
|
|
|
|
|
|
|
void on_packet_received(const network::Packet &packet)
|
|
|
|
{
|
2014-09-19 00:53:06 +03:00
|
|
|
log_i(MODULE, "test1::on_packet_received: name=%zu, size=%zu",
|
|
|
|
cs(packet.name), packet.data.size());
|
2014-09-18 01:35:53 +03:00
|
|
|
}
|
2014-09-16 23:14:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
2014-09-17 02:18:03 +03:00
|
|
|
EXPORT void* createModule_test1(interface::Server *server){
|
|
|
|
return (void*)(new Module(server));
|
|
|
|
}
|
2014-09-16 23:14:04 +03:00
|
|
|
}
|
2014-09-16 22:42:47 +03:00
|
|
|
}
|