@RequestMapping(value = VIDEO_SEARCH_PATH, method = RequestMethod.GET) public @ResponseBody String[] searchVideo( @RequestParam(value = "username") String uName, @RequestParam(value = "video") String videoHash, HttpServletResponse response) { System.out.println("Search from:" + uName); if (!user_vidNameMap.containsKey(uName)) { response.setStatus(402); // client not connected return null; } Set<String> users = vidName_UserMap.get(videoHash); if (users == null) { System.out.println("Srearching main server\n"); try { users = masterService.psSearch(hostAdder, videoHash); } catch (Exception e) { System.err.println(e.getMessage()); return null; } if (users == null) return null; if (vidName_UserMap.containsKey(videoHash)) { vidName_UserMap.get(videoHash).addAll(users); } else { Set<String> s = new HashSet<String>(); s.addAll(users); vidName_UserMap.put(videoHash, s); } } else { Iterator<String> it = users.iterator(); while (it.hasNext()) { String temp = it.next(); if (!activeUsers.contains(temp)) { it.remove(); } } } System.out.println("Search result : " + Arrays.asList(users.toArray(new String[0]))); // String [] a = new String[] return users.toArray(new String[0]); }
private void removeUser(String user) { int count = 0; if (user_vidNameMap.containsKey(user)) { Set<String> vids = user_vidNameMap.get(user); Iterator<String> it = vids.iterator(); while (it.hasNext()) { removeFromvidName_UserMap(user, it.next()); } vids.clear(); user_vidNameMap.remove(user); } activeUsers.remove(user); int ans = masterService.psDisConnectClient(hostAdder, user); while (ans != ACK) { if (count > 10) throw new RuntimeException("Time out in removing user from MS"); reconnectToMS(); ans = masterService.psDisConnectClient(hostAdder, user); count++; } System.out.println("User " + user + " removed."); }
@Scheduled(fixedDelay = SCHEDULED_CHECK) public void checkActiveClients() { Set<String> users = user_vidNameMap.keySet(); Iterator<String> it = users.iterator(); long time = System.currentTimeMillis(); int count = 0; while (it.hasNext()) { count++; String user = it.next(); if (user == null) continue; if (userAliveMap.containsKey(user)) { if (userAliveMap.get(user).longValue() < time) { removeUser(user); userAliveMap.remove(user); } } else { throw new RuntimeException("user in user-vid map but not in user-alive map"); } } // System.out.println("Debug: Scheduled Check count:"+count+" user // count:"+user_vidNameMap.size()); System.gc(); }