View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library 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 GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.server;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.util.Queue;
26  
27  import org.indi.clientmessages.GetProperties;
28  import org.indi.objects.BlobVector;
29  import org.indi.objects.NumberVector;
30  import org.indi.objects.SwitchVector;
31  import org.indi.objects.TextVector;
32  import org.indi.reactor.BasicQueueHandler;
33  
34  /**
35   * Dipatches the parsed indi messages from the client to the device drivers. The
36   * messages are received from the parsing thread via a queue.
37   * 
38   * @author Dirk Hünniger
39   * 
40   */
41  public class Dispatcher extends BasicQueueHandler {
42      /**
43       * the indiserver to interact with
44       */
45      private final IndiServer server;
46  
47      /**
48       * class consturctor
49       * 
50       * @param queue
51       *                a queue to receive the parsed messages from the parsing
52       *                thread
53       * @param server
54       *                the indiserver knowing about the drivers to dispach the
55       *                received objects to.
56       */
57      public Dispatcher(Queue<Object> queue, IndiServer server) {
58  	super(queue);
59  	this.server = server;
60      }
61  
62      /**
63       * Copies an obeject deeply. (copying the whole graph of the object)
64       * 
65       * @param o
66       *                the object to be copied
67       * @return the copy of the object
68       */
69      private static Object deepCopy(Object o) {
70  	try {
71  	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
72  	    new ObjectOutputStream(baos).writeObject(o);
73  	    ByteArrayInputStream bais = new ByteArrayInputStream(baos
74  		    .toByteArray());
75  	    return new ObjectInputStream(bais).readObject();
76  	} catch (Exception e) {
77  	    e.printStackTrace();
78  	    return null;
79  	}
80      }
81  
82      /**
83       * called by the reactor whenever a new element is in the queue waiting to
84       * be dispatched
85       * 
86       * @param o
87       *                the object to be dispachted
88       */
89      @Override
90      public void onRead(Object o) {
91  	if (o instanceof GetProperties) {
92  	    for (BasicDevice d : this.server.drivers) {
93  		d.onGetProperties((GetProperties) deepCopy(o));
94  	    }
95  	}
96  	for (BasicDevice d : this.server.drivers) {
97  	    if (o instanceof SwitchVector) {
98  		d.onNew((SwitchVector) deepCopy(o));
99  	    }
100 	    if (o instanceof NumberVector) {
101 		d.onNew((NumberVector) deepCopy(o));
102 	    }
103 	    if (o instanceof TextVector) {
104 		d.onNew((TextVector) deepCopy(o));
105 	    }
106 	    if (o instanceof BlobVector) {
107 		d.onNew((BlobVector) deepCopy(o));
108 	    }
109 	}
110     }
111 }