public static void main(String[] args) throws Exception { Properties properties = new Properties(); FileInputStream propFile = new FileInputStream("store.properties"); properties.load(propFile); final String id = properties.getProperty("ID"); final String placeForFiles = properties.getProperty("FilePlace"); int servicePort = Integer.parseInt(properties.getProperty("ServicePort")); int tcpPort = Integer.parseInt(properties.getProperty("TCPPort")); String ksName = properties.getProperty("KeyStore"); KeyStore ks = KeyStore.getInstance(properties.getProperty("KeyStoreType")); char[] password = properties.getProperty("Password").toCharArray(); char[] passwordForKey = properties.getProperty("PasswordForKey").toCharArray(); ks.load(new FileInputStream(ksName), password); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, passwordForKey); SSLContext context = SSLContext.getInstance("TLS"); context.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = context.getServerSocketFactory(); SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket(tcpPort); ServiceThread serviceThread = new ServiceThread(servicePort, id); serviceThread.start(); while (!serverSocket.isClosed()) { ClientThread client = new ClientThread(placeForFiles); SSLSocket socket = (SSLSocket) serverSocket.accept(); socket.startHandshake(); client.setSocket(socket); client.start(); } }
/** * 执行service线程 * * @param delegate * @param arg */ public void invokeServiceDelegate(String delegate, Object... arguments) { try { ServiceThread t = new ServiceThread(this, this); t.setDelegate(delegate); t.setArgs(arguments); t.start(); } catch (Exception ex) { throw new RuntimeException("Invoke service error, delegate: " + delegate, ex); } }
void start() { serviceThread = new ServiceThread("MetaWatch Service Thread"); serviceThread.start(); /* DEBUG */ String voltageFrequencyString = PreferenceManager.getDefaultSharedPreferences(this).getString("collectWatchVoltage", "0"); try { final int voltageFrequency = Integer.parseInt(voltageFrequencyString); if (voltageFrequency > 0) { AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); intent.putExtra("action_poll_voltage", "poll_voltage"); PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); long sleep = voltageFrequency * 60 * 1000; alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, sleep, sender); if (Preferences.logging) Log.d( MetaWatch.TAG, "MetaWatchService.start(): Set voltage reading every " + sleep + "ms"); } } catch (NumberFormatException nfe) { if (Preferences.logging) Log.e( MetaWatch.TAG, "MetaWatchService.start(): bad voltage frequency string '" + voltageFrequencyString + "'"); } }
/** * 运行 service 的缺省实现 * * @param t * @param arg * @return * @throws NoSuchMethodException * @throws SecurityException */ protected final boolean runService(Context t, ServiceThread srvOwner) { boolean ret = true; try { if (srvOwner != null && !StringUtil.isNullOrEmpty(srvOwner.getDelegate())) { String serviceDelegate = srvOwner.getDelegate(); Method m = this.getClass().getMethod(serviceDelegate, Object[].class); if (m != null) { m.invoke(this, srvOwner.getArgs()); } ret = true; } } catch (Exception ex) { throw new RuntimeException("runService error.", ex); } return ret; }
void startServices() { try { Throwable firstError = null; List<ServiceThread> threads = new ArrayList<ServiceThread>(); if (LOG.isDebugEnabled()) { LOG.debug("Begin parallel start"); } for (ServiceWithDependency sd : services.values()) { // start the service. If this fails that service // will be stopped and an exception raised ServiceThread st = new ServiceThread(sd); threads.add(st); } for (ServiceThread st : threads) { st.start(); } for (ServiceThread st : threads) { if (LOG.isDebugEnabled()) { LOG.debug("Waiting for service thread to join for " + st.getName()); } st.join(); if (st.error != null && firstError == null) { firstError = st.error; } } if (firstError != null) { throw ServiceStateException.convert(firstError); } if (LOG.isDebugEnabled()) { LOG.debug("End parallel start"); } } catch (InterruptedException e) { e.printStackTrace(); } }
@Override public void onDestroy() { disconnectExit(); super.onDestroy(); serviceThread.quit(); if (Preferences.logging) Log.d(MetaWatch.TAG, "MetaWatchService.onDestroy()"); PreferenceManager.getDefaultSharedPreferences(context) .unregisterOnSharedPreferenceChangeListener(prefChangeListener); Monitors.stop(this); removeNotification(); notifyClients(); mClients.clear(); }