/** Prijme spojeni od klienta */ public ServerThread accept() throws IOException { ServerThread st; st = new ServerThread(getServerSocket().accept()); st.setServer(this); st.start(); return st; }
@Override public void onCreate() { if (shouldRunAsServer()) { mDataServerBinder = new DataServerBinder(); mBluetoothReceiver = new BluetoothReceiver(); mAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mIsWorkAsGenerator) { IntentFilter bluetoothFilter = new IntentFilter(); bluetoothFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST); bluetoothFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); bluetoothFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(mBluetoothReceiver, bluetoothFilter); mAdapter.setName("Health Device"); if (!mAdapter.isEnabled()) { mAdapter.enable(); } if (mAdapter.getState() == BluetoothAdapter.STATE_ON) { mAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0); mAdapter.setDiscoverableTimeout(0); } } mServerThread = new ServerThread(); mServerThread.start(); } }
/* starts the socket server */ private void startServer(boolean isDaemon, int pause) { // try to connect, if no connection then start new server try { System.err.println("testing connection"); getClient().process("test"); } catch (Exception ioe) { System.err.println("no connection, so starting server"); // no connection, so start server server = new ServerThread( getModuleName() + "-server", getCodeCall() + " " + getPort() + " " + getParams(), isDaemon); server.start(); try { // pause to allow loading data for some modules Thread.sleep(pause); } catch (Exception e) { System.err.println(e); } // keep a reference to the process provided through the thread to be // able to destroy it when the module gets destroyed. Otherwise, // the process continues to run in the background even when the // module has been destroyed. p = server.getProcess(); return; } }
/** * Listen for incoming and outgoing requests. If the <code>serverEnabled</code> member is <code> * false</code> the server for incoming requests is not started. This starts the internal server * thread that processes messages. * * @throws SocketException when the transport is already listening for incoming/outgoing messages. * @throws IOException */ public synchronized void listen() throws java.io.IOException { if (server != null) { throw new SocketException("Port already listening"); } server = new ServerThread(); if (connectionTimeout > 0) { socketCleaner = new Timer(true); // run as daemon } server.setDaemon(true); server.start(); }