| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.dm.ProtocolServer = function VeDmProtocolServer( documentStore, logger ) { |
| 21 |
this.logger = logger; |
| 22 |
this.rebaseServer = new ve.dm.RebaseServer(); |
| 23 |
this.lastAuthorForDoc = new Map(); |
| 24 |
this.loadingForDoc = new Map(); |
| 25 |
this.documentStore = documentStore; |
| 26 |
this.logger.logServerEvent( { type: 'restart' }, 'info' ); |
| 27 |
}; |
| 28 |
|
| 29 |
OO.initClass( ve.dm.ProtocolServer ); |
| 30 |
|
| 31 |
ve.dm.ProtocolServer.static.palette = [ |
| 32 |
'1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd', |
| 33 |
'8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf', |
| 34 |
'aec7e8', 'ffbb78', '98df8a', 'ff9896', 'c5b0d5', |
| 35 |
'c49c94', 'f7b6d2', 'c7c7c7', 'dbdb8d', '9edae5' |
| 36 |
]; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
ve.dm.ProtocolServer.prototype.ensureLoaded = function ( docName ) { |
| 45 |
const rebaseServer = this.rebaseServer; |
| 46 |
|
| 47 |
let loading = this.loadingForDoc.get( docName ); |
| 48 |
|
| 49 |
if ( loading ) { |
| 50 |
return loading; |
| 51 |
} |
| 52 |
this.logger.logServerEvent( { type: 'ProtocolServer#load', docName: docName } ); |
| 53 |
loading = this.documentStore.load( docName ).then( ( change ) => { |
| 54 |
this.logger.logServerEvent( { |
| 55 |
type: 'ProtocolServer#loaded', |
| 56 |
docName: docName, |
| 57 |
length: change.getLength() |
| 58 |
} ); |
| 59 |
rebaseServer.updateDocState( docName, null, change ); |
| 60 |
} ); |
| 61 |
this.loadingForDoc.set( docName, loading ); |
| 62 |
return loading; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ve.dm.ProtocolServer.prototype.authenticate = function ( docName, authorId, token ) { |
| 77 |
const state = this.rebaseServer.stateForDoc.get( docName ), |
| 78 |
authorData = state && state.authors.get( authorId ); |
| 79 |
|
| 80 |
if ( !authorData || token !== authorData.token ) { |
| 81 |
authorId = 1 + ( this.lastAuthorForDoc.get( docName ) || 0 ); |
| 82 |
this.lastAuthorForDoc.set( docName, authorId ); |
| 83 |
token = Math.random().toString( 36 ).slice( 2 ); |
| 84 |
} |
| 85 |
const context = { |
| 86 |
serverId: this.documentStore.serverId, |
| 87 |
docName: docName, |
| 88 |
authorId: authorId |
| 89 |
}; |
| 90 |
this.logger.logServerEvent( { |
| 91 |
type: 'newClient', |
| 92 |
doc: docName, |
| 93 |
authorId: context.authorId |
| 94 |
} ); |
| 95 |
return context; |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
ve.dm.ProtocolServer.prototype.onLogEvent = function ( context, event ) { |
| 105 |
const ob = {}; |
| 106 |
ob.recvTimestamp = this.logger.getRelativeTimestamp(); |
| 107 |
ob.clientId = context.authorId; |
| 108 |
ob.doc = context.docName; |
| 109 |
for ( const key in event ) { |
| 110 |
ob[ key ] = event[ key ]; |
| 111 |
} |
| 112 |
this.logger.logEvent( ob ); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
ve.dm.ProtocolServer.prototype.welcomeClient = function ( context, startLength, usernameGenerator ) { |
| 123 |
const docName = context.docName, |
| 124 |
serverId = context.serverId, |
| 125 |
authorId = context.authorId; |
| 126 |
|
| 127 |
if ( !startLength ) { |
| 128 |
startLength = 0; |
| 129 |
} |
| 130 |
this.rebaseServer.updateDocState( docName, authorId, null, { |
| 131 |
name: usernameGenerator ? usernameGenerator( authorId ) : 'User ' + authorId, |
| 132 |
color: this.constructor.static.palette[ |
| 133 |
authorId % this.constructor.static.palette.length |
| 134 |
], |
| 135 |
active: true |
| 136 |
} ); |
| 137 |
|
| 138 |
const state = this.rebaseServer.getDocState( docName ); |
| 139 |
const authorData = state.authors.get( authorId ); |
| 140 |
|
| 141 |
context.sendAuthor( 'registered', { |
| 142 |
serverId: serverId, |
| 143 |
authorId: authorId, |
| 144 |
token: authorData.token |
| 145 |
} ); |
| 146 |
context.broadcast( 'authorChange', { |
| 147 |
authorId: authorId, |
| 148 |
authorData: { |
| 149 |
name: authorData.name, |
| 150 |
color: authorData.color |
| 151 |
} |
| 152 |
} ); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
… } |
| 158 |
context.sendAuthor( 'initDoc', { |
| 159 |
history: state.history.mostRecent( startLength ).serialize( true ), |
| 160 |
authors: state.getActiveAuthors() |
| 161 |
} ); |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
ve.dm.ProtocolServer.prototype.onSubmitChange = function ( context, data ) { |
| 171 |
const change = ve.dm.Change.static.deserialize( data.change, true ); |
| 172 |
const applied = this.rebaseServer.applyChange( context.docName, context.authorId, data.backtrack, change ); |
| 173 |
if ( !applied.isEmpty() ) { |
| 174 |
this.documentStore.onNewChange( context.docName, applied ); |
| 175 |
context.broadcast( 'newChange', applied.serialize( true ) ); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
ve.dm.ProtocolServer.prototype.onChangeAuthor = function ( context, newData ) { |
| 186 |
this.rebaseServer.updateDocState( context.docName, context.authorId, null, { |
| 187 |
name: newData.name, |
| 188 |
color: newData.color |
| 189 |
} ); |
| 190 |
context.broadcast( 'authorChange', { |
| 191 |
authorId: context.authorId, |
| 192 |
authorData: { |
| 193 |
name: newData.name, |
| 194 |
color: newData.color |
| 195 |
} |
| 196 |
} ); |
| 197 |
this.logger.logServerEvent( { |
| 198 |
type: 'authorChange', |
| 199 |
doc: context.docName, |
| 200 |
authorId: context.authorId, |
| 201 |
authorData: { |
| 202 |
name: newData.name, |
| 203 |
color: newData.color |
| 204 |
} |
| 205 |
} ); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ve.dm.ProtocolServer.prototype.onDisconnect = function ( context ) { |
| 214 |
this.rebaseServer.updateDocState( context.docName, context.authorId, null, { |
| 215 |
active: false, |
| 216 |
continueBase: null, |
| 217 |
rejections: null |
| 218 |
} ); |
| 219 |
context.broadcast( 'authorDisconnect', context.authorId ); |
| 220 |
this.logger.logServerEvent( { |
| 221 |
type: 'disconnect', |
| 222 |
doc: context.docName, |
| 223 |
authorId: context.authorId |
| 224 |
} ); |
| 225 |
}; |