~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to examples/org/jboss/remoting/samples/multiplex/PrimeScenarioExampleServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
 
23
 
/* 
24
 
 * Created on Jul 24, 2005
25
 
 */
26
 
package org.jboss.remoting.samples.multiplex;
27
 
 
28
 
import java.io.IOException;
29
 
import java.io.ObjectInputStream;
30
 
import java.io.ObjectOutputStream;
31
 
import java.net.ServerSocket;
32
 
import java.net.Socket;
33
 
 
34
 
import org.jboss.remoting.transport.multiplex.MasterServerSocket;
35
 
import org.jboss.remoting.transport.multiplex.VirtualSocket;
36
 
 
37
 
/**
38
 
 * 
39
 
 * <p>
40
 
 * Copyright (c) 2005
41
 
 * <p>
42
 
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
43
 
 */
44
 
public class PrimeScenarioExampleServer
45
 
{
46
 
   public void runPrimeScenario()
47
 
   {
48
 
      try
49
 
      {
50
 
         // create a MasterServerSocket and get a VirtualSocket
51
 
         ServerSocket serverSocket = new MasterServerSocket(5555);
52
 
         serverSocket.setSoTimeout(10000);
53
 
         Socket v2 = serverSocket.accept();
54
 
         
55
 
         // do some asynchronous communication in a separate thread
56
 
         Thread asynchronousThread = new AsynchronousThread(v2);
57
 
         asynchronousThread.start();
58
 
         
59
 
         // do some synchronous communication
60
 
         ObjectInputStream ois = new ObjectInputStream(v2.getInputStream());
61
 
         ObjectOutputStream oos = new ObjectOutputStream(v2.getOutputStream());
62
 
         v2.setSoTimeout(10000);
63
 
         Object o = ois.readObject();
64
 
         oos.writeObject(o);
65
 
         
66
 
         asynchronousThread.join();
67
 
         serverSocket.close();
68
 
         v2.close();
69
 
      }
70
 
      
71
 
      catch (Exception e)
72
 
      {
73
 
         e.printStackTrace();
74
 
         System.exit(1);
75
 
      }
76
 
   }
77
 
   
78
 
   
79
 
   class AsynchronousThread extends Thread
80
 
   {
81
 
      private Socket virtualSocket;
82
 
      
83
 
      public AsynchronousThread(Socket socket) throws IOException
84
 
      {
85
 
         this.virtualSocket = socket;
86
 
      }
87
 
      
88
 
      public void run()
89
 
      {   
90
 
         try
91
 
         {
92
 
            // Give VirtualServerSocket a chance to start.
93
 
            Thread.sleep(2000);
94
 
            
95
 
            // connect to VirtualServerSocket
96
 
            String hostName = virtualSocket.getInetAddress().getHostName();
97
 
            int port = virtualSocket.getPort();
98
 
            Socket v3 = new VirtualSocket(hostName, port);
99
 
            
100
 
            // send an object to the client
101
 
            ObjectOutputStream oos = new ObjectOutputStream(v3.getOutputStream());
102
 
            oos.writeObject(new Integer(7));
103
 
            
104
 
            oos.flush();
105
 
            v3.close();
106
 
         }
107
 
         catch (Exception e)
108
 
         {
109
 
            e.printStackTrace();
110
 
            System.exit(1);
111
 
         }
112
 
      }
113
 
   }
114
 
   
115
 
   
116
 
   
117
 
   public static void main(String[] args)
118
 
   {
119
 
      new PrimeScenarioExampleServer().runPrimeScenario();
120
 
   }
121
 
   
122
 
 
123
 
}