#ifndef lock_server_cache_h
#define lock_server_cache_h

#include <string>
#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
#include "lock_server.h"


typedef struct lock_data {
	int nonce;
	std::string hostname;
	int type;
	int cid;
	std::map<int, int> * acq_done_map ;//client id to last nonce
	std::map<int, int> * rel_done_map;
};

typedef struct request_info {
	int nonce;
	std::string hostname;
	int type;
	int cid;
};

class lock_server_cache : lock_server {
	private:

		int queries;
		pthread_mutex_t q_lock;

	

		
		rpcc cl;
		int acq_index; //indicates where the revoker thread is in the acquire queue
		//int acq_end;
		int rel_index;
		std::map<std::string, lock_data> * locks_map;
		
		std::list<std::pair<std::string, request_info> > * acq_list;
		std::vector<std::pair<std::string, request_info> > * acq_vector;
		
		std::list<std::pair<std::string, request_info> > * rel_list;
		std::vector<std::pair<std::string, request_info> > * rel_vector;
		
		
		
		const static int FREE = 0; //the peer whose info is in the map released it
		const static int REVOKING = 1; //sent revoke query to the peer whose info is in the map
		const static int NONE = 2; //no one has it, pretty much at the beginning
		const static int LOCKED = 3; //the client with the info holds the lock
		
		const static int DEBUG = 0;
		
		void ce(std::pair<std::string, lock_data> p);
 public:
  lock_server_cache();
  lock_protocol::status stat(std::string, int &);
  void revoker();
  void granter();
  lock_protocol::status acquire(std::string hostname, std::string name, int nonce, int pid, int &r);
  lock_protocol::status release(std::string hostname, std::string name, int nonce, int pid, int &r);
};

#endif
