felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rcbandit2 <peter.pen...@gmail.com>
Subject RE: How to add Java network server into OSGI bundle
Date Sun, 12 Feb 2012 15:46:06 GMT

This is the source code of the Activator:

package org.DX_57.osgi.CB_27.impl;

import java.util.Properties;
import org.DX_57.osgi.CB_27.api.CBridge;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class CBridgeApp implements BundleActivator {

    private EchoServer method;
    
    public void start(BundleContext bc) throws Exception {
        ServiceRegistration registerService =
bc.registerService(CBridge.class.getName(), new CBridgeImpl(), new
Properties());
        method = new EchoServer();
        method.start();
    }

    public void stop(BundleContext bc) throws Exception {
        boolean ungetService =
bc.ungetService(bc.getServiceReference(CBridge.class.getName()));
        method.stop();        
    }
}


This is the source code of the Java server:

package org.DX_57.osgi.CB_27.impl;

import java.net.*;
import java.io.*;

public class EchoServer {    
    
    private volatile boolean started;
    ServerSocket m_ServerSocket;   
      
   
    public void start() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                started = true;

                try {
                    m_ServerSocket = new ServerSocket(12111);
                } catch(IOException ioe) {
                    System.out.println("Could not create server socket at
12111. Quitting.");
                    System.exit(-1);
                }
                System.out.println("Listening for clients on 12111...");

                // Successfully created Server Socket. Now wait for
connections.
                int id = 0;

                while (started) {
                    try {

                        Socket clientSocket = m_ServerSocket.accept();
                        ClientServiceThread cliThread = new
ClientServiceThread(clientSocket, id++);
                        cliThread.start();    
                    } catch(IOException ioe) {
                        System.out.println("Exception encountered on accept.
Ignoring. Stack Trace :");
                        ioe.printStackTrace();
                    }
                }
            }
        }).start();
    }
    
    Socket m_clientSocket; 
    
    class ClientServiceThread extends Thread {
        
        Socket m_clientSocket;        
        int m_clientID = -1;
        boolean m_bRunThread = true;
        
        ClientServiceThread(Socket s, int clientID){
            m_clientSocket = s;
            m_clientID = clientID;
        }
       
        public void run(){            
         
            BufferedReader in = null; 
            PrintWriter out = null;
            
            System.out.println("Accepted Client : ID - " + m_clientID + " :
Address - " + 
                             m_clientSocket.getInetAddress().getHostName());
                
            try{                                
                in = new BufferedReader(new
InputStreamReader(m_clientSocket.getInputStream()));
                out = new PrintWriter(new
OutputStreamWriter(m_clientSocket.getOutputStream()));
                                
                // Run in a loop until m_bRunThread is set to false
                while(m_bRunThread){                    
                    // read incoming stream
                    String clientCommand = in.readLine();
                    
                    System.out.println("Client Says :" + clientCommand);
                    
                    
                    if(clientCommand.equalsIgnoreCase("quit"))
                    {
                        // Special command. Quit this thread
                        m_bRunThread = false;   
                        System.out.print("Stopping client thread for client
: " + m_clientID);
                    }else{
                        // Echo it back to the client.
                        out.println(clientCommand);
                        out.flush();
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }finally{
                // Clean up
                try{                    
                    in.close();
                    out.close();
                    m_clientSocket.close();
                    System.out.println("...Stopped");
                }
                catch(IOException ioe){
                    ioe.printStackTrace();
                }
            }
        }
    }
    
    public void stop() throws IOException {
        started = false;
        m_ServerSocket.close();        
    }

}





Lulseged Zerfu wrote:
> 
> How did you solve it?
> 
> -----Original Message-----
> From: rcbandit2 [mailto:peter.penzov@gmail.com] 
> Sent: den 11 februari 2012 22:40
> To: users@felix.apache.org
> Subject: Re: How to add Java network server into OSGI bundle
> 
> 
> Problem is solved! Thank you!
> 
> 
> Christian Schneider wrote:
>> 
>> Hi,
>> 
>> the problem is that the EchoServer constructor never returns. This is 
>> ok if you call it in a java main but not in an OSGi activator.
>> Like Ivanhoe already hinted you need to start a new thread where your 
>> loop can run. This is outside the scope of OSGi though.
>> 
>> Christian
>> 
>> 
>> Am 11.02.2012 15:21, schrieb rcbandit2:
>>> This the complete source code:
>>> http://www.2shared.com/file/jnGjnZjZ/CB_27.html
>>> How I must edit the code in order to work?
>>> I'm new to java.
>>>
>>>
>> 
>> --
>> Christian Schneider
>> http://www.liquid-reality.de
>> 
>> Open Source Architect
>> Talend Application Integration Division http://www.talend.com
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>> 
>> 
>> 
> 
> --
> View this message in context:
> http://old.nabble.com/How-to-add-Java-network-server-into-OSGI-bundle-tp3329
> 5152p33307899.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/How-to-add-Java-network-server-into-OSGI-bundle-tp33295152p33310364.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Mime
View raw message