Browse Source

Updated Parser to use better callback method.

git-svn-id: http://svn.dmdirc.com/trunk@11 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Shane Mc Cormack 17 years ago
parent
commit
5be34129a7
1 changed files with 47 additions and 25 deletions
  1. 47
    25
      src/dmdirc/parser/IRCParser.java

+ 47
- 25
src/dmdirc/parser/IRCParser.java View File

24
 
24
 
25
 import java.io.*;
25
 import java.io.*;
26
 import java.net.*;
26
 import java.net.*;
27
-
28
-interface IDataInEvent { public void onDataIn(IRCParser parser, String data); }
29
-interface IDataOutEvent { public void onDataOut(IRCParser parser, String data); }
27
+import java.util.ArrayList;
30
 
28
 
31
 public class IRCParser implements Runnable {
29
 public class IRCParser implements Runnable {
32
 
30
 
46
 	private boolean IsFirst = true;
44
 	private boolean IsFirst = true;
47
 
45
 
48
 	// Events
46
 	// Events
49
-	public interface GeneralIRCEvent { public void GeneralEvent(String sName); }
50
-	public interface LogEvent { public void LogEvent(String sName, String sData); }
47
+	public interface IMOTDEnd { public void onMOTDEnd(IRCParser tParser); }
48
+	public interface IDataIn { public void onDataIn(IRCParser tParser, String sData); }
49
+	public interface IDataOut { public void onDataOut(IRCParser tParser, String sData); }
51
 	class AllEvents {
50
 	class AllEvents {
52
-		GeneralIRCEvent EndOfMOTD = null;
53
-		LogEvent DataIn = null;
54
-		LogEvent DataOut = null;
55
-
56
-		String sEndOfMOTD = "";
57
-		String sDataIn = "";
58
-		String sDataOut = "";
51
+		ArrayList<IMOTDEnd> EndOfMOTD = new ArrayList<IMOTDEnd>();
52
+		ArrayList<IDataIn> DataIn = new ArrayList<IDataIn>();
53
+		ArrayList<IDataOut> DataOut = new ArrayList<IDataOut>();
59
 	}
54
 	}
60
 	public AllEvents cb = new AllEvents();
55
 	public AllEvents cb = new AllEvents();
61
 
56
 
62
-	public void SetCallback(String sType, Object eMethod) throws Exception { try { SetCallback(sType,sType,eMethod); } catch (Exception e) { throw e; } }
63
-	public void SetCallback(String sType, String sName, Object eMethod) throws Exception {
64
-		if (sName.equals("")) { sName = sType; }
65
-		sType = sType.toLowerCase();
66
-		if (sType.equals("endofmotd")) { cb.EndOfMOTD = (GeneralIRCEvent)eMethod; cb.sEndOfMOTD = sName; }
67
-		else if (sType.equals("datain")) { cb.DataIn = (LogEvent)eMethod; cb.sDataIn = sName; }
68
-		else if (sType.equals("dataout")) { cb.DataOut = (LogEvent)eMethod; cb.sDataOut = sName; }
69
-		else { throw new Exception("No such callback '"+sType+"'");}
57
+	public void AddMOTDEnd(Object eMethod) { cb.EndOfMOTD.add((IMOTDEnd)eMethod); }
58
+	public void DelMOTDEnd(Object eMethod) { 
59
+		for (int i = 0; i < cb.EndOfMOTD.size(); i++) {
60
+			if (eMethod.equals((Object)cb.EndOfMOTD.get(i))) { cb.EndOfMOTD.remove(i); break; }
61
+		}
62
+	}
63
+	private void CallMOTDEnd() { 
64
+		for (int i = 0; i < cb.EndOfMOTD.size(); i++) {
65
+			// IMOTDEnd(cb.EndOfMOTD.get(i)).OnMOTDEnd(this);
66
+			cb.EndOfMOTD.get(i).onMOTDEnd(this);
67
+		}
68
+	}
69
+
70
+	public void AddDataIn(Object eMethod) { cb.DataIn.add((IDataIn)eMethod); }
71
+	public void DelDataIn(Object eMethod) { 
72
+		for (int i = 0; i < cb.DataIn.size(); i++) {
73
+			if (eMethod.equals((Object)cb.DataIn.get(i))) { cb.DataIn.remove(i); break; }
74
+		}
75
+	}
76
+	private void CallDataIn(String data) { 
77
+		for (int i = 0; i < cb.DataIn.size(); i++) {
78
+			cb.DataIn.get(i).onDataIn(this, data);
79
+		}
80
+	}
81
+
82
+	public void AddDataOut(Object eMethod) { cb.DataOut.add((IDataOut)eMethod); }
83
+	public void DelDataOut(Object eMethod) { 
84
+		for (int i = 0; i < cb.DataOut.size(); i++) {
85
+			if (eMethod.equals((Object)cb.DataOut.get(i))) { cb.DataOut.remove(i); break; }
86
+		}
87
+	}
88
+	private void CallDataOut(String data) { 
89
+		for (int i = 0; i < cb.DataOut.size(); i++) {
90
+			cb.DataOut.get(i).onDataOut(this, data);
91
+		}
70
 	}
92
 	}
71
 
93
 
72
 	// Constructor.
94
 	// Constructor.
73
-	IRCParser () {}
95
+	IRCParser () { }
74
 
96
 
75
 	public void connect(String sHost) throws Exception {
97
 	public void connect(String sHost) throws Exception {
76
 		try {
98
 		try {
77
-			connect(sHost,6667);		
99
+			connect(sHost,6667);
78
 		} catch (Exception e) {
100
 		} catch (Exception e) {
79
 			throw e;
101
 			throw e;
80
 		}
102
 		}
146
 	
168
 	
147
 	// Our Method
169
 	// Our Method
148
 	private void SendString(String line) {
170
 	private void SendString(String line) {
149
-		if (!cb.sDataOut.equals("")) {cb.DataOut.LogEvent(cb.sDataOut,line);} // Ugly++
171
+		CallDataOut(line);
150
 		out.printf("%s\r\n",line);
172
 		out.printf("%s\r\n",line);
151
 	}
173
 	}
152
 
174
 
155
 		String sParam = token[token.length-1];
177
 		String sParam = token[token.length-1];
156
 
178
 
157
 		int nParam;
179
 		int nParam;
158
-		if (!cb.sDataIn.equals("")) {cb.DataIn.LogEvent(cb.sDataIn,line);} // Ugly++
180
+		CallDataIn(line);
159
 
181
 
160
 		try {nParam = Integer.parseInt(token[1]);} catch (Exception e) { nParam = -1;}
182
 		try {nParam = Integer.parseInt(token[1]);} catch (Exception e) { nParam = -1;}
161
 
183
 
188
 
210
 
189
 	private void ProcessEndOfMOTD(String sParam,String token[]) {
211
 	private void ProcessEndOfMOTD(String sParam,String token[]) {
190
 		// Process EndOfMOTD
212
 		// Process EndOfMOTD
191
-		if (!cb.sEndOfMOTD.equals("")) {cb.EndOfMOTD.GeneralEvent(cb.sEndOfMOTD);} // Ugly++
213
+		CallMOTDEnd();
192
 	}
214
 	}
193
 
215
 
194
 
216
 

Loading…
Cancel
Save