You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.2 KiB

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by itsmy_000 on 12.09.2016.
*/
public class SocketMaps{
public enum Events{
CREATING, PLAY, PAUSE, REWIND, DISCONNECT
}
public enum Roles{
CREATOR, LISTNER
}
public enum Keys{
SOCKETID, PARTYID, EVENT, ROLE, CREATOR, CLIENT
}
private static Map<Integer, Client> SID;
private static Map<Integer, Integer> PID;
private static Map<Integer, Events> EID;
private static Map<Integer, Client.Info> info;
private static Map<Integer, Roles> role;
private static Map<Integer, Integer> creator; // 1 - SID, 2 - PID
private static Integer counter;
static void initMaps(){
counter = 0;
SID = new HashMap<>();
PID = new HashMap<>();
EID = new HashMap<>();
info = new HashMap<>();
role = new HashMap<>();
creator = new HashMap<>();
}
static Integer addClient(Client c, Client.Info i){
SID.put(counter, c);
info.put(counter, i);
PID.put(counter,null);
EID.put(counter,Events.CREATING);
return counter++;
}
static void removeClient(Integer socketid) throws ctException{
containSID(socketid);
Integer partyid = getPID(socketid);
if(getCreator(partyid) == socketid){
creator.remove(partyid);
}
SID.remove(socketid);
PID.remove(socketid);
EID.remove(socketid);
role.remove(socketid);
info.remove(socketid);
}
static void setPID(Integer socketid, Integer partyid) throws ctException{
containSID(socketid);
PID.put(socketid,partyid);
}
static void setEID(Integer socketid, Events event) throws ctException {
containSID(socketid);
EID.put(socketid, event);
}
static void setRole(Integer socketid, Roles r) throws ctException {
containSID(socketid);
role.put(socketid, r);
}
static void setCreator(Integer partyid, Integer creatorsid){
if(!creator.containsKey(partyid)) {
creator.put(partyid,creatorsid);
}
}
static Client getClient(Integer socketid) throws ctException{
containSID(socketid);
return SID.get(socketid);
}
static Client.Info getInfo(Integer socketid) throws ctException{
containSID(socketid);
return info.get(socketid);
}
static Integer getPID(Integer socketid) throws ctException {
containSID(socketid);
return PID.get(socketid);
}
static Roles getRole(Integer socketid) throws ctException {
containSID(socketid);
return role.get(socketid);
}
static Events getEID(Integer socketid) throws ctException {
containSID(socketid);
return EID.get(socketid);
}
static ArrayList<Integer> getAllClients(){
ArrayList<Integer> list = new ArrayList<>();
for (Integer integer : SID.keySet()) {
list.add(integer);
}
return list;
}
static ArrayList<Integer> getAllParty(Integer partyid){
ArrayList<Integer> list = new ArrayList<>();
Console.debug("Enter to getAllParty()");
for (int i = 0; i < PID.size(); i++) {
Map.Entry<Integer, Integer> temp = PID.entrySet().iterator().next();
if(temp.getValue() == partyid) {
list.add(temp.getKey());
Console.debug("getAllParty()|"+temp.getKey()+"|"+temp.getValue());
}
}
return list;
}
static Integer getCreator(Integer partyid){
int noCreator = 0;
try{
containPID(partyid);
return creator.get(partyid);
}
catch (ctException temp){
temp = null;
return noCreator;
}
}
private static void containSID(Integer socketid) throws ctException{
if(!SID.containsKey(socketid)){
throw new ctException("Not finded SID Key!");
}
}
private static void containPID(Integer partyid) throws ctException{
if(!PID.containsKey(partyid)){
throw new ctException("Not finded PID Key!");
}
}
}