| Commit message (Collapse) | Author | Age | Files | Lines |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
The spec has clients pull updates every half second or so, by
fetching everything, only changes, or something in between. Take the
middle road: the engine keeps a version that moves on every change,
and GET /api/sync?since={version} answers {"changed": false} when
nothing has happened, which is almost every poll. Otherwise it returns
a snapshot of the event summaries, the user directory and the caller's
account, with the version to send next time. A client looking at one
event refetches its details from /api/events/{name} only when the
version moves.
The lock filter bumps the version after every state-changing request,
still inside the write lock, so a reader can never pick up new data
under an old version. CORS preflights now take the read lock, since
they change nothing. A session that times out also bumps the version,
so other users see that user go offline. The user directory rows move
into a helper that /api/users and the snapshot share.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
The engine was written for a single-threaded desktop app: users,
events and ledgers live in plain lists, and a balance update is a
read followed by a write. Tomcat runs each request on its own thread,
so once clients poll while others trade, a read can iterate a list
mid-insert and fail with a 500, and two deposits can both read the
same old balance and lose one update.
Every server-side call into the engine arrives through /api/*, so put
one ReentrantReadWriteLock in a filter there. GETs, which is all the
polling, share the read lock and still run in parallel. Everything else
takes the write lock and changes the market alone, which also makes
check-then-act steps like opening an event or claiming a username
atomic. Writes are short in-memory operations, so one lock is enough.
Per-event locks are the next step if trading volume ever needs them.
The test runs writers depositing into one account while others poll
three endpoints and log new users in. Without the filter it fails with
500s; with it, every request succeeds and the balance and ledger count
match the number of deposits.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Give each logged-in user their own account view and a view of
everyone else, as the spec asks:
- GET /api/user/account returns the caller's balance, blocked flag and
every ledger row (trades, fees, payouts, deposits).
- POST /api/user/deposit (form field "amount") tops the account up
through AccountContext.deposit, so its validation and DEPOSIT ledger
entry are the same as in the engine. Bad amounts get 400.
- GET /api/users lists every account with its name, balance and
whether it makes any market, the three facts the spec requires about
other users, plus whether it is logged in right now, which the login
registry already knows. Accounts outlive their sessions, so the list
includes users who are currently logged out.
A test drives two clients through a whole market: one deposits,
uploads and opens an event, the other deposits and buys, the first
resolves it, and the winner's balance rises by exactly one per winning
share.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Expose the market to logged-in clients:
- GET /api/events lists every event, GET /api/events/{name} returns
one event's details, order books, trades and participants.
- POST /api/events/{name}/open and /resolve (form field "option")
open and settle an event. The engine's openEvent does not know who
is asking, so the servlet checks the caller is the event's Market
Maker and answers 403 otherwise, for both actions.
- POST /api/orders places a buy or sell order as the logged-in user,
never as a name the client sends.
Events are addressed by their URL-encoded name, which is their
identity in v3. Request input is checked before it reaches the engine.
The engine treats any side other than SELL as a buy, takes any
quantity, and Event.getOption throws on an unknown key, so all of
these would have turned client mistakes into wrong trades or 500
errors. Above all, settling on an unknown winning option would close
the event and pay nobody. The servlets reject these as 400s.
Login is checked inside each servlet through Sessions.requireUser
rather than in a second filter: the order of annotation-registered
filters is unspecified, and an auth filter running before CorsFilter
would send 401s without CORS headers, which browsers report as
network errors.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Add POST /api/upload: a logged-in user sends a Schema v3 file as a
multipart part, its events join the market for everyone, and the
uploader becomes their Market Maker. It uses the Servlet API's own
multipart support, as the spec asks, and hands the part's stream to
the v3 loader, so validation errors (bad XML, broken Exercise 1 rules,
a name already in the market) come back as 400 with their reason.
As in Exercise 1, a file whose name does not end in .xml is refused
before it is read, since a request need not come from our client.
The spec warns that the grading server cannot write files, and Tomcat
writes any multipart part larger than @MultipartConfig's
fileSizeThreshold to a temp file. That threshold defaults to 0, so a
plain upload servlet always touches the disk. Cap uploads at 1 MB and
put the threshold above the whole request limit, so every upload stays
in memory. Bigger files get 413, requests without a session 401, and
non-multipart bodies 400.
Tomcat deletes spooled parts once the request ends, so comparing the
files before and after an upload cannot catch spooling. The test
instead watches the webapp's temp directory for any file being
created while a near-limit upload runs. It fails if the threshold is
set back to 0.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
The spec asks for a login by unique name only, with no passwords or
sign-up, and an error that lets the user try again if the name exists.
Add POST /api/login (form field "username") and POST /api/logout.
Sessions maps each name, ignoring case, to the one HTTP session
holding it. A second session asking for a held name gets 409
NAME_TAKEN, and a blank name gets 400. When a session ends, by logout
or by Tomcat timing it out after a client quits without logging out,
an HttpSessionListener frees the name. The account itself (ledger,
Market Maker roles) stays, and logging in with that name again resumes
it rather than locking the user out for the life of the server. New
accounts start with a zero balance and are funded through deposits.
Json gives every endpoint the same response shape, with errors as
{"error": CODE, "message": text} built from a failed Result. Tomcat
setup for the tests moves into a shared TestServer helper, whose
clients each keep their own cookie jar and so act as separate users.
|
|
|
Exercise 3 moves the market behind a Tomcat server that the desktop
and browser clients share. Add a server module, registered in the
build scripts, holding the pieces every later endpoint builds on:
- ServerEngine wires one in-memory market (repositories, ledger,
trading pipeline, v3 upload loader) and lives in the servlet context
for the webapp's lifetime. Its catalog refuses path-based loading,
so events can only arrive through uploaded streams and the server
never reads or writes files.
- EngineBootstrap builds that engine when the webapp deploys.
- CorsFilter answers preflights and echoes the caller's Origin with
Allow-Credentials, which the browser client needs to send its
session cookie from another origin. With usernames as the only
login, echoing any origin exposes nothing a page couldn't get by
logging in itself.
- /api/health reports that the engine is up.
Vendor tomcat-juli 10.1.26 next to the other Tomcat jars. It ships in
Tomcat's bin/ rather than lib/, and embedded Tomcat cannot start
without it. A test deploys the module into embedded Tomcat, so
annotation scanning, the health check and CORS are checked against a
real container.
|