Skip to content

Commit c0e45f1

Browse files
committed
Switch to named unix socket instead of tcp socket
1 parent 2c1cf03 commit c0e45f1

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/src/com/tns/NativeScriptSyncService.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.tns;
22

33
import java.io.BufferedReader;
4+
import java.io.Closeable;
45
import java.io.DataInputStream;
56
import java.io.File;
67
import java.io.FileFilter;
@@ -20,6 +21,8 @@
2021
import android.content.Context;
2122
import android.content.pm.ApplicationInfo;
2223
import android.content.pm.PackageManager.NameNotFoundException;
24+
import android.net.LocalServerSocket;
25+
import android.net.LocalSocket;
2326

2427
public class NativeScriptSyncService
2528
{
@@ -30,14 +33,16 @@ public class NativeScriptSyncService
3033

3134
private static Logger logger;
3235
private final Context context;
33-
private ServerThread serverThread;
34-
private Thread javaServerThread;
36+
3537
private final String syncPath;
3638
private final String fullSyncPath;
3739
private final String removedSyncPath;
3840
private final File fullSyncDir;
3941
private final File syncDir;
4042
private final File removedSyncDir;
43+
44+
private LocalServerSocketThread localServerThread;
45+
private Thread localServerJavaThread;
4146

4247
public NativeScriptSyncService(Logger logger, Context context)
4348
{
@@ -54,8 +59,7 @@ public NativeScriptSyncService(Logger logger, Context context)
5459

5560
public void sync()
5661
{
57-
58-
if (logger.isEnabled())
62+
if (logger != null && logger.isEnabled())
5963
{
6064
logger.write("Sync is enabled:");
6165
logger.write("Sync path : " + syncPath);
@@ -83,17 +87,17 @@ public void sync()
8387
}
8488
}
8589

86-
private class ServerThread implements Runnable
90+
private class LocalServerSocketThread implements Runnable
8791
{
8892
private volatile boolean running;
89-
private final int port;
93+
private final String name;
9094

9195
private ListenerWorker commThread;
92-
private ServerSocket serverSocket;
96+
private LocalServerSocket serverSocket;
9397

94-
public ServerThread(int port)
98+
public LocalServerSocketThread(String name)
9599
{
96-
this.port = port;
100+
this.name = name;
97101
this.running = false;
98102
}
99103

@@ -115,31 +119,30 @@ public void run()
115119
running = true;
116120
try
117121
{
118-
serverSocket = new ServerSocket(this.port);
122+
serverSocket = new LocalServerSocket(this.name);
119123
while (running)
120124
{
121-
Socket socket = serverSocket.accept();
122-
123-
commThread = new ListenerWorker(socket.getInputStream());
125+
LocalSocket socket = serverSocket.accept();
126+
commThread = new ListenerWorker(socket.getInputStream(), socket);
124127
new Thread(commThread).start();
125128
}
126129
}
127130
catch (IOException e)
128131
{
129-
e.printStackTrace();
132+
e.printStackTrace();
130133
}
131134
}
132135
}
133136

134-
135137
private class ListenerWorker implements Runnable
136138
{
137-
private BufferedReader reader;
138139
private final DataInputStream input;
140+
private Closeable socket;
139141

140-
public ListenerWorker(InputStream inputStream)
142+
public ListenerWorker(InputStream inputStream, Closeable socket)
141143
{
142-
input = new DataInputStream(inputStream);
144+
this.socket = socket;
145+
input = new DataInputStream(inputStream);
143146
}
144147

145148
public void run()
@@ -151,6 +154,8 @@ public void run()
151154
executePartialSync(context, syncDir);
152155

153156
Platform.callJSMethod(NativeScriptApplication.getInstance(), "onLiveSync", Void.class);
157+
158+
socket.close();
154159
}
155160
catch (IOException e)
156161
{
@@ -161,10 +166,9 @@ public void run()
161166

162167
public void startServer()
163168
{
164-
serverThread = new ServerThread(18181);
165-
166-
javaServerThread = new Thread(serverThread);
167-
javaServerThread.start();
169+
localServerThread = new LocalServerSocketThread(context.getPackageName() + "-livesync");
170+
localServerJavaThread = new Thread(localServerThread);
171+
localServerJavaThread.start();
168172
}
169173

170174
private void deleteRecursive(File fileOrDirectory)

0 commit comments

Comments
 (0)