Lines Matching defs:peer

63     /** Request for a connection to another peer. */
66 /** A connection to another peer. */
88 // TODO: Use custom headers for master->peer, peer->master, peer->peer.
100 /** A packet which will be sent to a peer. */
107 /** Connection to peer. Used with CONNECTION. */
124 /** Represents a remote peer. */
127 /** Local peer state. You typically have one peer per process. */
129 /** This peer's PID. */
133 * Map from pid to peer proxy. The peer has a peer proxy for each remote
134 * peer it's connected to.
146 /** Is this peer the master? */
167 /** Keeps track of data coming in from the remote peer. */
172 /** File descriptor for this peer. */
176 * Queue of packets to be written out to the remote peer.
190 /** Reference back to the local peer. */
191 Peer* peer;
194 * Used in master only. Maps this peer proxy to other peer proxies to
195 * which the peer has been connected to. Maps pid to PeerProxy. Helps
202 static const char* MASTER_PATH = "/master.peer";
204 /** Credentials of the master peer. */
207 /** Creates a peer proxy and adds it to the peer proxy map. */
208 static PeerProxy* peerProxyCreate(Peer* peer, Credentials credentials);
254 /** Gets exclusive access to the peer for this thread. */
255 static void peerLock(Peer* peer) {
256 pthread_mutex_lock(&peer->mutex);
259 /** Releases exclusive access to the peer. */
260 static void peerUnlock(Peer* peer) {
261 pthread_mutex_unlock(&peer->mutex);
271 * Prepare to read a new packet from the peer.
301 /** Takes the peer lock and enqueues the given packet. */
304 Peer* peer = peerProxy->peer;
305 peerLock(peer);
307 peerUnlock(peer);
315 Peer* peer = peerProxy->peer;
316 peerLock(peer);
322 peerUnlock(peer);
333 peerUnlock(peer);
336 peerUnlock(peer);
346 * Checks whether a peer died recently.
348 static bool peerIsDead(Peer* peer, pid_t pid) {
351 pid_t deadPeer = peer->deadPeers[i];
373 * Called when the peer dies.
388 Peer* localPeer = peerProxy->peer;
393 // Remember for awhile that the peer died.
401 // Remove from peer map.
404 // External threads can no longer get to this peer proxy, so we don't
429 // Free the peer proxy itself.
447 * Buffers output sent to a peer. May be called multiple times until the entire
460 /** Writes packet bytes to peer. */
469 /** Sends a socket to the peer. */
559 * Sets up a peer proxy's fd before we try to select() it.
566 peerLock(peerProxy->peer);
568 peerUnlock(peerProxy->peer);
580 /** Prepare to read bytes from the peer. */
595 * Gets a peer proxy for the given ID. Creates a peer proxy if necessary.
598 * Returns NULL if an error occurs. Sets errno to EHOSTDOWN if the peer died
601 static PeerProxy* peerProxyGetOrCreate(Peer* peer, pid_t pid,
603 if (pid == peer->pid) {
608 if (peerIsDead(peer, pid)) {
613 PeerProxy* peerProxy = hashmapGet(peer->peerProxies, &pid);
618 // If this is the master peer, we already know about all peers.
619 if (peer->master) {
624 // Try to create a peer proxy.
648 peerProxy = peerProxyCreate(peer, credentials);
656 PeerProxy* masterProxy = peer->masterProxy;
665 * Switches the master peer proxy into a state where it's waiting for a
675 // Kill off the evil peer.
681 Peer* localPeer = masterProxy->peer;
683 // Create a peer proxy so we have somewhere to stash the creds.
696 // Keep track of which peer proxy we're accepting a connection for.
701 * Reads input from a peer process.
777 // The peer proxy this connection is for.
780 LOGW("Received connection for unknown peer.");
783 Peer* peer = masterProxy->peer;
785 SelectableFd* selectableFd = selectorAdd(peer->selector, incomingFd);
841 peerLock(peerA->peer);
844 peerUnlock(peerA->peer);
848 * Informs a peer that the peer they're trying to connect to couldn't be
868 * Handles a request to be connected to another peer.
872 Peer* master = peerProxy->peer;
891 * The master told us this peer is dead.
895 Peer* peer = masterProxy->peer;
897 // Look up the peer proxy.
900 peerLock(peer);
901 peerProxy = hashmapGet(peer->peerProxies, &pid);
902 peerUnlock(peer);
939 * Buffers input sent by peer. May be called multiple times until the entire
963 * Reads input from a peer process.
984 peerProxy->peer->onBytes(peerProxy->credentials,
999 static PeerProxy* peerProxyCreate(Peer* peer, Credentials credentials) {
1011 peerProxy->peer = peer;
1018 // inside of the peer proxy itself.
1020 hashmapPut(peer->peerProxies, pid, peerProxy);
1024 /** Accepts a connection to the master peer. */
1078 // Create a peer proxy.
1091 * Creates the local peer.
1094 Peer* peer = calloc(1, sizeof(Peer));
1095 if (peer == NULL) {
1098 peer->peerProxies = hashmapCreate(10, &pidHash, &pidEquals);
1099 peer->selector = selectorCreate();
1108 if (pthread_mutex_init(&peer->mutex, &attributes) != 0) {
1112 peer->pid = getpid();
1113 return peer;
1116 /** The local peer. */
1127 * Sends a packet of bytes to a remote peer. Returns 0 on success.
1130 * allocated. Sets errno to EHOSTDOWN if the peer died recently. Sets errno
1134 Peer* peer = localPeer;
1135 assert(peer != NULL);
1160 peerLock(peer);
1162 PeerProxy* peerProxy = peerProxyGetOrCreate(peer, pid, true);
1164 // The peer is already dead or we couldn't alloc memory. Either way,
1166 peerUnlock(peer);
1171 peerUnlock(peer);
1172 selectorWakeUp(peer->selector);
1193 * Sends a packet of bytes to a remote peer without copying the bytes. Calls
1197 * allocated. Sets errno to EHOSTDOWN if the peer died recently. Sets errno
1202 Peer* peer = localPeer;
1203 assert(peer != NULL);
1235 peerLock(peer);
1237 PeerProxy* peerProxy = peerProxyGetOrCreate(peer, pid, true);
1239 // The peer is already dead or we couldn't alloc memory. Either way,
1241 peerUnlock(peer);
1246 peerUnlock(peer);
1247 selectorWakeUp(peer->selector);
1253 * Starts the master peer. The master peer differs from other peers in that
1255 * master peer.
1284 // Create the peer for this process. Fail if we already have one.
1306 * Starts a local peer.
1312 // Connect to master peer.
1323 // Create the peer for this process. Fail if we already have one.
1340 // Create a peer proxy for the master peer.
1350 /** Starts the master peer I/O loop. Doesn't return. */