Handling responses from an outputstream

I have an InputStream and an OutputStream to a server. I can send a bunch of JSON requests over the wire and get a stream of responses back from this server. From this, I'd like to build a wrapper that provides getters like getFoo() that create a request, send it, and get a response.

I'm not sure how to handle this considering I may have many clients using this single InputStream and OutputStream at a given time, and am hoping there's a pattern I can use.

Here's one option I considered:

  • InputStream reader constantly reads responses, and puts them in a Map<RequestType, Response> that stores the latest Response for each RequestType (where requestType is getFoo, getBar, etc)
  • When I call getFoo() on my wrapper, it writes the request to OutputStream, and then sits waiting for responseMap.get(RequestType.FOO), reads it, and deletes that entry in the map.

Another option:

  • When I call getFoo() on my wrapper, it registers itself as a callback with the InputStream reader
  • When the InputStream reader parses a response (e.g. the foo response from above) it notifies all listeners waiting for foo response, then clears the listeners.

Both of these seem fragile when I start thinking about concurrency. Is there a better way to approach this?

Replay

I have an InputStream and an OutputStream to a server. I can send a bunch of JSON requests over the wire and get a stream of responses back from this server

Can you not just open multiple connections to the server, and hence get multiple input/output streams ? In that scenario you can assert that the component opening that connection will only get the responses meant for it, and it alone.

Category: java Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.179 (s). 12 q(s)