2 * DEBUG: section 54 Interprocess Communication
6 #include "base/TextException.h"
8 #include "comm/Connection.h"
11 #include "ipc/Messages.h"
13 #include "ipc/SharedListen.h"
14 #include "ipc/StartListening.h"
15 #include "ipc/TypedMsgHdr.h"
20 /// holds information necessary to handle JoinListen response
21 class PendingOpenRequest
24 Ipc::OpenListenerParams params
; ///< actual comm_open_sharedListen() parameters
25 AsyncCall::Pointer callback
; // who to notify
28 /// maps ID assigned at request time to the response callback
29 typedef std::map
<int, PendingOpenRequest
> SharedListenRequestMap
;
30 static SharedListenRequestMap TheSharedListenRequestMap
;
33 AddToMap(const PendingOpenRequest
&por
)
35 // find unused ID using linear seach; there should not be many entries
36 for (int id
= 0; true; ++id
) {
37 if (TheSharedListenRequestMap
.find(id
) == TheSharedListenRequestMap
.end()) {
38 TheSharedListenRequestMap
[id
] = por
;
42 assert(false); // not reached
46 Ipc::OpenListenerParams::OpenListenerParams()
48 memset(this, 0, sizeof(*this));
52 Ipc::OpenListenerParams::operator <(const OpenListenerParams
&p
) const
54 if (sock_type
!= p
.sock_type
)
55 return sock_type
< p
.sock_type
;
58 return proto
< p
.proto
;
60 // ignore flags and fdNote differences because they do not affect binding
62 return addr
.compareWhole(p
.addr
) < 0;
65 Ipc::SharedListenRequest::SharedListenRequest(): requestorId(-1), mapId(-1)
67 // caller will then set public data members
70 Ipc::SharedListenRequest::SharedListenRequest(const TypedMsgHdr
&hdrMsg
)
72 hdrMsg
.checkType(mtSharedListenRequest
);
76 void Ipc::SharedListenRequest::pack(TypedMsgHdr
&hdrMsg
) const
78 hdrMsg
.setType(mtSharedListenRequest
);
82 Ipc::SharedListenResponse::SharedListenResponse(int aFd
, int anErrNo
, int aMapId
):
83 fd(aFd
), errNo(anErrNo
), mapId(aMapId
)
87 Ipc::SharedListenResponse::SharedListenResponse(const TypedMsgHdr
&hdrMsg
):
88 fd(-1), errNo(0), mapId(-1)
90 hdrMsg
.checkType(mtSharedListenResponse
);
93 // other conn details are passed in OpenListenerParams and filled out by SharedListenJoin()
96 void Ipc::SharedListenResponse::pack(TypedMsgHdr
&hdrMsg
) const
98 hdrMsg
.setType(mtSharedListenResponse
);
103 void Ipc::JoinSharedListen(const OpenListenerParams
¶ms
,
104 AsyncCall::Pointer
&callback
)
106 PendingOpenRequest por
;
108 por
.callback
= callback
;
110 SharedListenRequest request
;
111 request
.requestorId
= KidIdentifier
;
112 request
.params
= por
.params
;
113 request
.mapId
= AddToMap(por
);
115 debugs(54, 3, HERE
<< "getting listening FD for " << request
.params
.addr
<<
116 " mapId=" << request
.mapId
);
119 request
.pack(message
);
120 SendMessage(Ipc::Port::CoordinatorAddr(), message
);
123 void Ipc::SharedListenJoined(const SharedListenResponse
&response
)
125 // Dont debugs c fully since only FD is filled right now.
126 debugs(54, 3, HERE
<< "got listening FD " << response
.fd
<< " errNo=" <<
127 response
.errNo
<< " mapId=" << response
.mapId
);
129 Must(TheSharedListenRequestMap
.find(response
.mapId
) != TheSharedListenRequestMap
.end());
130 PendingOpenRequest por
= TheSharedListenRequestMap
[response
.mapId
];
131 Must(por
.callback
!= NULL
);
132 TheSharedListenRequestMap
.erase(response
.mapId
);
134 StartListeningCb
*cbd
= dynamic_cast<StartListeningCb
*>(por
.callback
->getDialer());
135 assert(cbd
&& cbd
->conn
!= NULL
);
136 Must(cbd
&& cbd
->conn
!= NULL
);
137 cbd
->conn
->fd
= response
.fd
;
139 if (Comm::IsConnOpen(cbd
->conn
)) {
140 OpenListenerParams
&p
= por
.params
;
141 cbd
->conn
->local
= p
.addr
;
142 cbd
->conn
->flags
= p
.flags
;
143 // XXX: leave the comm AI stuff to comm_import_opened()?
144 struct addrinfo
*AI
= NULL
;
145 p
.addr
.getAddrInfo(AI
);
146 AI
->ai_socktype
= p
.sock_type
;
147 AI
->ai_protocol
= p
.proto
;
148 comm_import_opened(cbd
->conn
, FdNote(p
.fdNote
), AI
);
149 Ip::Address::FreeAddrInfo(AI
);
152 cbd
->errNo
= response
.errNo
;
153 cbd
->handlerSubscription
= por
.params
.handlerSubscription
;
154 ScheduleCallHere(por
.callback
);