// notify_interest_done send a CLIENTAGENT_DONE_INTEREST_RESP to the // interest operation's caller, if one has been set. void Client::notify_interest_done(uint16_t interest_id, channel_t caller) { if(caller == 0) { return; } DatagramPtr resp = Datagram::create(caller, m_channel, CLIENTAGENT_DONE_INTEREST_RESP); resp->add_channel(m_channel); resp->add_uint16(interest_id); route_datagram(resp); }
// notify_interest_done send a CLIENTAGENT_DONE_INTEREST_RESP to the // interest operation's caller, if one has been set. void Client::notify_interest_done(const InterestOperation* iop) { if(iop->m_callers.size() == 0) { return; } DatagramPtr resp = Datagram::create(iop->m_callers, m_channel, CLIENTAGENT_DONE_INTEREST_RESP); resp->add_channel(m_channel); resp->add_uint16(iop->m_interest_id); route_datagram(resp); }
void DBStateServer::handle_set_fields(DatagramIterator &dgi) { doid_t do_id = dgi.read_doid(); if(m_loading.find(do_id) != m_loading.end()) { // Ignore this message for now, it'll be bounced back to us // from the loading object if it succeeds or fails at loading. return; } uint16_t field_count = dgi.read_uint16(); FieldValues db_fields; for(uint16_t i = 0; i < field_count; ++i) { uint16_t field_id = dgi.read_uint16(); const Field* field = g_dcf->get_field_by_id(field_id); if(!field) { m_log->warning() << "Received invalid field with id " << field_id << " in SetFields.\n"; return; } if(field->has_keyword("db")) { dgi.unpack_field(field, db_fields[field]); } else { dgi.skip_field(field); } } if(db_fields.size() > 0) { m_log->trace() << "Forwarding SetFields on object with id " << do_id << " to database.\n"; DatagramPtr dg = Datagram::create(m_db_channel, do_id, DBSERVER_OBJECT_SET_FIELDS); dg->add_doid(do_id); dg->add_uint16(db_fields.size()); for(const auto& it : db_fields) { dg->add_uint16(it.first->get_id()); dg->add_data(it.second); } route_datagram(dg); } }
bool DistributedObject::handle_one_get(DatagramPtr out, uint16_t field_id, bool succeed_if_unset, bool is_subfield) { const Field *field = m_dclass->get_field_by_id(field_id); if(!field) { m_log->error() << "Received get_field for field: " << field_id << ", not valid for class: " << m_dclass->get_name() << ".\n"; return false; } m_log->trace() << "Handling query for '" << field->get_name() << "'.\n"; const MolecularField *molecular = field->as_molecular(); if(molecular) { int n = molecular->get_num_fields(); out->add_uint16(field_id); for(int i = 0; i < n; ++i) { if(!handle_one_get(out, molecular->get_field(i)->get_id(), succeed_if_unset, true)) { return false; } } return true; } if(m_required_fields.count(field)) { if(!is_subfield) { out->add_uint16(field_id); } out->add_data(m_required_fields[field]); } else if(m_ram_fields.count(field)) { if(!is_subfield) { out->add_uint16(field_id); } out->add_data(m_ram_fields[field]); } else { return succeed_if_unset; } return true; }
void DistributedObject::append_required_data(DatagramPtr dg, bool client_only, bool also_owner) { dg->add_doid(m_do_id); dg->add_location(m_parent_id, m_zone_id); dg->add_uint16(m_dclass->get_id()); size_t field_count = m_dclass->get_num_fields(); for(size_t i = 0; i < field_count; ++i) { const Field *field = m_dclass->get_field(i); if(field->has_keyword("required") && !field->as_molecular() && (!client_only || field->has_keyword("broadcast") || field->has_keyword("clrecv") || (also_owner && field->has_keyword("ownrecv")))) { dg->add_data(m_required_fields[field]); } } }
void DistributedObject::append_other_data(DatagramPtr dg, bool client_only, bool also_owner) { if(client_only) { vector<const Field*> broadcast_fields; for(auto it = m_ram_fields.begin(); it != m_ram_fields.end(); ++it) { if(it->first->has_keyword("broadcast") || it->first->has_keyword("clrecv") || (also_owner && it->first->has_keyword("ownrecv"))) { broadcast_fields.push_back(it->first); } } dg->add_uint16(broadcast_fields.size()); for(auto it = broadcast_fields.begin(); it != broadcast_fields.end(); ++it) { dg->add_uint16((*it)->get_id()); dg->add_data(m_ram_fields[*it]); } } else { dg->add_uint16(m_ram_fields.size()); for(auto it = m_ram_fields.begin(); it != m_ram_fields.end(); ++it) { dg->add_uint16(it->first->get_id()); dg->add_data(it->second); } } }
void DBStateServer::handle_set_field(DatagramIterator &dgi) { doid_t do_id = dgi.read_doid(); if(m_loading.find(do_id) != m_loading.end()) { // Ignore this message for now, it'll be bounced back to us // from the loading object if it succeeds or fails at loading. return; } uint16_t field_id = dgi.read_uint16(); const Field* field = g_dcf->get_field_by_id(field_id); if(field && field->has_keyword("db")) { m_log->trace() << "Forwarding SetField for field \"" << field->get_name() << "\" on object with id " << do_id << " to database.\n"; DatagramPtr dg = Datagram::create(m_db_channel, do_id, DBSERVER_OBJECT_SET_FIELD); dg->add_doid(do_id); dg->add_uint16(field_id); dg->add_data(dgi.read_remainder()); route_datagram(dg); } }
// handle_datagram is the handler for datagrams received from the Astron cluster void Client::handle_datagram(DatagramHandle in_dg, DatagramIterator &dgi) { lock_guard<recursive_mutex> lock(m_client_lock); if(is_terminated()) { return; } channel_t sender = dgi.read_channel(); if(sender == m_channel) { return; // ignore messages from ourselves } uint16_t msgtype = dgi.read_uint16(); switch(msgtype) { case CLIENTAGENT_EJECT: { uint16_t reason = dgi.read_uint16(); string error_string = dgi.read_string(); send_disconnect(reason, error_string); return; } break; case CLIENTAGENT_DROP: { handle_drop(); return; } break; case CLIENTAGENT_SET_STATE: { m_state = (ClientState)dgi.read_uint16(); } break; case CLIENTAGENT_ADD_INTEREST: { uint32_t context = m_next_context++; Interest i; build_interest(dgi, false, i); handle_add_interest(i, context); add_interest(i, context, sender); } break; case CLIENTAGENT_ADD_INTEREST_MULTIPLE: { uint32_t context = m_next_context++; Interest i; build_interest(dgi, true, i); handle_add_interest(i, context); add_interest(i, context, sender); } break; case CLIENTAGENT_REMOVE_INTEREST: { uint32_t context = m_next_context++; uint16_t id = dgi.read_uint16(); Interest &i = m_interests[id]; handle_remove_interest(id, context); remove_interest(i, context, sender); } break; case CLIENTAGENT_SET_CLIENT_ID: { if(m_channel != m_allocated_channel) { unsubscribe_channel(m_channel); } m_channel = dgi.read_channel(); subscribe_channel(m_channel); } break; case CLIENTAGENT_SEND_DATAGRAM: { DatagramPtr forward = Datagram::create(); forward->add_data(dgi.read_string()); forward_datagram(forward); } break; case CLIENTAGENT_OPEN_CHANNEL: { subscribe_channel(dgi.read_channel()); } break; case CLIENTAGENT_CLOSE_CHANNEL: { unsubscribe_channel(dgi.read_channel()); } break; case CLIENTAGENT_ADD_POST_REMOVE: { add_post_remove(m_allocated_channel, dgi.read_datagram()); } break; case CLIENTAGENT_CLEAR_POST_REMOVES: { clear_post_removes(m_allocated_channel); } break; case CLIENTAGENT_DECLARE_OBJECT: { doid_t do_id = dgi.read_doid(); uint16_t dc_id = dgi.read_uint16(); if(m_declared_objects.find(do_id) != m_declared_objects.end()) { m_log->warning() << "Received object declaration for previously declared object " << do_id << ".\n"; return; } DeclaredObject obj; obj.id = do_id; obj.dcc = g_dcf->get_class_by_id(dc_id); m_declared_objects[do_id] = obj; } break; case CLIENTAGENT_UNDECLARE_OBJECT: { doid_t do_id = dgi.read_doid(); if(m_declared_objects.find(do_id) == m_declared_objects.end()) { m_log->warning() << "Received undeclare object for unknown object " << do_id << ".\n"; return; } m_declared_objects.erase(do_id); } break; case CLIENTAGENT_SET_FIELDS_SENDABLE: { doid_t do_id = dgi.read_doid(); uint16_t field_count = dgi.read_uint16(); unordered_set<uint16_t> fields; for(uint16_t i{}; i < field_count; ++i) { fields.insert(dgi.read_uint16()); } m_fields_sendable[do_id] = fields; } break; case CLIENTAGENT_ADD_SESSION_OBJECT: { doid_t do_id = dgi.read_doid(); if(m_session_objects.find(do_id) != m_session_objects.end()) { m_log->warning() << "Received add session object for existing session object " << do_id << ".\n"; return; } m_log->debug() << "Added session object with id " << do_id << ".\n"; m_session_objects.insert(do_id); } break; case CLIENTAGENT_REMOVE_SESSION_OBJECT: { doid_t do_id = dgi.read_doid(); if(m_session_objects.find(do_id) == m_session_objects.end()) { m_log->warning() << "Received remove session object for non-session object " << do_id << ".\n"; return; } m_log->debug() << "Removed session object with id " << do_id << ".\n"; m_session_objects.erase(do_id); } break; case CLIENTAGENT_GET_NETWORK_ADDRESS: { DatagramPtr resp = Datagram::create(sender, m_channel, CLIENTAGENT_GET_NETWORK_ADDRESS_RESP); resp->add_uint32(dgi.read_uint32()); // Context resp->add_string(get_remote_address()); resp->add_uint16(get_remote_port()); resp->add_string(get_local_address()); resp->add_uint16(get_local_port()); route_datagram(resp); } break; case STATESERVER_OBJECT_SET_FIELD: { doid_t do_id = dgi.read_doid(); if(!lookup_object(do_id)) { if(try_queue_pending(do_id, in_dg)) { return; } m_log->warning() << "Received server-side field update for unknown object " << do_id << ".\n"; return; } if(sender != m_channel) { uint16_t field_id = dgi.read_uint16(); handle_set_field(do_id, field_id, dgi); } } break; case STATESERVER_OBJECT_SET_FIELDS: { doid_t do_id = dgi.read_doid(); if(!lookup_object(do_id)) { if(try_queue_pending(do_id, in_dg)) { return; } m_log->warning() << "Received server-side multi-field update for unknown object " << do_id << ".\n"; return; } if(sender != m_channel) { uint16_t num_fields = dgi.read_uint16(); handle_set_fields(do_id, num_fields, dgi); } } break; case STATESERVER_OBJECT_DELETE_RAM: { doid_t do_id = dgi.read_doid(); m_log->trace() << "Received DeleteRam for object with id " << do_id << "\n."; if(!lookup_object(do_id)) { if(try_queue_pending(do_id, in_dg)) { return; } m_log->warning() << "Received server-side object delete for unknown object " << do_id << ".\n"; return; } if(m_session_objects.find(do_id) != m_session_objects.end()) { // We have to erase the object from our session_objects here, because // the object has already been deleted and we don't want it to be deleted // again in the client's destructor. m_session_objects.erase(do_id); stringstream ss; ss << "The session object with id " << do_id << " has been unexpectedly deleted."; send_disconnect(CLIENT_DISCONNECT_SESSION_OBJECT_DELETED, ss.str()); return; } if(m_seen_objects.find(do_id) != m_seen_objects.end()) { handle_remove_object(do_id); m_seen_objects.erase(do_id); } if(m_owned_objects.find(do_id) != m_owned_objects.end()) { handle_remove_ownership(do_id); m_owned_objects.erase(do_id); } m_historical_objects.insert(do_id); m_visible_objects.erase(do_id); } break; case STATESERVER_OBJECT_ENTER_OWNER_WITH_REQUIRED_OTHER: case STATESERVER_OBJECT_ENTER_OWNER_WITH_REQUIRED: { doid_t do_id = dgi.read_doid(); doid_t parent = dgi.read_doid(); zone_t zone = dgi.read_zone(); uint16_t dc_id = dgi.read_uint16(); m_owned_objects.insert(do_id); if(m_visible_objects.find(do_id) == m_visible_objects.end()) { VisibleObject obj; obj.id = do_id; obj.parent = parent; obj.zone = zone; obj.dcc = g_dcf->get_class_by_id(dc_id); m_visible_objects[do_id] = obj; } bool with_other = (msgtype == STATESERVER_OBJECT_ENTER_OWNER_WITH_REQUIRED_OTHER); handle_add_ownership(do_id, parent, zone, dc_id, dgi, with_other); } break; case STATESERVER_OBJECT_ENTER_LOCATION_WITH_REQUIRED: case STATESERVER_OBJECT_ENTER_LOCATION_WITH_REQUIRED_OTHER: { doid_t do_id = dgi.read_doid(); doid_t parent = dgi.read_doid(); zone_t zone = dgi.read_zone(); for(auto& it : m_pending_interests) { InterestOperation *interest_operation = it.second; if(interest_operation->m_parent == parent && interest_operation->m_zones.find(zone) != interest_operation->m_zones.end()) { interest_operation->queue_datagram(in_dg); // Add the DoId to m_pending_objects, because while it's not an object // from opening the interest, we should begin queueing messages for it m_pending_objects.emplace(do_id, it.first); return; } } // Object entrance doesn't pertain to any pending iop, // so seek back to where we started and handle it normally dgi.seek_payload(); dgi.skip(sizeof(channel_t) + sizeof(uint16_t)); // sender + msgtype bool with_other = (msgtype == STATESERVER_OBJECT_ENTER_LOCATION_WITH_REQUIRED_OTHER); handle_object_entrance(dgi, with_other); } break; case STATESERVER_OBJECT_ENTER_INTEREST_WITH_REQUIRED: case STATESERVER_OBJECT_ENTER_INTEREST_WITH_REQUIRED_OTHER: { uint32_t request_context = dgi.read_uint32(); auto it = m_pending_interests.find(request_context); if(it == m_pending_interests.end()) { m_log->warning() << "Received object entrance into interest with unknown context " << request_context << ".\n"; return; } m_pending_objects.emplace(dgi.read_doid(), request_context); it->second->queue_expected(in_dg); if(it->second->is_ready()) { it->second->finish(); } return; } break; case STATESERVER_OBJECT_GET_ZONES_COUNT_RESP: { uint32_t context = dgi.read_uint32(); // using doid_t because <max_objects_in_zones> == <max_total_objects> doid_t count = dgi.read_doid(); auto it = m_pending_interests.find(context); if(it == m_pending_interests.end()) { m_log->error() << "Received GET_ZONES_COUNT_RESP for unknown context " << context << ".\n"; return; } it->second->set_expected(count); if(it->second->is_ready()) { it->second->finish(); } } break; case STATESERVER_OBJECT_CHANGING_LOCATION: { doid_t do_id = dgi.read_doid(); if(try_queue_pending(do_id, in_dg)) { // We received a generate for this object, and the generate is sitting in a pending iop // we'll just store this dg under the m_pending_datagrams queue on the iop return; } doid_t n_parent = dgi.read_doid(); zone_t n_zone = dgi.read_zone(); bool disable = true; for(const auto& it : m_interests) { const Interest& i = it.second; for(const auto& it2 : i.zones) { if(it2 == n_zone) { disable = false; break; } } } if(m_visible_objects.find(do_id) != m_visible_objects.end()) { m_visible_objects[do_id].parent = n_parent; m_visible_objects[do_id].zone = n_zone; } else { // We don't actually *see* this object, we're receiving this // message as a fluke. return; } if(disable && m_owned_objects.find(do_id) == m_owned_objects.end()) { if(m_session_objects.find(do_id) != m_session_objects.end()) { stringstream ss; ss << "The session object with id " << do_id << " has unexpectedly left interest."; send_disconnect(CLIENT_DISCONNECT_SESSION_OBJECT_DELETED, ss.str()); return; } handle_remove_object(do_id); m_seen_objects.erase(do_id); m_historical_objects.insert(do_id); m_visible_objects.erase(do_id); } else { handle_change_location(do_id, n_parent, n_zone); } } break; case STATESERVER_OBJECT_CHANGING_OWNER: { doid_t do_id = dgi.read_doid(); channel_t n_owner = dgi.read_channel(); dgi.skip(sizeof(channel_t)); // don't care about the old owner if(n_owner == m_channel) { // We should already own this object, nothing changes and we // might get another enter_owner message. return; } if(m_owned_objects.find(do_id) == m_owned_objects.end()) { m_log->error() << "Received ChangingOwner for unowned object with id " << do_id << ".\n"; return; } if(m_seen_objects.find(do_id) == m_seen_objects.end()) { if(m_session_objects.find(do_id) != m_session_objects.end()) { stringstream ss; ss << "The session object with id " << do_id << " has unexpectedly left ownership."; send_disconnect(CLIENT_DISCONNECT_SESSION_OBJECT_DELETED, ss.str()); return; } handle_remove_ownership(do_id); m_owned_objects.erase(do_id); m_historical_objects.insert(do_id); m_visible_objects.erase(do_id); } } break; default: m_log->error() << "Recv'd unknown server msgtype " << msgtype << "\n."; } }
// add_interest will start a new interest operation and retrieve all the objects an interest // from the server, subscribing to each zone in the interest. If the interest already // exists, the interest will be updated with the new zones passed in by the argument. void Client::add_interest(Interest &i, uint32_t context, channel_t caller) { unordered_set<zone_t> new_zones; for(const auto& it : i.zones) { if(lookup_interests(i.parent, it).empty()) { new_zones.insert(it); } } if(m_interests.find(i.id) != m_interests.end()) { // This is an already-open interest that is actually being altered. // Therefore, we need to delete the objects that the client can see // through this interest only. Interest previous_interest = m_interests[i.id]; unordered_set<zone_t> killed_zones; for(const auto& it : previous_interest.zones) { if(lookup_interests(previous_interest.parent, it).size() > 1) { // An interest other than the altered one can see this parent/zone, // so we don't care about it. continue; } // If we've gotten here: parent,*it is unique, so if the new interest // doesn't cover it, we add it to the killed zones. if(i.parent != previous_interest.parent || i.zones.find(it) == i.zones.end()) { killed_zones.insert(it); } } // Now that we know what zones to kill, let's get to it: close_zones(previous_interest.parent, killed_zones); } m_interests[i.id] = i; if(new_zones.empty()) { // We aren't requesting any new zones with this operation, so don't // bother firing off a State Server request. Instead, let the client // know we're already done: notify_interest_done(i.id, caller); handle_interest_done(i.id, context); return; } uint32_t request_context = m_next_context++; InterestOperation *iop = new InterestOperation(this, m_client_agent->m_interest_timeout, i.id, context, request_context, i.parent, new_zones, caller); m_pending_interests.emplace(request_context, iop); DatagramPtr resp = Datagram::create(); resp->add_server_header(i.parent, m_channel, STATESERVER_OBJECT_GET_ZONES_OBJECTS); resp->add_uint32(request_context); resp->add_doid(i.parent); resp->add_uint16(new_zones.size()); for(const auto& it : new_zones) { resp->add_zone(it); subscribe_channel(location_as_channel(i.parent, it)); } route_datagram(resp); }
void DBStateServer::handle_get_all_resp(DatagramIterator& dgi) { uint32_t db_context = dgi.read_uint32(); if(!is_expected_context(db_context)) { return; } // Get the datagram from the db_context DatagramPtr dg = m_context_datagrams[db_context]; m_context_datagrams.erase(db_context); // Check to make sure the datagram is appropriate DatagramIterator check_dgi = DatagramIterator(dg); uint16_t resp_type = check_dgi.get_msg_type(); if(resp_type != STATESERVER_OBJECT_GET_ALL_RESP) { if(resp_type == STATESERVER_OBJECT_GET_FIELD_RESP) { m_log->warning() << "Received GetFieldResp, but expecting GetAllResp." << std::endl; } else if(resp_type == STATESERVER_OBJECT_GET_FIELDS_RESP) { m_log->warning() << "Received GetFieldsResp, but expecting GetAllResp." << std::endl; } return; } // Get do_id from datagram check_dgi.seek_payload(); check_dgi.skip(sizeof(channel_t) + sizeof(doid_t)); // skip over sender and context to do_id; doid_t do_id = check_dgi.read_doid(); // Remove cached loading operation if(m_inactive_loads[do_id].size() > 1) { m_inactive_loads[do_id].erase(db_context); } else { m_inactive_loads.erase(do_id); } m_log->trace() << "Received GetAllResp from database." << std::endl; // If object not found, just cleanup the context map if(dgi.read_bool() != true) { return; // Object not found } // Read object class uint16_t dc_id = dgi.read_uint16(); if(!dc_id) { m_log->error() << "Received object from database with unknown dclass" << " - id:" << dc_id << std::endl; return; } const Class* r_class = g_dcf->get_class_by_id(dc_id); // Get fields from database UnorderedFieldValues required_fields; FieldValues ram_fields; if(!unpack_db_fields(dgi, r_class, required_fields, ram_fields)) { m_log->error() << "Error while unpacking fields from database." << std::endl; return; } // Add class to response dg->add_uint16(r_class->get_id()); // Add required fields to datagram int dcc_field_count = r_class->get_num_fields(); for(int i = 0; i < dcc_field_count; ++i) { const Field *field = r_class->get_field(i); if(!field->as_molecular() && field->has_keyword("required")) { auto req_it = required_fields.find(field); if(req_it != required_fields.end()) { dg->add_data(req_it->second); } else { dg->add_data(field->get_default_value()); } } } // Add ram fields to datagram dg->add_uint16(ram_fields.size()); for(const auto& it : ram_fields) { dg->add_uint16(it.first->get_id()); dg->add_data(it.second); } // Send response back to caller route_datagram(dg); }
void DBStateServer::handle_get_fields(channel_t sender, DatagramIterator &dgi) { uint32_t r_context = dgi.read_uint32(); doid_t r_do_id = dgi.read_doid(); uint16_t field_count = dgi.read_uint16(); if(is_activated_object(r_do_id)) { return; } m_log->trace() << "Received GetFields for inactive object with id " << r_do_id << std::endl; // Read requested fields from datagram std::vector<const Field*> db_fields; // Ram|required db fields in request std::vector<const Field*> ram_fields; // Ram|required but not-db fields in request for(uint16_t i = 0; i < field_count; ++i) { uint16_t field_id = dgi.read_uint16(); const Field* field = g_dcf->get_field_by_id(field_id); if(!field) { DatagramPtr dg = Datagram::create(sender, r_do_id, STATESERVER_OBJECT_GET_FIELDS_RESP); dg->add_uint32(r_context); dg->add_uint8(false); route_datagram(dg); } else if(field->has_keyword("ram") || field->has_keyword("required")) { if(field->has_keyword("db")) { db_fields.push_back(field); } else { ram_fields.push_back(field); } } } if(db_fields.size()) { // Get context for db query uint32_t db_context = m_next_context++; // Prepare reponse datagram if(m_context_datagrams.find(db_context) == m_context_datagrams.end()) { m_context_datagrams[db_context] = Datagram::create(sender, r_do_id, STATESERVER_OBJECT_GET_FIELDS_RESP); } m_context_datagrams[db_context]->add_uint32(r_context); m_context_datagrams[db_context]->add_bool(true); m_context_datagrams[db_context]->add_uint16(ram_fields.size() + db_fields.size()); for(const auto& it : ram_fields) { m_context_datagrams[db_context]->add_uint16(it->get_id()); m_context_datagrams[db_context]->add_data(it->get_default_value()); } // Send query to database DatagramPtr dg = Datagram::create(m_db_channel, r_do_id, DBSERVER_OBJECT_GET_FIELDS); dg->add_uint32(db_context); dg->add_doid(r_do_id); dg->add_uint16(db_fields.size()); for(const auto& it : db_fields) { dg->add_uint16(it->get_id()); } route_datagram(dg); } else { // If no database fields exist DatagramPtr dg = Datagram::create(sender, r_do_id, STATESERVER_OBJECT_GET_FIELDS_RESP); dg->add_uint32(r_context); dg->add_bool(true); dg->add_uint16(ram_fields.size()); for(const auto& it : ram_fields) { dg->add_uint16(it->get_id()); dg->add_data(it->get_default_value()); } route_datagram(dg); } }
void DistributedObject::handle_datagram(DatagramHandle, DatagramIterator &dgi) { channel_t sender = dgi.read_channel(); uint16_t msgtype = dgi.read_uint16(); switch(msgtype) { case STATESERVER_DELETE_AI_OBJECTS: { if(m_ai_channel != dgi.read_channel()) { m_log->warning() << " received reset for wrong AI channel.\n"; break; // Not my AI! } annihilate(sender); break; } case STATESERVER_OBJECT_DELETE_RAM: { if(m_do_id != dgi.read_doid()) { break; // Not meant for me! } // Delete object annihilate(sender); break; } case STATESERVER_OBJECT_DELETE_CHILDREN: { doid_t r_do_id = dgi.read_doid(); if(r_do_id == m_do_id) { delete_children(sender); } else if(r_do_id == m_parent_id) { annihilate(sender, false); } break; } case STATESERVER_OBJECT_SET_FIELD: { if(m_do_id != dgi.read_doid()) { break; // Not meant for me! } handle_one_update(dgi, sender); break; } case STATESERVER_OBJECT_SET_FIELDS: { if(m_do_id != dgi.read_doid()) { break; // Not meant for me! } uint16_t field_count = dgi.read_uint16(); for(int16_t i = 0; i < field_count; ++i) { if(!handle_one_update(dgi, sender)) { break; } } break; } case STATESERVER_OBJECT_CHANGING_AI: { doid_t r_parent_id = dgi.read_doid(); channel_t new_channel = dgi.read_channel(); m_log->trace() << "Received ChangingAI notification from " << r_parent_id << ".\n"; if(r_parent_id != m_parent_id) { m_log->warning() << "Received AI channel from " << r_parent_id << " but my parent_id is " << m_parent_id << ".\n"; break; } if(m_ai_explicitly_set) { break; } handle_ai_change(new_channel, sender, false); break; } case STATESERVER_OBJECT_SET_AI: { channel_t new_channel = dgi.read_channel(); m_log->trace() << "Updating AI to " << new_channel << ".\n"; handle_ai_change(new_channel, sender, true); break; } case STATESERVER_OBJECT_GET_AI: { m_log->trace() << "Received AI query from " << sender << ".\n"; DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_AI_RESP); dg->add_uint32(dgi.read_uint32()); // Get context dg->add_doid(m_do_id); dg->add_channel(m_ai_channel); route_datagram(dg); break; } case STATESERVER_OBJECT_GET_AI_RESP: { dgi.read_uint32(); // Discard context doid_t r_parent_id = dgi.read_doid(); m_log->trace() << "Received AI query response from " << r_parent_id << ".\n"; if(r_parent_id != m_parent_id) { m_log->warning() << "Received AI channel from " << r_parent_id << " but my parent_id is " << m_parent_id << ".\n"; break; } channel_t new_ai = dgi.read_channel(); if(m_ai_explicitly_set) { break; } handle_ai_change(new_ai, sender, false); break; } case STATESERVER_OBJECT_CHANGING_LOCATION: { doid_t child_id = dgi.read_doid(); doid_t new_parent = dgi.read_doid(); zone_t new_zone = dgi.read_zone(); doid_t r_do_id = dgi.read_doid(); zone_t r_zone = dgi.read_zone(); if(new_parent == m_do_id) { if(m_do_id == r_do_id) { if(new_zone == r_zone) { break; // No change, so do nothing. } auto &children = m_zone_objects[r_zone]; children.erase(child_id); if(children.empty()) { m_zone_objects.erase(r_zone); } } m_zone_objects[new_zone].insert(child_id); DatagramPtr dg = Datagram::create(child_id, m_do_id, STATESERVER_OBJECT_LOCATION_ACK); dg->add_doid(m_do_id); dg->add_zone(new_zone); route_datagram(dg); } else if(r_do_id == m_do_id) { auto &children = m_zone_objects[r_zone]; children.erase(child_id); if(children.empty()) { m_zone_objects.erase(r_zone); } } else { m_log->warning() << "Received changing location from " << child_id << " for " << r_do_id << ", but my id is " << m_do_id << ".\n"; } break; } case STATESERVER_OBJECT_LOCATION_ACK: { doid_t r_parent_id = dgi.read_doid(); zone_t r_zone_id = dgi.read_zone(); if(r_parent_id != m_parent_id) { m_log->trace() << "Received location acknowledgement from " << r_parent_id << " but my parent_id is " << m_parent_id << ".\n"; } else if(r_zone_id != m_zone_id) { m_log->trace() << "Received location acknowledgement for zone " << r_zone_id << " but my zone_id is " << m_zone_id << ".\n"; } else { m_log->trace() << "Parent acknowledged my location change.\n"; m_parent_synchronized = true; } break; } case STATESERVER_OBJECT_SET_LOCATION: { doid_t new_parent = dgi.read_doid(); zone_t new_zone = dgi.read_zone(); m_log->trace() << "Updating location to Parent: " << new_parent << ", Zone: " << new_zone << ".\n"; handle_location_change(new_parent, new_zone, sender); break; } case STATESERVER_OBJECT_GET_LOCATION: { uint32_t context = dgi.read_uint32(); DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_LOCATION_RESP); dg->add_uint32(context); dg->add_doid(m_do_id); dg->add_location(m_parent_id, m_zone_id); route_datagram(dg); break; } case STATESERVER_OBJECT_GET_LOCATION_RESP: { // This case occurs immediately after object creation. // A parent expects to receive a location_resp from each // of its pre-existing children. if(dgi.read_uint32() != STATESERVER_CONTEXT_WAKE_CHILDREN) { m_log->warning() << "Received unexpected GetLocationResp from " << dgi.read_uint32() << ".\n"; break; } // Get DOID of our child doid_t doid = dgi.read_doid(); // Get location doid_t r_parent = dgi.read_doid(); zone_t r_zone = dgi.read_zone(); // Update the child count if(r_parent == m_do_id) { m_zone_objects[r_zone].insert(doid); } break; } case STATESERVER_OBJECT_GET_ALL: { uint32_t context = dgi.read_uint32(); if(dgi.read_doid() != m_do_id) { return; // Not meant for this object! } DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_ALL_RESP); dg->add_uint32(context); append_required_data(dg); append_other_data(dg); route_datagram(dg); break; } case STATESERVER_OBJECT_GET_FIELD: { uint32_t context = dgi.read_uint32(); if(dgi.read_doid() != m_do_id) { return; // Not meant for this object! } uint16_t field_id = dgi.read_uint16(); DatagramPtr raw_field = Datagram::create(); bool success = handle_one_get(raw_field, field_id); DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_FIELD_RESP); dg->add_uint32(context); dg->add_bool(success); if(success) { dg->add_data(raw_field); } route_datagram(dg); break; } case STATESERVER_OBJECT_GET_FIELDS: { uint32_t context = dgi.read_uint32(); if(dgi.read_doid() != m_do_id) { return; // Not meant for this object! } uint16_t field_count = dgi.read_uint16(); // Read our requested fields into a sorted set set<uint16_t> requested_fields; for(int i = 0; i < field_count; ++i) { uint16_t field_id = dgi.read_uint16(); if(!requested_fields.insert(field_id).second) { const dclass::Field* field = m_dclass->get_field_by_id(field_id); if(field != nullptr) { // If it is null, handle_one_get will produce a warning for us later m_log->warning() << "Received duplicate field '" << field->get_name() << "' in get_fields.\n"; } } } // Try to get the values for all the fields bool success = true; uint16_t fields_found = 0; DatagramPtr raw_fields = Datagram::create(); for(auto it = requested_fields.begin(); it != requested_fields.end(); ++it) { uint16_t field_id = *it; uint16_t length = raw_fields->size(); if(!handle_one_get(raw_fields, field_id, true)) { success = false; break; } if(raw_fields->size() > length) { fields_found++; } } // Send get fields response DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_FIELDS_RESP); dg->add_uint32(context); dg->add_bool(success); if(success) { dg->add_uint16(fields_found); dg->add_data(raw_fields); } route_datagram(dg); break; } case STATESERVER_OBJECT_SET_OWNER: { channel_t new_owner = dgi.read_channel(); m_log->trace() << "Updating owner to " << new_owner << "...\n"; if(new_owner == m_owner_channel) { m_log->trace() << "... owner is the same, do nothing.\n"; return; } if(m_owner_channel) { m_log->trace() << "... broadcasting changing owner...\n"; DatagramPtr dg = Datagram::create(m_owner_channel, sender, STATESERVER_OBJECT_CHANGING_OWNER); dg->add_doid(m_do_id); dg->add_channel(new_owner); dg->add_channel(m_owner_channel); route_datagram(dg); } m_owner_channel = new_owner; if(new_owner) { m_log->trace() << "... sending owner entry...\n"; send_owner_entry(new_owner); } m_log->trace() << "... updated owner.\n"; break; } case STATESERVER_OBJECT_GET_ZONE_OBJECTS: case STATESERVER_OBJECT_GET_ZONES_OBJECTS: { uint32_t context = dgi.read_uint32(); doid_t queried_parent = dgi.read_doid(); m_log->trace() << "Handling get_zones_objects with parent '" << queried_parent << "'" << ". My id is " << m_do_id << " and my parent is " << m_parent_id << ".\n"; uint16_t zone_count = 1; if(msgtype == STATESERVER_OBJECT_GET_ZONES_OBJECTS) { zone_count = dgi.read_uint16(); } if(queried_parent == m_parent_id) { // Query was relayed from parent! See if we match any of the zones // and if so, reply: for(uint16_t i = 0; i < zone_count; ++i) { if(dgi.read_zone() == m_zone_id) { // The parent forwarding this request down to us may or may // not yet know about our presence (and therefore have us // included in the count that it sent to the interested // peer). If we are included in this count, we reply with a // normal interest entry. If not, we reply with a standard // location entry and allow the interested peer to resolve // the difference itself. if(m_parent_synchronized) { send_interest_entry(sender, context); } else { send_location_entry(sender); } break; } } } else if(queried_parent == m_do_id) { doid_t child_count = 0; // Start datagram to relay to children DatagramPtr child_dg = Datagram::create(parent_to_children(m_do_id), sender, STATESERVER_OBJECT_GET_ZONES_OBJECTS); child_dg->add_uint32(context); child_dg->add_doid(queried_parent); child_dg->add_uint16(zone_count); // Get all zones requested for(int i = 0; i < zone_count; ++i) { zone_t zone = dgi.read_zone(); child_count += m_zone_objects[zone].size(); child_dg->add_zone(zone); } // Reply to requestor with count of objects expected DatagramPtr count_dg = Datagram::create(sender, m_do_id, STATESERVER_OBJECT_GET_ZONES_COUNT_RESP); count_dg->add_uint32(context); count_dg->add_doid(child_count); route_datagram(count_dg); // Bounce the message down to all children and have them decide // whether or not to reply. // TODO: Is this really that efficient? if(child_count > 0) { route_datagram(child_dg); } } break; } // zones in Astron don't have meaning to the cluster itself // as such, there is no table of zones to query in the network // instead, a zone is said to be active if it has at least one object in it // to get the active zones, get the keys from m_zone_objects and dump them into a std::set<zone_t> // using an std::set ensures that no duplicate zones are sent // TODO: evaluate efficiency on large games with many DistributedObjects case STATESERVER_GET_ACTIVE_ZONES: { uint32_t context = dgi.read_uint32(); std::unordered_set<zone_t> keys; for(auto kv : m_zone_objects) { keys.insert(kv.first); } DatagramPtr dg = Datagram::create(sender, m_do_id, STATESERVER_GET_ACTIVE_ZONES_RESP); dg->add_uint32(context); dg->add_uint16(keys.size()); std::unordered_set<zone_t>::iterator it; for(it = keys.begin(); it != keys.end(); ++it) { dg->add_zone(*it); } route_datagram(dg); break; } default: if(msgtype < STATESERVER_MSGTYPE_MIN || msgtype > STATESERVER_MSGTYPE_MAX) { m_log->warning() << "Received unknown message of type " << msgtype << ".\n"; } else { m_log->trace() << "Ignoring stateserver message of type " << msgtype << ".\n"; } } }