Returning to the subject of last post, about Caché's sockets, lets explore other possibilities.
At last post, I showed a simple example of how to create sockets, both server and client sockets. However, that example had a limitation: at the ServerSocket, the code shown only accepted one request, and after dealing with the connection, it ended its execution. As an example, it is still valid, but in a real-scenario, it's too limited.
In fact, the class %IO.ServerSocket has another method, which allows us to create a Server socket more alike a Java's ServerSocket. This method is ListenJob
.
First, lets see a simple example code:
Class teste.ServerSpawnSocket Extends %RegisteredObject
{
ClassMethod server()
{
set sock = ##class(%IO.ServerSocket).%New()
set st = $$$OK
w "Starting server...", !
// open serverSocket, param:
// port, timeOut, status
w sock.Open(7777, -1, st), !
w st, !
// server to listen incoming connections,
// and when it arrives, spawns a new job in background
// params:
//
// timeOut,
// classNameContainingMethodToBeCalled,
// jobArguments,
// socketClassName (default: %IO.ServerSocket),
// maxNumberOfJobs (default: -1),
// runInForeground (default: 0 - no),
// status
w sock.ListenJob(-1, "teste.ServerSpawnSocket", "", , , , st), !
w st, !
w "Closing server...", !
quit
}
//===================================================
ClassMethod OnConnected(StreamHandler As %IO.DeviceStream, AnotherArgs)
{
set st = $$$OK
// reading from socket, param:
// maxStringSize, timeout, status, lineTerminator
set ^someVar("read from tcp") = StreamHandler.ReadLine(50, 30, st, $c(13))
// writing to socket, param:
// stringToBeWritten, automaticFlush, status
d StreamHandler.Write("Goodbye Dolly", 1, st)
d StreamHandler.Close(st)
set ^someVar("status") = st
quit
}
}
The method server()
is quite simple, having more lines of comments than lines of code =P. After we create an object, again we call method Open
. In this method, we define the port that the socket will keep listening. But the method we are more interested now, is the following one, ListenJob
This method does the following: when a new request arrives at the socket, if there's no restriction to request's address, the method creates a new job in background, passing as parameter an object of class %IO.DeviceStream, which holds the data stream created for the connection. But a question arises: what method does Caché calls? Well, after surfing through source-code of %IO.ServerSocket, I found out that after calling some internal methods of its own class, the object invokes a classmethod (in Java, we call it static class method) OnConnected
, of the class whose name was passed as the second parameter of ListenJob
.ListenJob
also can control the max number of concurrent jobs, which is defined in 5th parameter. The first parameter is our old known friend timeout, that in our example, is set to -1, to never expire. The third parameter might be made of a list, with parameters to be passed to OnConnected
method. The 6th parameter is useless (I'll talk about it in the future), and the last parameter is also an old friend, an object with the status of method's execution.
At the example, we can see that we passed as parameter, the name of it's own class, but it could have been another class as well. The important is that the class has the class method OnConnected
. The method's interface can be seen at the example. (IMPORTANT: this interface is not fixed or official, since I didn't find any reference to this in documentation, just in sources).
Inside OnConnected
method, we can manipulate the data stream as we wish, the example just read and write something from/to the stream, before closing the connection.
That's it. Hope it is usefull.
terça-feira, 24 de julho de 2007
More sockets in Caché
Postado por
Andarilho
às
06:49
Marcadores: caché, network, programming, sockets
Assinar:
Postar comentários (Atom)
0 comentários:
Postar um comentário