1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.task; |
24 |
|
25 |
import java.net.InetSocketAddress; |
26 |
import java.net.Socket; |
27 |
|
28 |
import org.jsampler.CC; |
29 |
|
30 |
import static org.jsampler.JSI18n.i18n; |
31 |
|
32 |
|
33 |
/** |
34 |
* Establishes the connection to LinuxSampler used by the LS Console. |
35 |
* @author Grigor Iliev |
36 |
*/ |
37 |
public class LSConsoleConnect extends EnhancedTask<Socket> { |
38 |
private Socket oldSocket; |
39 |
|
40 |
/** Creates a new instance of <code>LSConsoleConnect</code>. */ |
41 |
public |
42 |
LSConsoleConnect() { this(null); } |
43 |
|
44 |
/** |
45 |
* Creates a new instance of <code>LSConsoleConnect</code>. |
46 |
* @param oldSocket The socket to close. |
47 |
*/ |
48 |
public |
49 |
LSConsoleConnect(Socket oldSocket) { |
50 |
setTitle("LSConsoleConnect_task"); |
51 |
setDescription(i18n.getMessage("LSConsoleConnect.description")); |
52 |
setSilent(true); |
53 |
this.oldSocket = oldSocket; |
54 |
} |
55 |
|
56 |
/** The entry point of the task. */ |
57 |
@Override |
58 |
public void |
59 |
exec() throws Exception { |
60 |
String address = CC.getCurrentServer().getAddress(); |
61 |
int port = CC.getCurrentServer().getPort(); |
62 |
|
63 |
if(oldSocket != null) oldSocket.close(); |
64 |
InetSocketAddress sockAddr = new InetSocketAddress(address, port); |
65 |
|
66 |
int soTimeout = 10000; |
67 |
Socket sock = new Socket(); |
68 |
sock.bind(null); |
69 |
sock.connect(sockAddr, soTimeout); |
70 |
sock.setSoTimeout(soTimeout); |
71 |
sock.setTcpNoDelay(true); |
72 |
|
73 |
setResult(sock); |
74 |
} |
75 |
} |