1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include <skstream/skstream.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "commander.h"
#include "stubServer.h"
#include "agent.h"
#include "clientConnection.h"
#include <Atlas/Objects/Operation.h>
#include <Eris/Exceptions.h>
#include <Eris/LogStream.h>
#include <Atlas/Objects/Encoder.h>
using Atlas::Objects::Root;
using Atlas::Objects::smart_dynamic_cast;
using namespace Atlas::Objects::Operation;
using namespace Eris;
using namespace Atlas::Objects::Entity;
typedef std::list<std::string> StringList;
#pragma mark -
Commander::Commander(StubServer* stub, int fd) :
m_server(stub),
m_channel(fd)
{
m_acceptor = new Atlas::Net::StreamAccept("Eris Stub Server", m_channel);
m_acceptor->poll(false);
}
Commander::~Commander()
{
}
void Commander::recv()
{
if (m_channel.eof()) return;
if (m_acceptor)
negotiate();
else {
m_codec->poll();
while (!m_objDeque.empty())
{
RootOperation op = smart_dynamic_cast<RootOperation>(m_objDeque.front());
if (!op.isValid())
throw InvalidOperation("Commander recived something that isn't an op");
dispatch(op);
m_objDeque.pop_front();
}
}
}
void Commander::objectArrived(const Root& obj)
{
m_objDeque.push_back(obj);
}
void Commander::negotiate()
{
m_acceptor->poll();
switch (m_acceptor->getState()) {
case Atlas::Net::StreamAccept::IN_PROGRESS:
break;
case Atlas::Net::StreamAccept::FAILED:
error() << "Commander got Atlas negotiation failure";
m_channel.close();
break;
case Atlas::Net::StreamAccept::SUCCEEDED:
m_codec = m_acceptor->getCodec(*this);
m_encoder = new Atlas::Objects::ObjectsEncoder(*m_codec);
m_codec->streamBegin();
delete m_acceptor;
m_acceptor = NULL;
break;
default:
throw InvalidOperation("unknown state from Atlas StreamAccept in Commander::negotiate");
}
}
#pragma mark -
void Commander::dispatch(const RootOperation& op)
{
Appearance appear = smart_dynamic_cast<Appearance>(op);
if (appear.isValid()) {
assert(op->hasAttr("for"));
Agent* ag = m_server->findAgentForEntity(op->getAttr("for").asString());
if (ag) {
ag->setEntityVisible(op->getTo(), true);
} else {
// doesn't exist yet, mark as visible if / when the agent is created
Agent::setEntityVisibleForFutureAgent(op->getTo(), op->getAttr("for").asString());
}
}
Disappearance disap = smart_dynamic_cast<Disappearance>(op);
if (disap.isValid()) {
assert(op->hasAttr("for"));
Agent* ag = m_server->findAgentForEntity(op->getAttr("for").asString());
if (ag) ag->setEntityVisible(op->getTo(), false);
}
Create cr = smart_dynamic_cast<Create>(op);
if (cr.isValid()) {
std::vector<Root> args(op->getArgs());
assert(!args.empty());
RootEntity ent = smart_dynamic_cast<RootEntity>(args.front());
assert(ent.isValid());
static int idCounter = 900;
char buf[32];
snprintf(buf, 32, "_created_%d", ++idCounter);
std::string id(buf);
ent->setId(id);
std::string loc = ent->getLoc();
assert(m_server->m_world.count(loc));
StringList children(m_server->m_world[loc]->getContains());
children.push_back(id);
m_server->m_world[loc]->setContains(children);
m_server->m_world[id] = ent;
Create bcr(cr);
bcr->setArgs1(ent);
Agent::broadcastSight(bcr);
}
Delete del = smart_dynamic_cast<Delete>(op);
if (del.isValid()) {
std::vector<Root> args(op->getArgs());
assert(!args.empty());
std::string id = args.front()->getId();
assert(m_server->m_world.count(id));
m_server->m_world.erase(id);
Agent::broadcastSight(op);
}
Move mv = smart_dynamic_cast<Move>(op);
if (mv.isValid()) {
RootEntity ent = m_server->getEntity(op->getTo());
std::vector<Root> args(op->getArgs());
if (args.front()->hasAttr("loc")) {
std::string newLocId = args.front()->getAttr("loc").asString();
RootEntity oldLoc = m_server->getEntity(ent->getLoc()),
newLoc = m_server->getEntity(newLocId);
ent->setLoc(newLocId);
// modify stamps?
oldLoc->modifyContains().remove(ent->getId());
newLoc->modifyContains().push_back(ent->getId());
}
if (args.front()->hasAttr("pos"))
ent->setPosAsList(args.front()->getAttr("pos").asList());
// handle velocity changes
Agent::broadcastSight(op);
return;
}
Sound snd = smart_dynamic_cast<Sound>(op);
if (snd.isValid()) {
std::vector<Root> args(op->getArgs());
assert(!args.empty());
if (snd->hasAttr("broadcast")) {
Agent::broadcastSound(smart_dynamic_cast<RootOperation>(args.front()));
}
}
Sight st = smart_dynamic_cast<Sight>(op);
if (st.isValid()) {
if (st->hasAttr("broadcast")) {
std::vector<Root> args(op->getArgs());
assert(!args.empty());
Agent::broadcastSight(smart_dynamic_cast<RootOperation>(args.front()));
}
}
Set s = smart_dynamic_cast<Set>(op);
if (s.isValid()) {
std::vector<Root> args(op->getArgs());
for (unsigned int A=0; A < args.size(); ++A) {
std::string eid = args[A]->getId();
RootEntity entity = m_server->getEntity(eid);
Root::const_iterator I = args[A]->begin();
for (; I != args[A]->end(); ++I) {
if ((I->first == "id") || (I->first == "parents") || (I->first == "objtype")) {
continue;
}
assert(I->first != "loc");
entity->setAttr(I->first, I->second);
}
}
Agent::broadcastSight(s);
}
Action act = smart_dynamic_cast<Action>(op);
if (act.isValid()) {
std::vector<Root> args(op->getArgs());
if (act->getParents().front() == "command") {
std::string cid = args[0]->getAttr("cid").asString();
if (cid == "socket-shutdown") {
std::string acc = args[0]->getAttr("acc").asString();
ClientConnection* cc = m_server->getConnectionForAccount(acc);
assert(cc);
cc->shutdown();
} else if (cid == "add-many-objects") {
m_server->addManyObjects(args[0]->getAttr("acc").asString());
} else if (cid == "set-world-time") {
/* double t = */ args[0]->getAttr("seconds").asFloat();
} else {
std::cerr << "unknown command " << cid << std::endl;
}
} // of command action case
} // of action case
}
|