Getting port number

136 views
Skip to first unread message

Channing

unread,
Jul 13, 2011, 7:24:01 AM7/13/11
to Lift
Hi,
I need to be able to get the port number of the running app at runtime
and potentially from calls from Comet actors. Using S doesn't work,
any other way?

Channing

atta ur rehman

unread,
Jul 13, 2011, 7:38:17 AM7/13/11
to lif...@googlegroups.com
How about accessing S.servletRequest and then getServerPort() on it? 


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Channing

unread,
Jul 13, 2011, 8:04:25 AM7/13/11
to Lift
S.servletRequest does not exist in 2.4 as far as I can tell.

I tried S.request (a Box[Req]) which returned a Full(Req), but
req.request is null.

This stuff is being called from within an actor's render method and
later from code that will asynchronously send a message to an actor so
there is no session or request guaranteed to be available.

I was thinking that there might be something global I could poke
around in to find out what port the server is running on.

Tim Nelson

unread,
Jul 13, 2011, 8:30:04 AM7/13/11
to lif...@googlegroups.com
Hi,

There won't be anything available to your comet actor to get the port number. What you need to do is capture it on first load and save it as a local val so it's there when you need it.

The port number is available at:

val port: Box[Int] = S.request.map(_.request.serverPort)

Tim

On Wednesday, July 13, 2011 7:04:25 AM UTC-5, Channing wrote:
S.servletRequest does not exist in 2.4 as far as I can tell.

I tried S.request (a Box[Req]) which returned a Full(Req), but
req.request is null.

This stuff is being called from within an actor's render method and
later from code that will asynchronously send a message to an actor so
there is no session or request guaranteed to be available.

I was thinking that there might be something global I could poke
around in to find out what port the server is running on.

atta ur rehman

unread,
Jul 13, 2011, 8:29:59 AM7/13/11
to lif...@googlegroups.com

Diego Medina

unread,
Jul 13, 2011, 8:35:35 AM7/13/11
to lif...@googlegroups.com

There is also S.hostAndPath. But as Tim said, comet actors don't have access to the S object

> To view this discussion on the web visit https://groups.google.com/d/msg/liftweb/-/v_-xEmlRKL8J.

Andreas Joseph Krogh

unread,
Jul 13, 2011, 8:50:07 AM7/13/11
to lif...@googlegroups.com
On Wed, Jul 13, 2011 at 2:35 PM, Diego Medina <di...@fmpwizard.com> wrote:

There is also S.hostAndPath. But as Tim said, comet actors don't have access to the S object


From a CometActor you can always capture the original request which led to the creation of the actor by overriding captureInitialReq:

  /**
   * Comet Actors live outside the HTTP request/response cycle.
   * However, it may be useful to know what Request led to the
   * creation of the CometActor.  You can override this method
   * and capture the initial Req object.  Note that keeping a reference
   * to the Req may lead to memory retention issues if the Req contains
   * large message bodies, etc.  It's optimal to capture the path
   * or capture any request parameters that you care about rather
   * the keeping the whole Req reference.
   */
  protected def captureInitialReq(initialReq: Box[Req]) {}


--
Andreas Joseph Krogh <and...@officenet.no>
Senior Software Developer / CTO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Rosenholmveien 25       | know how to do a thing and to watch         |
1414 Trollåsen          | somebody else doing it wrong, without       |
NORWAY                  | comment.                                    |
Org.nr: NO 981 479 076  |                                             |
                        |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+


David Pollak

unread,
Jul 13, 2011, 8:59:57 AM7/13/11
to lif...@googlegroups.com
CometActors exist outside of the HTTP request/response cycle.  This means that during the processing of a message in a CometActor, you don't get to see the Req because there's never a Req available as part of message processing.

Contrary to some of the posts on this thread, the S context *is* available as are SessionVars (but not RequestVars because the CometActor is outside of the scope of a request).

If you use the Req instance by overriding:

  /**
   * Is this CometActor going to capture the initial Req
   * object?  If yes, override this method and return true
   * and override captureInitialReq to capture the Req.  Why
   * have to explicitly ask for the Req? In order to send Req
   * instances across threads, the Req objects must be snapshotted
   * which is the process of reading the POST or PUT body from the
   * HTTP request stream.  We don't want to do this unless we
   * have to, so by default the Req is not snapshotted/sent.  But
   * if you want it, you can have it.
   */
  override def sendInitialReq_? : Boolean = true

And implementing:

  /**
   * Comet Actors live outside the HTTP request/response cycle.
   * However, it may be useful to know what Request led to the
   * creation of the CometActor.  You can override this method
   * and capture the initial Req object.  Note that keeping a reference
   * to the Req may lead to memory retention issues if the Req contains
   * large message bodies, etc.  It's optimal to capture the path
   * or capture any request parameters that you care about rather
   * the keeping the whole Req reference.
   */
  override protected def captureInitialReq(initialReq: Box[Req]) { /* do something here */ }

You will still not capture the underlying ServletRequest because the Req instance is a copy and that copy nulls out the underlying ServletRequest because some containers reuse ServletRequest instances and that causes problems across threads (and everything in a CometActor is done in the Actor pool threads).

Your best bet is to create a SessionVar (or a few SessionVars) that hold the host and port.  Set those SessionVars from a snippet (if they are not already set).  Then access the SessionVars from your CometActors.

Hope this helps.

Thanks,

David

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net

Channing Walton

unread,
Jul 13, 2011, 11:58:59 AM7/13/11
to lif...@googlegroups.com
Thanks everyone for the help, I think I have a way though now. 

Reply all
Reply to author
Forward
0 new messages