

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//_/_/
//_/_/  This file is part of PSYCLONE version 2.1.1.260726 (26 July 2026)
//_/_/  
//_/_/  developed by
//_/_/  
//_/_/  Communicative Machines Ltd. 
//_/_/    http://cmlabs.com/
//_/_/  Copyright (c) 2026
//_/_/  
//_/_/  in collaboration with 
//_/_/  
//_/_/  Icelandic Institute for Intelligent Machines ses
//_/_/    Menntavegur 1
//_/_/    101 Reykjavik
//_/_/    ICELAND
//_/_/    http://www.iiim.is/
//_/_/
//_/_/  PSYCLONE 2.1 is released under the GNU Lesser General Public License 
//_/_/  (LGPL) version 3.0, and is subject to the following
//_/_/  
//_/_/  - CADIA Clause: The license granted in and to the software under this 
//_/_/    agreement is a limited-use license. The software may not be used in 
//_/_/    furtherance of: (i) intentionally causing bodily injury or severe 
//_/_/    emotional harm to any person; (ii) invading the personal privacy or 
//_/_/    violating the human rights of any person; or (iii) committing or 
//_/_/    preparing for any act of war. 
//_/_/    
//_/_/  A commercial license for PSYCLONE 2.1 is also available to 
//_/_/  any interested party from the copyright holder.
//_/_/  
//_/_/  You can redistribute this copy of PSYCLONE 2.1 and/or modify it under 
//_/_/  the terms of the GNU General Public License as published by the Free 
//_/_/  Software Foundation, either version 3 of the License, or (at your 
//_/_/  option) any later version.
//_/_/  
//_/_/  You should have received a copy of the GNU General Public License
//_/_/  and the GNU Lesser General Public License along with PSYCLONE 2.1.  
//_/_/  If not, see <http://www.gnu.org/licenses/>.
//_/_/  
//_/_/  PSYCLONE 2.1 is distributed in the hope that it will be useful,
//_/_/  but WITHOUT ANY WARRANTY; without even the implied warranty of
//_/_/  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//_/_/  GNU General Public License for more details.
//_/_/
//_/_/  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//_/_/  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//_/_/  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//_/_/  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
//_/_/  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//_/_/  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//_/_/  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//_/_/  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//_/_/  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//_/_/  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//_/_/  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//_/_/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#if !defined(_IDMANAGER_H_)
#define _IDMANAGER_H_

#pragma once

#include "MemoryManager.h"
#include "Utils.h"
#include "PsyTime.h"

#define RESERVE_COMPONENT_ID	1
#define RESERVE_TYPE_ID			2
#define RESERVE_TAG_ID		3
#define RESERVE_CONTEXT_ID		4
#define RESERVE_CRANK_ID		5
#define RESERVE_ID_COUNT		5

namespace	cmlabs{

class Networking;

struct IDReservation {
	uint32 size;
	uint32 cid;
	uint32 start;
	uint32 end;
	uint64 time;
	uint16 owner;
	uint8 type;
};

class IDManager;
class ReserveID {
	friend class IDManager;
public:
	ReserveID();
	~ReserveID();
	
	bool init(uint16 localID);

	uint32 reserveNextID();
	uint32 getNextID();
	uint32 getRemainingCount();
	double getRemainingPercent();
	IDReservation getNextReservationBlock();
	bool enterReservation(IDReservation& res);
	uint32 writeAllReservationsToMsg(DataMessage* msg);
	
private:
	utils::Mutex mutex;
	uint16 localID;
	uint8 type;
	uint32 blockSize;
	uint32 lastID;
	std::map<uint32, IDReservation> reservations;
	IDReservation currentReservation;
	IDReservation nextReservation;
};

class IDManager {
public:
	static uint32 MAXIDS[];
	static char IDNAMES[][10];

	IDManager();
	~IDManager();

	// Called by non-config nodes
	bool init(Networking* networking);
	// Only called on config node
	bool setup(Networking* networking, uint16 nodeCount);

	uint32 getNextComponentID();
	uint16 getNextTypeID();
	uint32 getNextTagID();
	uint16 getNextContextID();
	uint16 getNextCrankID();

	bool enterIDReservation(IDReservation& res);
	bool enterIDReservations(DataMessage* msg);

	bool checkSync();
	bool resyncAllReservations();

private:
	Networking* networking;
	ReserveID reserveIDs[RESERVE_ID_COUNT+1];

	uint8 syncIDReservation(IDReservation& res);
};

}

#include "Networking.h"

#endif // _IDMANAGER_H_
