1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import java.io.BufferedInputStream;
14 import java.io.BufferedOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.net.InetAddress;
19 import java.net.Socket;
20 import java.util.Arrays;
21 import java.util.Collection;
22
23 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
24 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
25
26
27
28
29 public class DaemonClient {
30 private final Daemon daemon;
31
32 private InetAddress peer;
33
34 private InputStream rawIn;
35
36 private OutputStream rawOut;
37
38 DaemonClient(Daemon d) {
39 daemon = d;
40 }
41
42 void setRemoteAddress(InetAddress ia) {
43 peer = ia;
44 }
45
46
47
48
49
50
51 public Daemon getDaemon() {
52 return daemon;
53 }
54
55
56
57
58
59
60 public InetAddress getRemoteAddress() {
61 return peer;
62 }
63
64
65
66
67
68
69 public InputStream getInputStream() {
70 return rawIn;
71 }
72
73
74
75
76
77
78 public OutputStream getOutputStream() {
79 return rawOut;
80 }
81
82 void execute(Socket sock) throws IOException,
83 ServiceNotEnabledException, ServiceNotAuthorizedException {
84 rawIn = new BufferedInputStream(sock.getInputStream());
85 rawOut = new BufferedOutputStream(sock.getOutputStream());
86
87 if (0 < daemon.getTimeout())
88 sock.setSoTimeout(daemon.getTimeout() * 1000);
89 String cmd = new PacketLineIn(rawIn).readStringRaw();
90
91 Collection<String> extraParameters = null;
92
93 int nulnul = cmd.indexOf("\0\0");
94 if (nulnul != -1) {
95 extraParameters = Arrays.asList(cmd.substring(nulnul + 2).split("\0"));
96 }
97
98 final int nul = cmd.indexOf('\0');
99 if (nul >= 0) {
100
101
102
103
104 cmd = cmd.substring(0, nul);
105 }
106
107 final DaemonService srv = getDaemon().matchService(cmd);
108 if (srv == null)
109 return;
110 sock.setSoTimeout(0);
111 srv.execute(this, cmd, extraParameters);
112 }
113 }