@Override public void run() { try { for (; ; ) { long startMillis = System.currentTimeMillis(); try { Map<String, BotUpdateEvent> lastBotUpdateEvents; synchronized (LAST_BOT_UPDATE_EVENTS) { lastBotUpdateEvents = new HashMap<>(LAST_BOT_UPDATE_EVENTS); } session.pulse(lastBotUpdateEvents.values()); } catch (SessionEndedException e) { break; } catch (Exception e) { System.err.println("Exception occured for session " + session); e.printStackTrace(); } long duration = System.currentTimeMillis() - startMillis; if (duration > 20) { // System.out.println(">>> Slow Pulse (" + duration + " ms) for " + session + " -- " + // session.getMe()); } Thread.sleep(10); } } catch (InterruptedException e) { throw new RuntimeException(e); } synchronized (sessions) { sessions.remove(session.getPort()); } System.err.println("EQ Session ended, pulse thread exiting: " + session); }
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException { Log.initialize(); /* * Read spell data */ Map<Integer, SpellData> rawSpellData = new HashMap<>(); try (LineNumberReader reader = new LineNumberReader(new FileReader("spells_us.txt"))) { String line; while ((line = reader.readLine()) != null) { String[] fields = line.split("\\^"); SpellData sd = new SpellData(fields); rawSpellData.put(sd.getId(), sd); } } ItemDAO itemDAO = new FileItemDAO(); /* * Initialize sessions */ for (; ; ) { for (int port = 7777; port < 7784; port++) { try { synchronized (sessions) { if (!sessions.containsKey(port)) { final EverquestSession session = new DefaultEverquestSession( itemDAO, rawSpellData, new File("global.ini"), "localhost", port, "root", "8192"); sessions.put(port, session); session.setBotUpdateHandler( new EventHandler<BotUpdateEvent>() { @Override public void handle(BotUpdateEvent event) { synchronized (LAST_BOT_UPDATE_EVENTS) { LAST_BOT_UPDATE_EVENTS.put(event.getName(), event); // Remove any events older than 5 seconds for (Iterator<BotUpdateEvent> iterator = LAST_BOT_UPDATE_EVENTS.values().iterator(); iterator.hasNext(); ) { BotUpdateEvent e = iterator.next(); if (e.getEventAge() > 5000) { System.err.println("Removed old bot event: " + e.getName() + ": " + e); iterator.remove(); } } } } }); session.setCommandHandler( new EventHandler<ExternalCommandEvent>() { @Override public void handle(ExternalCommandEvent event) { String command = event.getCommand(); int colon = command.indexOf(":"); String targetSession = command.substring(0, colon).trim(); synchronized (sessions) { for (EverquestSession otherSession : sessions.values()) { if (targetSession.equalsIgnoreCase("all") || (targetSession.equalsIgnoreCase("group") && otherSession .getGroupMemberNames() .contains(session.getMe().getName())) || (targetSession.equalsIgnoreCase("others") && otherSession != session) || (targetSession.equalsIgnoreCase("zone") && otherSession.getZoneId() == session.getZoneId()) || (targetSession.equalsIgnoreCase("!zone") && otherSession.getZoneId() != session.getZoneId()) || (targetSession.equalsIgnoreCase("near") && otherSession.getZoneId() == session.getZoneId() && otherSession.getMeSpawn() != null && session.getMeSpawn() != null && otherSession .getMeSpawn() .getDistance( session.getMeSpawn().getX(), session.getMeSpawn().getY()) < 200) || (otherSession.getMeSpawn() != null && otherSession .getMeSpawn() .getName() .equalsIgnoreCase(targetSession))) { otherSession.addExternalCommand(command.substring(colon + 1).trim()); } } } } }); new Thread(new PulseThread(session), "PulseThread(" + session + ")").start(); } } } catch (SocketTimeoutException e) { // Occurs when connection is rejection when already connected System.err.println( "EQ running on port " + port + ", but did not accept connection. Another AutoEQ running?"); } catch (ConnectException e) { // Occurs when the port is not listening (ie, no EQ), we ignore this } catch (Exception e) { System.err.println("Exception while creating session"); e.printStackTrace(); } } Thread.sleep(10000); } }