View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Michael Lifshits
4    *  Copyright (C) 2008 Vlad Skarzhevskyy
5    *
6    *  Licensed to the Apache Software Foundation (ASF) under one
7    *  or more contributor license agreements.  See the NOTICE file
8    *  distributed with this work for additional information
9    *  regarding copyright ownership.  The ASF licenses this file
10   *  to you under the Apache License, Version 2.0 (the
11   *  "License"); you may not use this file except in compliance
12   *  with the License.  You may obtain a copy of the License at
13   *
14   *    http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing,
17   *  software distributed under the License is distributed on an
18   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   *  KIND, either express or implied.  See the License for the
20   *  specific language governing permissions and limitations
21   *  under the License.
22   *
23   *  @author vlads
24   *  @version $Id: EmulatorCommandReceiver.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
25   */
26  package com.intel.bluetooth;
27  
28  import java.io.File;
29  import java.io.FileWriter;
30  import java.io.IOException;
31  import java.io.OutputStreamWriter;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  import com.intel.bluetooth.emu.DeviceCommand;
38  
39  /**
40   * 
41   */
42  class EmulatorCommandReceiver extends Thread {
43  
44  	private EmulatorLocalDevice localDevice;
45  
46  	private boolean stoped = false;
47  
48  	EmulatorCommandReceiver(EmulatorLocalDevice localDevice) {
49  		super("BlueCoveEmulatorCommandReceiver");
50  		this.localDevice = localDevice;
51  	}
52  
53  	void shutdownReceiver() {
54  		stoped = true;
55  	}
56  
57  	public void run() {
58  		while (!stoped) {
59  			DeviceCommand cmd = localDevice.getDeviceManagerService().pollCommand(this.localDevice.getAddress());
60  			if (cmd == null) {
61  				break;
62  			}
63  			execute(cmd);
64  		}
65  	}
66  
67  	private void execute(DeviceCommand command) {
68  		switch (command.getType()) {
69  		case keepAlive:
70  			break;
71  		case chagePowerState:
72  			localDevice.setLocalDevicePower((Boolean) command.getParameters()[0]);
73  			break;
74  		case updateLocalDeviceProperties:
75  			localDevice.updateLocalDeviceProperties();
76  			break;
77  		case createThreadDumpStdOut:
78  			threadDump(false);
79  			break;
80  		case createThreadDumpFile:
81  			threadDump(true);
82  			break;
83  		case shutdownJVM:
84  			System.exit(0);
85  			break;
86  
87  		}
88  	}
89  
90  	static void threadDump(boolean useFile) {
91  		SimpleDateFormat fmt = new SimpleDateFormat("MM-dd_HH-mm-ss");
92  		OutputStreamWriter out = null;
93  		try {
94  			File file = null;
95  			if (useFile) {
96  				file = new File("ThreadDump-" + fmt.format(new Date()) + ".log");
97  				out = new FileWriter(file);
98  			} else {
99  				out = new OutputStreamWriter(System.out);
100 			}
101 			Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
102 			for (Iterator<Map.Entry<Thread, StackTraceElement[]>> iterator = traces.entrySet().iterator(); iterator
103 					.hasNext();) {
104 				Map.Entry<Thread, StackTraceElement[]> entry = iterator.next();
105 				Thread thread = entry.getKey();
106 				out.write("Thread= " + thread.getName() + " " + (thread.isDaemon() ? "daemon" : "") + " prio="
107 						+ thread.getPriority() + "id=" + thread.getId() + " " + thread.getState());
108 				out.write("\n");
109 
110 				StackTraceElement[] ste = entry.getValue();
111 				for (int i = 0; i < ste.length; i++) {
112 					out.write("\t");
113 					out.write(ste[i].toString());
114 					out.write("\n");
115 				}
116 				out.write("---------------------------------\n");
117 			}
118 			out.close();
119 			out = null;
120 			if (useFile) {
121 				System.err.println("Full ThreadDump created " + file.getAbsolutePath());
122 			}
123 		} catch (IOException ignore) {
124 		} finally {
125 			try {
126 				out.close();
127 			} catch (IOException ignore) {
128 			}
129 
130 		}
131 	}
132 }