private static Iterable<Slave> slaves(int... ids) { List<Slave> slaves = new ArrayList<>(ids.length); for (int id : ids) { Slave slaveMock = mock(Slave.class); when(slaveMock.getServerId()).thenReturn(id); slaves.add(slaveMock); } return slaves; }
@Test public void roundRobinWithTwoSlavesAndPushFactorOne() { // Given SlavePriority roundRobin = SlavePriorities.roundRobin(); // When Slave slave1 = roundRobin.prioritize(slaves(2, 3)).iterator().next(); Slave slave2 = roundRobin.prioritize(slaves(2, 3)).iterator().next(); // Then assertEquals(2, slave1.getServerId()); assertEquals(3, slave2.getServerId()); }
public void openWhenClose(Master master, Slave slave, String key) { Permission p = slave.getPermission(this); if (p == null || !p.isValid()) { mPermissionsListener.error(BluetoothClient.DONT_HAVE_PERMISSION); return; } mBluetoothClient = BluetoothClient.getInstance(mContext, this); if (!mBluetoothClient.isSupported()) { mBluetoothListener.bluetoothNotSupported(); return; } else if (!mBluetoothClient.isEnabled()) { mBluetoothListener.enableBluetooth(); return; } mBluetoothClient.executeOpenDoorWhenClose(p.getKey(), master.getName(), slave.getId()); }
public void run() { Socket s = slave.getSocket(); DataInputStream input; try { input = new DataInputStream(s.getInputStream()); // First job, is to read the period so we can create the clock uid = input.readInt(); System.out.println("read in my uid : " + uid); ///////////////////////////////////////////////////////////////// // LOAD the game //////////////////////////////////////////////////////////////// String FILE_TO_RECEIVED = "Load_From.xml"; File file = new File(FILE_TO_RECEIVED); int FILE_SIZE = input.readInt(); if (FILE_SIZE != 0) { // receive file System.out.println("received file size : " + FILE_SIZE); byte[] data = new byte[FILE_SIZE]; input.readFully(data); // convert array of bytes into file FileOutputStream fileOuputStream = new FileOutputStream(file); fileOuputStream.write(data); fileOuputStream.close(); try { LoadOldGame loadXML = new LoadOldGame(file); net.setBoardData(loadXML.getBoardData()); } catch (JDOMException | XMLException e) { // e.printStackTrace(); } } else { throw new FileNotSentError(); } readyToStart = true; // now the players can start trying to do things if (frame == null) { System.out.println("Frame is null"); } frame.startMyClient(uid); while (locked) { recieveMassUpdate(input); frame.repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } catch (IOException e) { // e.printStackTrace(); } }
public void readAllPermissions(Master master, Slave slave, String permissionKey) { mBluetoothClient = BluetoothClient.getInstance(mContext, this); if (!mBluetoothClient.isSupported()) { mBluetoothListener.bluetoothNotSupported(); return; } else if (!mBluetoothClient.isEnabled()) { mBluetoothListener.enableBluetooth(); return; } mBluetoothClient.executeReadAllPermissions(master.getName(), slave.getId(), permissionKey); }
/** Opens a door if the user has a valid permission. */ public void openDoor(Master master, Slave slave) { Permission permission = getValidPermission(master, slave); if (permission == null) { mPermissionsListener.error(BluetoothClient.DONT_HAVE_PERMISSION); return; } mBluetoothClient = BluetoothClient.getInstance(mContext, this); if (!mBluetoothClient.isSupported()) { mBluetoothListener.bluetoothNotSupported(); return; } else if (!mBluetoothClient.isEnabled()) { mBluetoothListener.enableBluetooth(); return; } mMasterListener.doorOpening(); mBluetoothClient.executeOpenDoor(permission, master.getId(), slave.getId()); }
private Permission getValidPermission(Master master, Slave slave) { HashMap<Integer, Permission> permissions = master.getPermissions(this); if (permissions == null || permissions.size() == 0) { return null; } Permission permission = permissions.get(0); if (permission == null || Permission.getType(permission.getType()) != Permission.ADMIN_PERMISSION) { permission = null; Object[] permissionsArray = permissions.values().toArray(); for (int i = 0; i < permissionsArray.length; i++) { Permission p = (Permission) permissionsArray[i]; // If it is valid and (the permission is associated with the same slave id OR it is // for all slaves) if (p.isValid() && (p.getSlaveId() == slave.getId() || p.getSlaveId() == Slave.ALL_SLAVES)) { permission = (Permission) permissionsArray[i]; break; } } } return permission; }
public void processOperation(Operation op, RemoteFile file) { boolean isPossible = true; if (op.equals(Operation.READ)) { Slave avaliableSlave = null; for (Slave s : SlaveGroup.slaves) { if (!s.isBusy()) avaliableSlave = s; } for (Slave s : SlaveGroup.slaves) { if (s.isBusy() && s.getRemoteFile().getId() == file.getId() && !s.getCurrentOperation().equals(Operation.READ)) { // Coloca na fila waitingOperations.add(new WaitingOperation(file, op)); isPossible = false; } } if (avaliableSlave != null && isPossible) avaliableSlave.processOperation(op, file); } if (op.equals(Operation.WRITE) || op.equals(Operation.CREATE) || op.equals(Operation.REMOVE)) { Slave avaliableSlave = null; for (Slave s : SlaveGroup.slaves) { if (!s.isBusy()) avaliableSlave = s; } for (Slave s : SlaveGroup.slaves) { if (s.isBusy() && s.getRemoteFile().getId() == file.getId()) { waitingOperations.add(new WaitingOperation(file, op)); isPossible = false; } } if (avaliableSlave != null && isPossible) { avaliableSlave.processOperation(op, file); } } }