View Javadoc

1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.oscar.io;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import net.sf.dexterim.oscar.entity.Flap;
25  import net.sf.dexterim.oscar.entity.Snac;
26  import net.sf.dexterim.oscar.entity.Word;
27  import net.sf.dexterim.oscar.server.GenericSnacResponse;
28  import net.sf.dexterim.oscar.server.Response;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * @author christoph
35   */
36  public class SnacParser implements MessageParser {
37  
38  	private Map delegates;
39  
40  	private Log log;
41  	
42  	public SnacParser() {
43  		log = LogFactory.getLog(getClass());
44  		
45  		delegates = new HashMap();
46  		register(0x0001, new GenericServiceControlsParser());
47  	}
48  
49  	public final void register(int family, SnacParserDelegate delegate) {
50  		delegates.put(new Integer(family), delegate);
51  	}
52  
53  	/*
54  	 * (non-Javadoc)
55  	 * 
56  	 * @see net.sf.dexterim.oscar.io.MessageParser#parse(net.sf.dexterim.oscar.io.Flap)
57  	 */
58  	public Response parse(Flap flap) throws ParserException {
59  		byte data[] = flap.getData();
60  
61  		byte familyArray[] = new byte[2];
62  		familyArray[0] = data[0];
63  		familyArray[1] = data[1];
64  
65  		int family = new Word(familyArray).getValue();
66  
67  		byte typeArray[] = new byte[2];
68  		typeArray[0] = data[2];
69  		typeArray[1] = data[3];
70  		
71  		int type = new Word(typeArray).getValue();
72  
73  		byte flagsArray[] = new byte[2];
74  		flagsArray[0] = data[4];
75  		flagsArray[1] = data[5];
76  		int flags = new Word(flagsArray).getValue();
77  
78  		byte requestArray[] = new byte[4];
79  		requestArray[0] = data[6];
80  		requestArray[1] = data[7];
81  		requestArray[2] = data[8];
82  		requestArray[3] = data[9];
83  		int requestId = new Word(requestArray).getValue();
84  
85  		byte snacData[] = new byte[data.length - 10];
86  		System.arraycopy(data, 10, snacData, 0, snacData.length);
87  		
88  		Snac snac = new Snac(family, type, flags);
89  		snac.setRequest(requestId);
90  		snac.setData(snacData);
91  
92  		SnacParserDelegate delegate = (SnacParserDelegate)delegates
93  				.get(new Integer(snac.getFamily()));
94  
95  		if (delegate == null) {
96  			if (log.isDebugEnabled()) {
97  				log.debug("No Parser for Family " + snac.getFamily() + " available");
98  			}
99  			
100 			return new GenericSnacResponse(snac);
101 		}
102 		else {
103 		return delegate.parse(snac);
104 		}
105 	}
106 }