Skip to content

Commit

Permalink
add ZnUnixSocketClient [feenkcom/gtoolkit#3433]
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajKubelka committed Aug 29, 2023
1 parent f83a86c commit c00a559
Show file tree
Hide file tree
Showing 33 changed files with 207 additions and 0 deletions.
5 changes: 5 additions & 0 deletions repository/Zinc-HTTP-UnixSocket.package/.filetree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"separateMethodMetaAndSource" : false,
"noMethodMetaData" : true,
"useCypressPropertiesFile" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*Zinc-HTTP-UnixSocket
newIPC
"Create a socket and initialise it for IPC aka Unix domain."

self initializeNetwork.
^ [ super new initialize: TCPSocketType withDomain: 1 ]
repeatWithGCIf: [ :socket | socket isValid not ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
*Zinc-HTTP-UnixSocket
initialize: socketType withDomain: socketDomain
"Initialize a new socket handle. If socket creation fails, socketHandle will be set to nil."

| semaIndex readSemaIndex writeSemaIndex |
socketHandle ifNotNil: [^Error signal: 'The socket is already bound'].
semaphore := Semaphore new.
readSemaphore := Semaphore new.
writeSemaphore := Semaphore new.
semaIndex := Smalltalk registerExternalObject: semaphore.
readSemaIndex := Smalltalk registerExternalObject: readSemaphore.
writeSemaIndex := Smalltalk registerExternalObject: writeSemaphore.
socketHandle := self
primSocketCreateNetwork: socketDomain
type: socketType
receiveBufferSize: 8000
sendBufSize: 8000
semaIndex: semaIndex
readSemaIndex: readSemaIndex
writeSemaIndex: writeSemaIndex.
socketHandle
ifNil: [
"socket creation failed"
Smalltalk unregisterExternalObject: semaphore.
Smalltalk unregisterExternalObject: readSemaphore.
Smalltalk unregisterExternalObject: writeSemaphore.
readSemaphore := writeSemaphore := semaphore := nil ]
ifNotNil: [ self register ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "Socket"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ZnCannotConnectUnixSocket is signalled when a Unix Domain Socket connection should be established but was not.

Part of Zinc HTTP Components.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file: anObject
file := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file
^ file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
messageText
^ super messageText
ifEmpty: [ self file ifNotNil: [ self file fullName ] ifNil: [ '' ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commentStamp" : "<historical>",
"super" : "Error",
"category" : "Zinc-HTTP-UnixSocket-Exceptions",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"file"
],
"name" : "ZnCannotConnectUnixSocket",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ZnCannotCreateUnixSocket is signalled when a Unix Domain Socket should be instantiated but was not.

Part of Zinc HTTP Components.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file: anObject
file := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file
^ file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
messageText
^ super messageText
ifEmpty: [ self file ifNotNil: [ self file fullName ] ifNil: [ '' ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commentStamp" : "<historical>",
"super" : "Error",
"category" : "Zinc-HTTP-UnixSocket-Exceptions",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"file"
],
"name" : "ZnCannotCreateUnixSocket",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ZnMissingUnixSocket is signalled when a Unix Domain Socket file does not exist but should exist.

Part of Zinc HTTP Components.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file: anObject
file := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
file
^ file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
messageText
^ super messageText
ifEmpty: [ self file ifNotNil: [ self file fullName ] ifNil: [ '' ] ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commentStamp" : "<historical>",
"super" : "Error",
"category" : "Zinc-HTTP-UnixSocket-Exceptions",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"file"
],
"name" : "ZnMissingUnixSocket",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*Zinc-HTTP-UnixSocket
socketStreamToUnixSocketFile: socketFile
^ self default
socketStreamToUnixSocketFile: socketFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*Zinc-HTTP-UnixSocket
socketStreamToUnixSocketFile: socketFile
| ipcSocket |
ipcSocket := self unixSocketOnFile: socketFile.
^ ZnNetworkingUtils socketStreamOn: ipcSocket.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*Zinc-HTTP-UnixSocket
unixSocketOnFile: socketFile
| addressSize socketAddress ipcSocket |
socketFile exists ifFalse: [
ZnMissingUnixSocket new file: socketFile; signal ].

NetNameResolver
primGetAddressInfoHost: ''
service: socketFile fullName
flags: 0
family: 1
type: 0
protocol: 0.
addressSize := NetNameResolver primGetAddressInfoSize.
socketAddress := SocketAddress new: addressSize withAll: 0.
NetNameResolver primGetAddressInfoResult: socketAddress.

ipcSocket := Socket newIPC.
ipcSocket ifNil: [
ZnCannotCreateUnixSocket new file: socketFile; signal ].
ipcSocket connectTo: socketAddress.
ipcSocket isConnected ifFalse: [
ZnCannotConnectUnixSocket new file: socketFile; signal ].
^ ipcSocket
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name" : "ZnNetworkingUtils"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
I am ZnClient.
I am ZnUnixSocketClient, an object to build, execute, and process HTTP client request over Unix Domain Sockets.

Simplest possible invocation:

ZnUnixSocketClient new
unixSocket: '/var/run/docker.sock';
get: 'http://localhost/v1.43/containers/json'.

It is equivalent to the following cURL command:

curl --unix-socket /var/run/docker.sock http://localhost/v1.43/containers/json

Part of Zinc HTTP Components.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
private
newConnectionTo: url
self unixSocket
ifNil: [ super newConnectionTo: url ]
ifNotNil: [ :socketPath | self newConnectionToUnixSocketFile: socketPath asFileReference ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
private
newConnectionToUnixSocketFile: socketFile
| initialMilliseconds |
self signalHTTPProgress: 'Connecting to ', socketFile asString.
initialMilliseconds := Time millisecondClockValue.
(connection notNil and: [ connection isConnected ])
ifTrue: [ connection close ].
connection := ZnNetworkingUtils socketStreamToUnixSocketFile: socketFile.
self logConnectionEstablishedTo: socketFile asZnUrl started: initialMilliseconds
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
unixSocket: socketPath
self optionAt: #unixSocket put: socketPath
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
unixSocket
^ self optionAt: #unixSocket ifAbsent: [ nil ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "<historical>",
"super" : "ZnClient",
"category" : "Zinc-HTTP-UnixSocket-Client-Server",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "ZnUnixSocketClient",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SystemOrganization addCategory: #'Zinc-HTTP-UnixSocket'!
SystemOrganization addCategory: #'Zinc-HTTP-UnixSocket-Client-Server'!
SystemOrganization addCategory: #'Zinc-HTTP-UnixSocket-Exceptions'!
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(name 'Zinc-HTTP-UnixSocket')
1 change: 1 addition & 0 deletions repository/Zinc-HTTP-UnixSocket.package/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }

0 comments on commit c00a559

Please sign in to comment.