1 24 25 package org.objectweb.dream.channel; 26 27 import java.io.IOException ; 28 import java.io.ObjectInputStream ; 29 import java.util.Collections ; 30 31 import org.objectweb.dream.AbstractComponent; 32 import org.objectweb.dream.control.activity.Util; 33 import org.objectweb.dream.control.activity.task.AbstractTask; 34 import org.objectweb.dream.control.activity.task.Task; 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.NoSuchInterfaceException; 37 import org.objectweb.fractal.api.control.IllegalBindingException; 38 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 39 import org.objectweb.util.monolog.api.BasicLevel; 40 41 46 public abstract class AbstractAcceptSocketImpl extends AbstractComponent 47 { 48 49 protected Task acceptTask = new AcceptTask(); 50 51 55 protected OpenedSocket openedSocketItf; 56 57 61 64 protected abstract SocketState acceptSocket() throws IOException ; 65 66 70 protected class AcceptTask extends AbstractTask 71 { 72 73 74 public AcceptTask() 75 { 76 super("ChannelInTask"); 77 } 78 79 82 public Object execute(Object hints) throws InterruptedException 83 { 84 SocketState socketState; 85 ObjectInputStream ois = null; 86 try 87 { 88 logger.log(BasicLevel.DEBUG, "accept socket."); 89 socketState = acceptSocket(); 90 logger.log(BasicLevel.DEBUG, "connected"); 91 } 92 catch (IOException e) 93 { 94 logger.log(BasicLevel.ERROR, 95 "An error occrued while accepting incoming connections", e); 96 return EXECUTE_AGAIN; 97 } 98 try 99 { 100 openedSocketItf.openedSocket(socketState); 101 } 102 catch (IOException e) 103 { 104 logger.log(BasicLevel.WARN, "Input/Output socket error", e); 105 socketState.close(); 106 } 107 108 return EXECUTE_AGAIN; 109 } 110 } 111 112 116 119 protected void beforeFirstStart(Component componentItf) 120 throws IllegalLifeCycleException 121 { 122 try 123 { 124 Util.addTask(componentItf, acceptTask, Collections.EMPTY_MAP); 125 } 126 catch (Exception e) 127 { 128 throw new IllegalLifeCycleException("Can't add task"); 129 } 130 } 131 132 136 139 public String [] listFc() 140 { 141 return new String []{OpenedSocket.ITF_NAME}; 142 } 143 144 148 public void bindFc(String clientItfName, Object serverItf) 149 throws NoSuchInterfaceException, IllegalBindingException, 150 IllegalLifeCycleException 151 { 152 super.bindFc(clientItfName, serverItf); 153 if (clientItfName.equals(OpenedSocket.ITF_NAME)) 154 { 155 openedSocketItf = (OpenedSocket) serverItf; 156 } 157 } 158 } | Popular Tags |