Crossfire JXClient, Trunk
PacketWatcher.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.scripts;
24 
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.regex.Pattern;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
38 public class PacketWatcher {
39 
43  @NotNull
44  private final Collection<String> commands = new HashSet<>();
45 
49  @NotNull
51 
55  @NotNull
57 
62  @Nullable
63  private Pattern pattern;
64 
70  @NotNull
72 
73  @Override
74  public void process(@NotNull final String command, @NotNull final ClientSocketMonitorCommand args) {
75  if (matchesCommand(command)) {
76  final String args2 = args.getMonitorCommand();
77  if (args2.isEmpty()) {
78  scriptProcess.commandSent("watch "+command);
79  } else {
80  scriptProcess.commandSent("watch "+command+" "+args2);
81  }
82  }
83  }
84 
85  };
86 
93  this.crossfireServerConnection = crossfireServerConnection;
94  this.scriptProcess = scriptProcess;
96  }
97 
102  public void destroy() {
103  if (pattern != null) {
104  pattern = null;
106  }
107  }
108 
112  private void rebuildPattern() {
113  final StringBuilder sb = new StringBuilder();
114  for (String command : commands) {
115  sb.append(Pattern.quote(command));
116  sb.append(".*|");
117  }
118  final int length = sb.length();
119  if (length <= 0) {
120  if (pattern != null) {
121  pattern = null;
123  }
124  } else {
125  if (pattern == null) {
127  }
128  sb.setLength(length-1);
129  pattern = Pattern.compile(sb.toString());
130  }
131  }
132 
137  public void addCommand(@NotNull final String command) {
138  if (commands.add(command)) {
139  rebuildPattern();
140  }
141  }
142 
147  public void removeCommand(@NotNull final String command) {
148  if (commands.remove(command)) {
149  rebuildPattern();
150  }
151  }
152 
158  private boolean matchesCommand(@NotNull final CharSequence command) {
159  return pattern != null && pattern.matcher(command).matches();
160  }
161 
162 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.scripts.ScriptProcess.commandSent
void commandSent(@NotNull String cmd)
com.realtime.crossfire.jxclient.scripts.PacketWatcher.matchesCommand
boolean matchesCommand(@NotNull final CharSequence command)
Definition: PacketWatcher.java:158
com.realtime.crossfire.jxclient.scripts.PacketWatcher.removeCommand
void removeCommand(@NotNull final String command)
Definition: PacketWatcher.java:147
com.realtime.crossfire.jxclient.scripts.PacketWatcher.commands
final Collection< String > commands
Definition: PacketWatcher.java:44
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.scripts.PacketWatcher.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: PacketWatcher.java:50
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.addPacketWatcherListener
void addPacketWatcherListener(@NotNull ReceivedPacketListener listener)
com.realtime.crossfire.jxclient.scripts.PacketWatcher
Definition: PacketWatcher.java:38
com.realtime.crossfire.jxclient.scripts.PacketWatcher.addCommand
void addCommand(@NotNull final String command)
Definition: PacketWatcher.java:137
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.scripts.PacketWatcher.PacketWatcher
PacketWatcher(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ScriptProcess scriptProcess)
Definition: PacketWatcher.java:92
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.server.server.ReceivedPacketListener
Definition: ReceivedPacketListener.java:33
com.realtime.crossfire.jxclient.scripts.PacketWatcher.rebuildPattern
void rebuildPattern()
Definition: PacketWatcher.java:112
com.realtime.crossfire.jxclient.server.server
Definition: DefaultServerConnection.java:23
com.realtime.crossfire
com.realtime.crossfire.jxclient.scripts.ScriptProcess
Definition: ScriptProcess.java:31
com.realtime
com.realtime.crossfire.jxclient.server.socket
Definition: ClientSocket.java:23
com
com.realtime.crossfire.jxclient.scripts.PacketWatcher.scriptProcess
final ScriptProcess scriptProcess
Definition: PacketWatcher.java:56
com.realtime.crossfire.jxclient.scripts.PacketWatcher.receivedPacketListener
final ReceivedPacketListener receivedPacketListener
Definition: PacketWatcher.java:71
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.removePacketWatcherListener
void removePacketWatcherListener(@NotNull ReceivedPacketListener listener)
com.realtime.crossfire.jxclient.server.socket.ClientSocketMonitorCommand
Definition: ClientSocketMonitorCommand.java:8
com.realtime.crossfire.jxclient.scripts.PacketWatcher.pattern
Pattern pattern
Definition: PacketWatcher.java:63
com.realtime.crossfire.jxclient.scripts.PacketWatcher.destroy
void destroy()
Definition: PacketWatcher.java:102