This script shows how to make a simple iterative server that can receive TMemFile from multiple clients and merge them into a single file without block.
Note: This server assumes that the client will reset the histogram after each upload to simplify the merging.
This server can accept connections while handling currently open connections. Compare this script to hserv.C that blocks on accept. In this script a server socket is created and added to a monitor. A monitor object is used to monitor connection requests on the server socket. After accepting the connection the new socket is added to the monitor and immediately ready for use. Once two connections are accepted the server socket is removed from the monitor and closed. The monitor continues monitoring the sockets.
void fastMergeServer(bool cache = false) {
return;
}
merger.SetPrintLevel(0);
enum StatusKind {
kStartConnection = 0,
kProtocol = 1,
kProtocolVersion = 1
};
while (true) {
if (clientCount > 100) {
printf("only accept 100 clients connections\n");
} else {
client->
Send(clientCount, kStartConnection);
client->
Send(kProtocolVersion, kProtocol);
++clientCount;
printf("Accept %d connections\n",clientCount);
}
continue;
}
if (mess==nullptr) {
Error(
"fastMergeServer",
"The client did not send a message\n");
char str[64];
printf("Client %d: %s\n", clientCount, str);
printf(
"Client %d: bytes recv = %d, bytes sent = %d\n", clientCount, s->
GetBytesRecv(),
--clientCount;
if (mon->
GetActive() == 0 || clientCount == 0) {
printf("No more active clients... stopping\n");
break;
}
Info(
"fastMergeServer",
"Receive input from client %d for %s",clientId,filename.
Data());
delete transient;
merger.OutputFile(filename,"UPDATE");
merger.AddAdoptFile(transient);
transient = nullptr;
} else {
printf("*** Unexpected message ***\n");
}
delete mess;
}
}
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
long long Long64_t
Portable signed long integer 8 bytes.
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void ReadTString(TString &s) override
Read TString from TBuffer.
char * ReadString(char *s, Int_t max) override
Read string from I/O buffer.
void ReadInt(Int_t &i) override
void ReadLong64(Long64_t &l) override
void SetBufferOffset(Int_t offset=0)
A cache when writing files over the network.
This class provides file copy and merging services.
@ kAllIncremental
Merge incrementally all type of objects.
A TMemFile is like a normal TFile except that it reads and writes only from memory.
TClass * GetClass() const
TSocket * Select()
Return pointer to socket for which an event is waiting.
virtual void Add(TSocket *sock, Int_t interest=kRead)
Add socket to the monitor's active list.
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
const char * GetName() const override
Returns name of object.
This class implements server sockets.
This class implements client sockets.
UInt_t GetBytesRecv() const
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
UInt_t GetBytesSent() const
virtual void Close(Option_t *opt="")
Close the socket.
TClass * IsA() const override
virtual Bool_t IsValid() const
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
const char * Data() const
BVH_ALWAYS_INLINE T length(const Vec< T, N > &v)