Skip to content

Commit c2883f4

Browse files
cosminbascatimfel
authored andcommitted
socket builtins stub methods
1 parent d414de1 commit c2883f4

File tree

3 files changed

+335
-2
lines changed

3 files changed

+335
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/PSocket.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,84 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.socket;
4242

43+
import java.net.InetSocketAddress;
44+
import java.nio.channels.SelectableChannel;
45+
4346
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4447
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4548

4649
public class PSocket extends PythonBuiltinObject {
50+
public final static int AF_UNSPEC = 0;
51+
public final static int AF_INET = 2;
52+
public final static int AF_INET6 = 23;
53+
54+
public final static int SOCK_DGRAM = 1;
55+
public final static int SOCK_STREAM = 2;
56+
57+
private final static InetSocketAddress EPHEMERAL_ADDRESS = new InetSocketAddress(0);
58+
4759
private final int family;
4860
private final int type;
4961
private final int proto;
5062

63+
private double timeout;
64+
private boolean blocking;
65+
66+
private SelectableChannel channel;
67+
private InetSocketAddress address = EPHEMERAL_ADDRESS;
68+
69+
private enum SocketType {
70+
UNKNOWN_SOCKET,
71+
CLIENT_SOCKET,
72+
SERVER_SOCKET,
73+
DATAGRAM_SOCKET
74+
}
75+
76+
private SocketType socketType;
77+
5178
public PSocket(PythonClass cls, int family, int type, int proto) {
5279
super(cls);
5380
this.family = family;
5481
this.type = type;
5582
this.proto = proto;
83+
switch (type) {
84+
case SOCK_DGRAM:
85+
socketType = SocketType.DATAGRAM_SOCKET;
86+
break;
87+
default:
88+
socketType = SocketType.UNKNOWN_SOCKET;
89+
}
90+
}
91+
92+
public int getFamily() {
93+
return family;
94+
}
95+
96+
public int getType() {
97+
return type;
98+
}
99+
100+
public int getProto() {
101+
return proto;
102+
}
103+
104+
public double getTimeout() {
105+
return timeout;
106+
}
107+
108+
public void setTimeout(double timeout) {
109+
this.timeout = timeout;
110+
}
111+
112+
public InetSocketAddress getAddress() {
113+
return address;
114+
}
115+
116+
public void setBlocking(boolean blocking) {
117+
if (blocking) {
118+
this.setTimeout(-1.0);
119+
} else {
120+
this.setTimeout(0.0);
121+
}
56122
}
57123
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,239 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.socket;
4242

43-
import java.util.ArrayList;
4443
import java.util.List;
4544

45+
import com.oracle.graal.python.builtins.Builtin;
4646
import com.oracle.graal.python.builtins.CoreFunctions;
4747
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4848
import com.oracle.graal.python.builtins.PythonBuiltins;
49+
import com.oracle.graal.python.builtins.objects.PNone;
50+
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4951
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
52+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
53+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
54+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
55+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
56+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5057
import com.oracle.truffle.api.dsl.NodeFactory;
58+
import com.oracle.truffle.api.dsl.Specialization;
5159

5260
@CoreFunctions(extendClasses = PythonBuiltinClassType.PSocket)
5361
public class SocketBuiltins extends PythonBuiltins {
5462

5563
@Override
5664
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
57-
return new ArrayList<>();
65+
return SocketBuiltinsFactory.getFactories();
66+
}
67+
68+
// accept()
69+
@Builtin(name = "accept", fixedNumOfPositionalArgs = 1)
70+
@GenerateNodeFactory
71+
abstract static class AcceptNode extends PythonUnaryBuiltinNode {
72+
@Specialization
73+
Object accept(PSocket socket) {
74+
return PNotImplemented.NOT_IMPLEMENTED;
75+
}
76+
}
77+
78+
// bind(address)
79+
@Builtin(name = "bind", fixedNumOfPositionalArgs = 2)
80+
@GenerateNodeFactory
81+
abstract static class BindNode extends PythonBinaryBuiltinNode {
82+
@Specialization
83+
Object bind(PSocket socket, Object address) {
84+
return PNotImplemented.NOT_IMPLEMENTED;
85+
}
86+
}
87+
88+
// close()
89+
@Builtin(name = "close", fixedNumOfPositionalArgs = 1)
90+
@GenerateNodeFactory
91+
abstract static class CloseNode extends PythonUnaryBuiltinNode {
92+
@Specialization
93+
Object close(PSocket socket) {
94+
return PNotImplemented.NOT_IMPLEMENTED;
95+
}
96+
}
97+
98+
// connect(address)
99+
@Builtin(name = "connect", fixedNumOfPositionalArgs = 2)
100+
@GenerateNodeFactory
101+
abstract static class ConnectNode extends PythonBinaryBuiltinNode {
102+
@Specialization
103+
Object connect(PSocket socket, Object address) {
104+
return PNotImplemented.NOT_IMPLEMENTED;
105+
}
106+
}
107+
108+
// getpeername()
109+
@Builtin(name = "getpeername", fixedNumOfPositionalArgs = 1)
110+
@GenerateNodeFactory
111+
abstract static class GetPeerNameNode extends PythonUnaryBuiltinNode {
112+
@Specialization
113+
Object get(PSocket socket) {
114+
return PNotImplemented.NOT_IMPLEMENTED;
115+
}
116+
}
117+
118+
// getsockname()
119+
@Builtin(name = "getsockname", fixedNumOfPositionalArgs = 1)
120+
@GenerateNodeFactory
121+
abstract static class GetSockNameNode extends PythonUnaryBuiltinNode {
122+
@Specialization
123+
Object get(PSocket socket) {
124+
return PNotImplemented.NOT_IMPLEMENTED;
125+
}
126+
}
127+
128+
// getblocking()
129+
@Builtin(name = "getblocking", fixedNumOfPositionalArgs = 1)
130+
@GenerateNodeFactory
131+
abstract static class GetBlockingNode extends PythonUnaryBuiltinNode {
132+
@Specialization
133+
boolean get(PSocket socket) {
134+
return false;
135+
}
136+
}
137+
138+
// gettimeout
139+
@Builtin(name = "gettimeout", fixedNumOfPositionalArgs = 1)
140+
@GenerateNodeFactory
141+
abstract static class GetTimeoutNode extends PythonUnaryBuiltinNode {
142+
@Specialization
143+
Object get(PSocket socket) {
144+
return PNone.NONE;
145+
}
146+
}
147+
148+
// listen
149+
@Builtin(name = "listen", fixedNumOfPositionalArgs = 1)
150+
@GenerateNodeFactory
151+
abstract static class ListenNode extends PythonUnaryBuiltinNode {
152+
@Specialization
153+
Object listen(PSocket socket) {
154+
return PNotImplemented.NOT_IMPLEMENTED;
155+
}
156+
}
157+
158+
// recv(bufsize[, flags])
159+
@Builtin(name = "recv", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
160+
@GenerateNodeFactory
161+
abstract static class RecvNode extends PythonTernaryBuiltinNode {
162+
@Specialization
163+
Object recv(PSocket socket, int bufsize, int flags) {
164+
return PNotImplemented.NOT_IMPLEMENTED;
165+
}
166+
}
167+
168+
// recvfrom(bufsize[, flags])
169+
@Builtin(name = "recvfrom", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
170+
@GenerateNodeFactory
171+
abstract static class RecvFromNode extends PythonTernaryBuiltinNode {
172+
@Specialization
173+
Object recvFrom(PSocket socket, int bufsize, int flags) {
174+
return PNotImplemented.NOT_IMPLEMENTED;
175+
}
176+
}
177+
178+
// recvmsg(bufsize[, ancbufsize[, flags]])
179+
@Builtin(name = "recvmsg", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4)
180+
@GenerateNodeFactory
181+
abstract static class RecvMsgNode extends PythonBuiltinNode {
182+
@Specialization
183+
Object recvFrom(PSocket socket, int bufsize, int ancbufsize, int flags) {
184+
return PNotImplemented.NOT_IMPLEMENTED;
185+
}
186+
}
187+
188+
// send(bytes[, flags])
189+
@Builtin(name = "send", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
190+
@GenerateNodeFactory
191+
abstract static class SendNode extends PythonTernaryBuiltinNode {
192+
@Specialization
193+
Object send(PSocket socket, Object bytes, int flags) {
194+
return PNotImplemented.NOT_IMPLEMENTED;
195+
}
196+
}
197+
198+
// sendall(bytes[, flags])
199+
@Builtin(name = "sendall", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
200+
@GenerateNodeFactory
201+
abstract static class SendAllNode extends PythonTernaryBuiltinNode {
202+
@Specialization
203+
Object sendAll(PSocket socket, Object bytes, int flags) {
204+
return PNotImplemented.NOT_IMPLEMENTED;
205+
}
206+
}
207+
208+
// sendto(bytes, address)
209+
// sendto(bytes, flags, address)
210+
@Builtin(name = "sendto", minNumOfPositionalArgs = 3, maxNumOfPositionalArgs = 4)
211+
@GenerateNodeFactory
212+
abstract static class SendToNode extends PythonBuiltinNode {
213+
@Specialization
214+
Object sendTo(PSocket socket, Object bytes, int flags, Object address) {
215+
return PNotImplemented.NOT_IMPLEMENTED;
216+
}
217+
}
218+
219+
// sendmsg(buffers[, ancdata[, flags[, address]]])
220+
@Builtin(name = "sendmsg", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 5)
221+
@GenerateNodeFactory
222+
abstract static class SendMsgNode extends PythonBuiltinNode {
223+
@Specialization
224+
Object sendMsg(PSocket socket, Object buffers, Object ancdata, int flags, Object address) {
225+
return PNotImplemented.NOT_IMPLEMENTED;
226+
}
227+
}
228+
229+
// settimeout(value)
230+
@Builtin(name = "settimeout", fixedNumOfPositionalArgs = 2)
231+
@GenerateNodeFactory
232+
abstract static class SetTimeoutNode extends PythonBinaryBuiltinNode {
233+
@Specialization
234+
Object family(PSocket socket, double value) {
235+
return PNone.NONE;
236+
}
237+
}
238+
239+
// shutdown(how)
240+
@Builtin(name = "shutdown", fixedNumOfPositionalArgs = 2)
241+
@GenerateNodeFactory
242+
abstract static class shutdownNode extends PythonBinaryBuiltinNode {
243+
@Specialization
244+
Object family(PSocket socket, Object how) {
245+
return PNotImplemented.NOT_IMPLEMENTED;
246+
}
247+
}
248+
249+
// family
250+
@Builtin(name = "family", fixedNumOfPositionalArgs = 1, isGetter = true)
251+
@GenerateNodeFactory
252+
abstract static class SocketFamilyNode extends PythonUnaryBuiltinNode {
253+
@Specialization
254+
int family(PSocket socket) {
255+
return socket.getFamily();
256+
}
257+
}
258+
259+
// type
260+
@Builtin(name = "type", fixedNumOfPositionalArgs = 1, isGetter = true)
261+
@GenerateNodeFactory
262+
abstract static class SocketTypeNode extends PythonUnaryBuiltinNode {
263+
@Specialization
264+
int type(PSocket socket) {
265+
return socket.getType();
266+
}
267+
}
268+
269+
// proto
270+
@Builtin(name = "proto", fixedNumOfPositionalArgs = 1, isGetter = true)
271+
@GenerateNodeFactory
272+
abstract static class SockProtoNode extends PythonUnaryBuiltinNode {
273+
@Specialization
274+
int proto(PSocket socket) {
275+
return socket.getProto();
276+
}
58277
}
59278
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
AF_UNSPEC = 0
41+
AF_INET = 2
42+
AF_INET6 = 23
43+
44+
SOCK_DGRAM = 1
45+
SOCK_STREAM = 2
46+
SOCK_RAW = 3
47+
SOCK_RDM = 4
48+
SOCK_SEQPACKET = 5

0 commit comments

Comments
 (0)