@@ -20,7 +20,10 @@ const debug = createDebug("discojs:client");
2020 * Main, abstract, class representing a Disco client in a network, which handles
2121 * communication with other nodes, be it peers or a server.
2222 */
23- export abstract class Client extends EventEmitter < { 'status' : RoundStatus } > {
23+ export abstract class Client extends EventEmitter < {
24+ 'status' : RoundStatus ,
25+ 'participants' : number
26+ } > {
2427 // Own ID provided by the network's server.
2528 protected _ownId ?: NodeID
2629 // The network's server.
@@ -40,7 +43,10 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
4043 * we were doing before waiting (training locally or updating our model).
4144 * We use this attribute to store the status to rollback to when we stop waiting
4245 */
43- private previousStatus : RoundStatus | undefined ;
46+ #previousStatus: RoundStatus | undefined ;
47+
48+ // Current number of participants including this client in the training session
49+ #nbOfParticipants: number = 1 ;
4450
4551 constructor (
4652 public readonly url : URL , // The network server's URL to connect to
@@ -82,7 +88,7 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
8288 * the waiting status and once enough participants join, it can display the previous status again
8389 */
8490 protected saveAndEmit ( status : RoundStatus ) {
85- this . previousStatus = status
91+ this . # previousStatus = status
8692 this . emit ( "status" , status )
8793 }
8894
@@ -111,12 +117,13 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
111117 protected setupServerCallbacks ( setMessageInversionFlag : ( ) => void ) {
112118 // Setup an event callback if the server signals that we should
113119 // wait for more participants
114- this . server . on ( type . WaitingForMoreParticipants , ( ) => {
120+ this . server . on ( type . WaitingForMoreParticipants , ( event ) => {
115121 if ( this . promiseForMoreParticipants !== undefined )
116122 throw new Error ( "Server sent multiple WaitingForMoreParticipants messages" )
117123 debug ( `[${ shortenId ( this . ownId ) } ] received WaitingForMoreParticipants message from server` )
118124 // Display the waiting status right away
119125 this . emit ( "status" , "not enough participants" )
126+ this . nbOfParticipants = event . nbOfParticipants // emits the `participants` event
120127 // Upon receiving a WaitingForMoreParticipants message,
121128 // the client will await for this promise to resolve before sending its
122129 // local weight update
@@ -129,10 +136,10 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
129136 // and directly follows with an EnoughParticipants message when the 2nd participant joins
130137 // However, the EnoughParticipants can arrive before the NewNodeInfo (which can be much bigger)
131138 // so we check whether we received the EnoughParticipants before being assigned a node ID
132- this . server . once ( type . EnoughParticipants , ( ) => {
139+ this . server . once ( type . EnoughParticipants , ( event ) => {
133140 if ( this . _ownId === undefined ) {
134- debug ( `Received EnoughParticipants message from server before the NewFederatedNodeInfo message` )
135141 setMessageInversionFlag ( )
142+ this . nbOfParticipants = event . nbOfParticipants
136143 }
137144 } )
138145 }
@@ -146,10 +153,11 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
146153 protected async createPromiseForMoreParticipants ( ) : Promise < void > {
147154 return new Promise < void > ( ( resolve ) => {
148155 // "once" is important because we can't resolve the same promise multiple times
149- this . server . once ( type . EnoughParticipants , ( ) => {
156+ this . server . once ( type . EnoughParticipants , ( event ) => {
150157 debug ( `[${ shortenId ( this . ownId ) } ] received EnoughParticipants message from server` )
151158 // Emit the last status emitted before waiting if defined
152- if ( this . previousStatus !== undefined ) this . emit ( "status" , this . previousStatus )
159+ if ( this . #previousStatus !== undefined ) this . emit ( "status" , this . #previousStatus)
160+ this . nbOfParticipants = event . nbOfParticipants
153161 resolve ( )
154162 } )
155163 } )
@@ -190,7 +198,18 @@ export abstract class Client extends EventEmitter<{'status': RoundStatus}>{
190198 * If federated, it should the number of participants excluding the server
191199 * If local it should be 1
192200 */
193- abstract getNbOfParticipants ( ) : number ;
201+ public get nbOfParticipants ( ) : number {
202+ return this . #nbOfParticipants
203+ }
204+
205+ /**
206+ * Setter for the number of participants
207+ * It emits the number of participants to the client
208+ */
209+ public set nbOfParticipants ( nbOfParticipants : number ) {
210+ this . #nbOfParticipants = nbOfParticipants
211+ this . emit ( "participants" , nbOfParticipants )
212+ }
194213
195214 get ownId ( ) : NodeID {
196215 if ( this . _ownId === undefined ) {
0 commit comments